introduce a ComponentID Enum to manage ids for gci dashboards

This change introduces an enum that assigns unique ids to gci
dashboard components and eliminates the needs to assign them manually.

Change-Id: I80da26ed75718a7adb1ae81a2618289de58183a9
diff --git a/app/soc/modules/gci/views/dashboard.py b/app/soc/modules/gci/views/dashboard.py
index 9dc0d53..dd7d793 100644
--- a/app/soc/modules/gci/views/dashboard.py
+++ b/app/soc/modules/gci/views/dashboard.py
@@ -14,6 +14,7 @@
 
 """Module for the GCI participant dashboard."""
 
+import enum
 import json
 import logging
 
@@ -419,11 +420,24 @@
       return None
 
 
+class ComponentID(enum.Enum):
+  (
+      MY_ORGS_TASK_LIST,
+      MANAGED_ORGANIZATIONS,
+      MY_ORGS_LIST,
+      MY_ORGS_LIST_BEFORE_CREATE_TASK,
+      MY_ORGS_LIST_BEFORE_BULK_CREATE_TASK,
+      MY_ORGS_SCORES_LIST,
+      MY_ORGS_MENTORS_LIST,
+      DOCUMENT
+  ) = range(8)
+
+
 class MyOrgsTaskList(Component):
   """Component for listing the tasks of the orgs of the current user.
   """
 
-  IDX = 1
+  IDX = ComponentID.MY_ORGS_TASK_LIST
   PUBLISH_BUTTON_ID = 'publish'
   UNPUBLISH_BUTTON_ID = 'unpublish'
 
@@ -550,7 +564,7 @@
   def post(self):
     """Processes the form post data by checking what buttons were pressed."""
     idx = lists.getListIndex(self.data.request)
-    if idx != self.IDX:
+    if idx != self.IDX.value:
       return None
 
     data = self.data.POST.get('data')
@@ -611,7 +625,7 @@
   def context(self):
     """Returns the context of this component."""
     task_list = lists.ListConfigurationResponse(
-        self.data, self._list_config, idx=self.IDX, preload_list=False)
+        self.data, self._list_config, idx=self.IDX.value, preload_list=False)
 
     return {
         'name': 'all_org_tasks',
@@ -626,7 +640,7 @@
     If the lists as requested is not supported by this component None is
     returned.
     """
-    if lists.getListIndex(self.data.request) != 1:
+    if lists.getListIndex(self.data.request) != self.IDX.value:
       return None
 
     q = GCITask.all()
@@ -653,7 +667,7 @@
   the current user.
   """
 
-  IDX = 5
+  IDX = ComponentID.MANAGED_ORGANIZATIONS
 
   def __init__(self, data):
     """Initializes the component.
@@ -736,7 +750,7 @@
   def context(self):
     """Returns the context of this component."""
     list_configuration_response = lists.ListConfigurationResponse(
-        self.data, self._list_config, idx=self.IDX, preload_list=False)
+        self.data, self._list_config, idx=self.IDX.value, preload_list=False)
 
     return {
         'name': 'org_app',
@@ -752,7 +766,7 @@
     If the lists as requested is not supported by this component None is
     returned.
     """
-    if lists.getListIndex(self.data.request) != self.IDX:
+    if lists.getListIndex(self.data.request) != self.IDX.value:
       return None
 
     orgs = ndb.get_multi(self.data.ndb_profile.admin_for)
@@ -772,6 +786,8 @@
   Since mentor_for is a list of orgs, we need to give org selection first
   """
 
+  IDX = ComponentID.MY_ORGS_LIST
+
   def __init__(self, data):
     """Initializes the component.
 
@@ -788,11 +804,6 @@
 
     self._setRowAction(data.request, data)
 
-    self._setIdx()
-
-  def _setIdx(self):
-    raise NotImplementedError
-
   # TODO(nathaniel): Drop the "request" parameter of this method.
   def _setRowAction(self, request, data):
     """Since setRowAction can be vary, it must be implemented individually.
@@ -818,7 +829,7 @@
     If the lists as requested is not supported by this component None is
     returned.
     """
-    if lists.getListIndex(self.data.request) != self.idx:
+    if lists.getListIndex(self.data.request) != self.IDX.value:
       return None
 
     response = lists.ListContentResponse(self.data.request, self._list_config)
@@ -838,12 +849,11 @@
   task.
   """
 
-  def _setIdx(self):
-    self.idx = 2
+  IDX = ComponentID.MY_ORGS_LIST_BEFORE_CREATE_TASK
 
   def _getContext(self):
     org_list = lists.ListConfigurationResponse(
-        self.data, self._list_config, idx=self.idx, preload_list=False)
+        self.data, self._list_config, idx=self.IDX.value, preload_list=False)
 
     return {
         'name': 'create_tasks',
@@ -865,12 +875,11 @@
   task.
   """
 
-  def _setIdx(self):
-    self.idx = 3
+  IDX = ComponentID.MY_ORGS_LIST_BEFORE_BULK_CREATE_TASK
 
   def _getContext(self):
     org_list = lists.ListConfigurationResponse(
-        self.data, self._list_config, idx=self.idx, preload_list=False)
+        self.data, self._list_config, idx=self.IDX.value, preload_list=False)
 
     return {
         'name': 'bulk_create_tasks',
@@ -892,8 +901,7 @@
   see scores of the students.
   """
 
-  def _setIdx(self):
-    self.idx = 12
+  IDX = ComponentID.MY_ORGS_SCORES_LIST
 
   def _setRowAction(self, request, data):
     self._list_config.setRowAction(
@@ -902,7 +910,7 @@
                 entity.key, gci_url_names.GCI_ORG_SCORES)))
 
   def getListData(self):
-    if lists.getListIndex(self.data.request) != self.idx:
+    if lists.getListIndex(self.data.request) != self.IDX.value:
       return None
 
     orgs = ndb.get_multi(self.data.ndb_profile.admin_for)
@@ -917,7 +925,7 @@
 
   def _getContext(self):
     org_list = lists.ListConfigurationResponse(
-        self.data, self._list_config, idx=self.idx, preload_list=False)
+        self.data, self._list_config, idx=self.IDX.value, preload_list=False)
 
     return {
         'name': 'orgs_scores',
@@ -931,6 +939,8 @@
   """Component for listing the mentors of the orgs of the current user.
   """
 
+  IDX = ComponentID.MY_ORGS_MENTORS_LIST
+
   def __init__(self, data):
     """Initializes the component.
 
@@ -956,7 +966,7 @@
   def context(self):
     """Returns the context of this component."""
     list_configuration_response = lists.ListConfigurationResponse(
-        self.data, self._list_config, idx=6, preload_list=False)
+        self.data, self._list_config, idx=self.IDX.value, preload_list=False)
 
     return {
         'name': 'all_orgs_mentors',
@@ -971,7 +981,7 @@
     If the lists as requested is not supported by this component None is
     returned.
     """
-    if lists.getListIndex(self.data.request) != 6:
+    if lists.getListIndex(self.data.request) != self.IDX.value:
       return None
 
     query = profile_logic.queryAllMentorsForOrg(
@@ -989,7 +999,7 @@
   """Component listing all the documents for the current user.
   """
 
-  IDX = 10
+  IDX = ComponentID.DOCUMENT
 
   def __init__(self, data):
     """Initializes this component.
@@ -1013,7 +1023,7 @@
   def getListData(self):
     idx = lists.getListIndex(self.data.request)
 
-    if idx != self.IDX:
+    if idx != self.IDX.value:
       return None
 
     visibilities = gsoc_document_logic.getVisibilities(self.data.ndb_profile)
@@ -1025,7 +1035,7 @@
 
   def context(self):
     list_config = lists.ListConfigurationResponse(
-        self.data, self._list_config, idx=self.IDX, preload_list=False)
+        self.data, self._list_config, idx=self.IDX.value, preload_list=False)
 
     return {
         'name': 'documents',
diff --git a/tests/app/soc/modules/gci/views/test_dashboard.py b/tests/app/soc/modules/gci/views/test_dashboard.py
index 237b140..7938515 100644
--- a/tests/app/soc/modules/gci/views/test_dashboard.py
+++ b/tests/app/soc/modules/gci/views/test_dashboard.py
@@ -20,7 +20,7 @@
 from seeder import user as user_seeder
 
 from soc.modules.gci.models import task as task_model
-from soc.modules.gci.views import dashboard as dashboard_view
+from soc.modules.gci.views.dashboard import MyOrgsTaskList
 
 from tests import profile_utils
 from tests import task_utils
@@ -72,7 +72,8 @@
 
     response = self.get(self._getDashboardUrl())
     self.assertDashboardComponentTemplatesUsed(response)
-    response = self.getListResponse(self._getDashboardUrl(), 1)
+    response = self.getListResponse(self._getDashboardUrl(),
+                                    MyOrgsTaskList.IDX.value)
     self.assertIsJsonResponse(response)
     data = json.loads(response.content)
     self.assertEqual(1, len(data['data']['']))
@@ -139,12 +140,12 @@
     data = json.dumps([{'key': str(task.key().id())}])
 
     if action == 'publish':
-      button_id = dashboard_view.MyOrgsTaskList.PUBLISH_BUTTON_ID
+      button_id = MyOrgsTaskList.PUBLISH_BUTTON_ID
     else:
-      button_id = dashboard_view.MyOrgsTaskList.UNPUBLISH_BUTTON_ID
+      button_id = MyOrgsTaskList.UNPUBLISH_BUTTON_ID
 
     post_data = {
-        'idx': dashboard_view.MyOrgsTaskList.IDX,
+        'idx': MyOrgsTaskList.IDX.value,
         'data': data,
         'button_id': button_id
         }
@@ -174,7 +175,7 @@
     self.assertResponseOK(response)
 
     list_data = self.getListData(self._getDashboardUrl(),
-                                 dashboard_view.MyOrgsTaskList.IDX)
+                                 MyOrgsTaskList.IDX.value)
     self.assertEqual(len(list_data), 2)
 
   def _getDashboardUrl(self):