OpenStack Dashboard (Horizon)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tables.py 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  2. # not use this file except in compliance with the License. You may obtain
  3. # a copy of the License at
  4. #
  5. # http://www.apache.org/licenses/LICENSE-2.0
  6. #
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  9. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  10. # License for the specific language governing permissions and limitations
  11. # under the License.
  12. from django.core.urlresolvers import reverse # noqa
  13. from django.utils.http import urlencode # noqa
  14. from django.utils.translation import ugettext_lazy as _ # noqa
  15. from horizon import tables
  16. from openstack_dashboard import api
  17. from openstack_dashboard.api import keystone
  18. class ViewMembersLink(tables.LinkAction):
  19. name = "users"
  20. verbose_name = _("Modify Users")
  21. url = "horizon:admin:projects:update"
  22. classes = ("ajax-modal", "btn-edit")
  23. policy_rules = (("identity", "identity:list_users"),
  24. ("identity", "identity:list_roles"))
  25. def get_link_url(self, project):
  26. step = 'update_members'
  27. base_url = reverse(self.url, args=[project.id])
  28. param = urlencode({"step": step})
  29. return "?".join([base_url, param])
  30. class ViewGroupsLink(tables.LinkAction):
  31. name = "groups"
  32. verbose_name = _("Modify Groups")
  33. url = "horizon:admin:projects:update"
  34. classes = ("ajax-modal", "btn-edit")
  35. def allowed(self, request, project):
  36. return keystone.VERSIONS.active >= 3
  37. def get_link_url(self, project):
  38. step = 'update_group_members'
  39. base_url = reverse(self.url, args=[project.id])
  40. param = urlencode({"step": step})
  41. return "?".join([base_url, param])
  42. class UsageLink(tables.LinkAction):
  43. name = "usage"
  44. verbose_name = _("View Usage")
  45. url = "horizon:admin:projects:usage"
  46. classes = ("btn-stats",)
  47. policy_rules = (("compute", "compute_extension:simple_tenant_usage:show"),)
  48. class CreateProject(tables.LinkAction):
  49. name = "create"
  50. verbose_name = _("Create Project")
  51. url = "horizon:admin:projects:create"
  52. classes = ("btn-launch", "ajax-modal",)
  53. policy_rules = (('identity', 'identity:create_project'),)
  54. def allowed(self, request, project):
  55. return api.keystone.keystone_can_edit_project()
  56. class UpdateProject(tables.LinkAction):
  57. name = "update"
  58. verbose_name = _("Edit Project")
  59. url = "horizon:admin:projects:update"
  60. classes = ("ajax-modal", "btn-edit")
  61. policy_rules = (('identity', 'identity:update_project'),)
  62. def allowed(self, request, project):
  63. return api.keystone.keystone_can_edit_project()
  64. class ModifyQuotas(tables.LinkAction):
  65. name = "quotas"
  66. verbose_name = _("Modify Quotas")
  67. url = "horizon:admin:projects:update"
  68. classes = ("ajax-modal", "btn-edit")
  69. policy_rules = (('compute', "compute_extension:quotas:update"),)
  70. def get_link_url(self, project):
  71. step = 'update_quotas'
  72. base_url = reverse(self.url, args=[project.id])
  73. param = urlencode({"step": step})
  74. return "?".join([base_url, param])
  75. class DeleteTenantsAction(tables.DeleteAction):
  76. data_type_singular = _("Project")
  77. data_type_plural = _("Projects")
  78. policy_rules = (("identity", "identity:delete_project"),)
  79. def allowed(self, request, project):
  80. return api.keystone.keystone_can_edit_project()
  81. def delete(self, request, obj_id):
  82. api.keystone.tenant_delete(request, obj_id)
  83. class TenantFilterAction(tables.FilterAction):
  84. def filter(self, table, tenants, filter_string):
  85. """ Really naive case-insensitive search. """
  86. # FIXME(gabriel): This should be smarter. Written for demo purposes.
  87. q = filter_string.lower()
  88. def comp(tenant):
  89. if q in tenant.name.lower():
  90. return True
  91. return False
  92. return filter(comp, tenants)
  93. class TenantsTable(tables.DataTable):
  94. name = tables.Column('name', verbose_name=_('Name'))
  95. description = tables.Column(lambda obj: getattr(obj, 'description', None),
  96. verbose_name=_('Description'))
  97. id = tables.Column('id', verbose_name=_('Project ID'))
  98. enabled = tables.Column('enabled', verbose_name=_('Enabled'), status=True)
  99. class Meta:
  100. name = "tenants"
  101. verbose_name = _("Projects")
  102. row_actions = (ViewMembersLink, ViewGroupsLink, UpdateProject,
  103. UsageLink, ModifyQuotas, DeleteTenantsAction)
  104. table_actions = (TenantFilterAction, CreateProject,
  105. DeleteTenantsAction)
  106. pagination_param = "tenant_marker"