File: views.py

package info (click to toggle)
horizon 3%3A25.3.0-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 32,900 kB
  • sloc: python: 100,002; javascript: 49,478; sh: 453; makefile: 79
file content (179 lines) | stat: -rw-r--r-- 6,727 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# Copyright 2013 Hewlett-Packard Development Company, L.P.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

from django.urls import reverse
from django.urls import reverse_lazy
from django.utils.translation import gettext_lazy as _

from horizon import exceptions
from horizon import forms
from horizon import messages
from horizon import tables
from horizon.utils import memoized

from openstack_dashboard import api
from openstack_dashboard import policy

from openstack_dashboard.dashboards.identity.groups import constants
from openstack_dashboard.dashboards.identity.groups \
    import forms as project_forms
from openstack_dashboard.dashboards.identity.groups \
    import tables as project_tables
from openstack_dashboard.utils import identity
from openstack_dashboard.utils import settings as setting_utils


class IndexView(tables.DataTableView):
    table_class = project_tables.GroupsTable
    template_name = constants.GROUPS_INDEX_VIEW_TEMPLATE
    page_title = _("Groups")

    def needs_filter_first(self, table):
        return self._needs_filter_first

    def get_data(self):
        groups = []
        filters = self.get_filters()
        self._needs_filter_first = False

        if policy.check((("identity", "identity:list_groups"),),
                        self.request):

            # If filter_first is set and if there are not other filters
            # selected, then search criteria must be provided and
            # return an empty list
            if (setting_utils.get_dict_config(
                    'FILTER_DATA_FIRST', 'identity.groups') and not filters):
                self._needs_filter_first = True
                return groups

            domain_id = identity.get_domain_id_for_operation(self.request)
            try:
                groups = api.keystone.group_list(self.request,
                                                 domain=domain_id,
                                                 filters=filters)
            except Exception:
                exceptions.handle(self.request,
                                  _('Unable to retrieve group list.'))
        else:
            msg = _("Insufficient privilege level to view group information.")
            messages.info(self.request, msg)
        return groups


class CreateView(forms.ModalFormView):
    template_name = constants.GROUPS_CREATE_VIEW_TEMPLATE
    form_id = "create_group_form"
    form_class = project_forms.CreateGroupForm
    submit_label = _("Create Group")
    submit_url = reverse_lazy(constants.GROUPS_CREATE_URL)
    success_url = reverse_lazy(constants.GROUPS_INDEX_URL)
    page_title = _("Create Group")


class UpdateView(forms.ModalFormView):
    template_name = constants.GROUPS_UPDATE_VIEW_TEMPLATE
    form_id = "update_group_form"
    form_class = project_forms.UpdateGroupForm
    submit_url = "horizon:identity:groups:update"
    submit_label = _("Update Group")
    success_url = reverse_lazy(constants.GROUPS_INDEX_URL)
    page_title = _("Update Group")

    @memoized.memoized_method
    def get_object(self):
        try:
            return api.keystone.group_get(self.request,
                                          self.kwargs['group_id'])
        except Exception:
            redirect = reverse(constants.GROUPS_INDEX_URL)
            exceptions.handle(self.request,
                              _('Unable to update group.'),
                              redirect=redirect)

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        args = (self.get_object().id,)
        context['submit_url'] = reverse(self.submit_url, args=args)
        return context

    def get_initial(self):
        group = self.get_object()
        return {'group_id': group.id,
                'name': group.name,
                'description': group.description}


class GroupManageMixin(object):
    @memoized.memoized_method
    def _get_group(self):
        group_id = self.kwargs['group_id']
        return api.keystone.group_get(self.request, group_id)

    @memoized.memoized_method
    def _get_group_members(self):
        group_id = self.kwargs['group_id']
        domain_id = identity.get_domain_id_for_operation(self.request)
        return api.keystone.user_list(self.request, domain=domain_id,
                                      group=group_id)

    @memoized.memoized_method
    def _get_group_non_members(self):
        domain_id = self._get_group().domain_id
        all_users = api.keystone.user_list(self.request,
                                           domain=domain_id)
        group_members = self._get_group_members()
        group_member_ids = [user.id for user in group_members]
        return [u for u in all_users if u.id not in group_member_ids]


class ManageMembersView(GroupManageMixin, tables.DataTableView):
    table_class = project_tables.GroupMembersTable
    template_name = constants.GROUPS_MANAGE_VIEW_TEMPLATE
    page_title = _("Group Management: {{ group.name }}")

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['group'] = self._get_group()
        return context

    def get_data(self):
        group_members = []
        try:
            group_members = self._get_group_members()
        except Exception:
            exceptions.handle(self.request,
                              _('Unable to retrieve group users.'))
        return group_members


class NonMembersView(GroupManageMixin, forms.ModalFormMixin,
                     tables.DataTableView):
    template_name = constants.GROUPS_ADD_MEMBER_VIEW_TEMPLATE
    ajax_template_name = constants.GROUPS_ADD_MEMBER_AJAX_VIEW_TEMPLATE
    table_class = project_tables.GroupNonMembersTable

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['group'] = self._get_group()
        return context

    def get_data(self):
        group_non_members = []
        try:
            group_non_members = self._get_group_non_members()
        except Exception:
            exceptions.handle(self.request,
                              _('Unable to retrieve users.'))
        return group_non_members