Przeglądaj źródła

Cleans up following PEP8 rules

Adrien Panay 7 lat temu
rodzic
commit
f1a8e80c73
1 zmienionych plików z 58 dodań i 64 usunięć
  1. 58 64
      tracim/tracim/controllers/admin/user.py

+ 58 - 64
tracim/tracim/controllers/admin/user.py Wyświetl plik

1
 # -*- coding: utf-8 -*-
1
 # -*- coding: utf-8 -*-
2
-import uuid
3
 import random
2
 import random
4
 
3
 
5
 import pytz
4
 import pytz
6
-from tracim import model  as pm
5
+from tracim import model as pm
7
 
6
 
8
-from sprox.tablebase import TableBase
9
-from sprox.formbase import EditableForm, AddRecordForm
10
-from sprox.fillerbase import TableFiller, EditFormFiller
11
-from tracim.config.app_cfg import CFG
12
-from tw2 import forms as tw2f
13
 import tg
7
 import tg
14
 from tg import predicates
8
 from tg import predicates
15
 from tg import tmpl_context
9
 from tg import tmpl_context
16
 from tg.i18n import ugettext as _
10
 from tg.i18n import ugettext as _
17
 
11
 
18
-from sprox.widgets import PropertyMultipleSelectField
19
-from sprox._compat import unicode_text
20
-
21
-from formencode import Schema
22
-from formencode.validators import FieldsMatch
23
-
24
 from tracim.controllers import TIMRestController
12
 from tracim.controllers import TIMRestController
25
 from tracim.controllers.user import UserWorkspaceRestController
13
 from tracim.controllers.user import UserWorkspaceRestController
26
 
14
 
28
 from tracim.lib import helpers as h
16
 from tracim.lib import helpers as h
29
 from tracim.lib.base import logger
17
 from tracim.lib.base import logger
30
 from tracim.lib.email import get_email_manager
18
 from tracim.lib.email import get_email_manager
31
-from tracim.lib.user import UserApi
32
 from tracim.lib.group import GroupApi
19
 from tracim.lib.group import GroupApi
20
+from tracim.lib.user import UserApi
33
 from tracim.lib.userworkspace import RoleApi
21
 from tracim.lib.userworkspace import RoleApi
34
 from tracim.lib.workspace import WorkspaceApi
22
 from tracim.lib.workspace import WorkspaceApi
35
 
23
 
36
 from tracim.model import DBSession
24
 from tracim.model import DBSession
37
-from tracim.model.auth import Group, User
38
-from tracim.model.serializers import Context, CTX, DictLikeClass
25
+from tracim.model.auth import Group
26
+from tracim.model.serializers import CTX
27
+from tracim.model.serializers import Context
28
+from tracim.model.serializers import DictLikeClass
29
+
39
 
30
 
40
 class UserProfileAdminRestController(TIMRestController):
31
 class UserProfileAdminRestController(TIMRestController):
41
-    """
42
-     CRUD Controller allowing to manage groups of a user
43
-    """
32
+    """CRUD Controller allowing to manage groups of a user."""
44
 
33
 
45
     allow_only = predicates.in_group(Group.TIM_ADMIN_GROUPNAME)
34
     allow_only = predicates.in_group(Group.TIM_ADMIN_GROUPNAME)
46
 
35
 
51
     @property
40
     @property
52
     def allowed_profiles(self):
41
     def allowed_profiles(self):
53
         return [
42
         return [
54
-        UserProfileAdminRestController._ALLOWED_PROFILE_USER,
55
-        UserProfileAdminRestController._ALLOWED_PROFILE_MANAGER,
56
-        UserProfileAdminRestController._ALLOWED_PROFILE_ADMIN
57
-    ]
43
+            UserProfileAdminRestController._ALLOWED_PROFILE_USER,
44
+            UserProfileAdminRestController._ALLOWED_PROFILE_MANAGER,
45
+            UserProfileAdminRestController._ALLOWED_PROFILE_ADMIN,
46
+        ]
58
 
47
 
59
     def _before(self, *args, **kw):
48
     def _before(self, *args, **kw):
60
         """
49
         """
61
-        Instantiate the current workspace in tg.tmpl_context
50
+        Instantiate the current workspace in tg.tmpl_context.
51
+
62
         :param args:
52
         :param args:
63
         :param kw:
53
         :param kw:
64
         :return:
54
         :return:
72
         tg.tmpl_context.user = user
62
         tg.tmpl_context.user = user
73
 
63
 
74
     @tg.expose()
64
     @tg.expose()
75
-    def switch(self, new_role):
65
+    def switch(self, new_role) -> None:
76
         """
66
         """
77
-        :param new_role: value should be 'tracim-user', 'tracim-manager' (allowed to create workspaces) or 'tracim-admin' (admin the whole system)
78
-        :return:
67
+        Switch to the given new role.
68
+
69
+        :param new_role: value should be:
70
+            'tracim-user',
71
+            'tracim-manager' (allowed to create workspaces) or
72
+            'tracim-admin' (admin the whole system)
79
         """
73
         """
80
         return self.put(new_role)
74
         return self.put(new_role)
81
 
75
 
87
 
81
 
88
         group_api = GroupApi(current_user)
82
         group_api = GroupApi(current_user)
89
 
83
 
90
-        if current_user.user_id==user.user_id:
84
+        if current_user.user_id == user.user_id:
91
             tg.flash(_('You can\'t change your own profile'), CST.STATUS_ERROR)
85
             tg.flash(_('You can\'t change your own profile'), CST.STATUS_ERROR)
92
             tg.redirect(self.parent_controller.url())
86
             tg.redirect(self.parent_controller.url())
93
 
87
 
94
-
95
         redirect_url = self.parent_controller.url(skip_id=True)
88
         redirect_url = self.parent_controller.url(skip_id=True)
96
 
89
 
97
         if new_profile not in self.allowed_profiles:
90
         if new_profile not in self.allowed_profiles:
102
         pod_manager_group = group_api.get_one(Group.TIM_MANAGER)
95
         pod_manager_group = group_api.get_one(Group.TIM_MANAGER)
103
         pod_admin_group = group_api.get_one(Group.TIM_ADMIN)
96
         pod_admin_group = group_api.get_one(Group.TIM_ADMIN)
104
 
97
 
105
-        flash_message = _('User updated.') # this is the default value ; should never appear
98
+        # this is the default value ; should never appear
99
+        flash_message = _('User updated.')
106
 
100
 
107
-        if new_profile==UserProfileAdminRestController._ALLOWED_PROFILE_USER:
101
+        if new_profile == UserProfileAdminRestController._ALLOWED_PROFILE_USER:
108
             if pod_user_group not in user.groups:
102
             if pod_user_group not in user.groups:
109
                 user.groups.append(pod_user_group)
103
                 user.groups.append(pod_user_group)
110
 
104
 
120
 
114
 
121
             flash_message = _('User {} is now a basic user').format(user.get_display_name())
115
             flash_message = _('User {} is now a basic user').format(user.get_display_name())
122
 
116
 
123
-        elif new_profile==UserProfileAdminRestController._ALLOWED_PROFILE_MANAGER:
117
+        elif new_profile == UserProfileAdminRestController._ALLOWED_PROFILE_MANAGER:
124
             if pod_user_group not in user.groups:
118
             if pod_user_group not in user.groups:
125
                 user.groups.append(pod_user_group)
119
                 user.groups.append(pod_user_group)
126
             if pod_manager_group not in user.groups:
120
             if pod_manager_group not in user.groups:
133
 
127
 
134
             flash_message = _('User {} can now workspaces').format(user.get_display_name())
128
             flash_message = _('User {} can now workspaces').format(user.get_display_name())
135
 
129
 
136
-
137
-        elif new_profile==UserProfileAdminRestController._ALLOWED_PROFILE_ADMIN:
130
+        elif new_profile == UserProfileAdminRestController._ALLOWED_PROFILE_ADMIN:
138
             if pod_user_group not in user.groups:
131
             if pod_user_group not in user.groups:
139
                 user.groups.append(pod_user_group)
132
                 user.groups.append(pod_user_group)
140
             if pod_manager_group not in user.groups:
133
             if pod_manager_group not in user.groups:
145
             flash_message = _('User {} is now an administrator').format(user.get_display_name())
138
             flash_message = _('User {} is now an administrator').format(user.get_display_name())
146
 
139
 
147
         else:
140
         else:
148
-            logger.error(self, 'Trying to change user {} profile with unexpected profile {}'.format(user.user_id, new_profile))
141
+            error_msg = \
142
+                'Trying to change user {} profile with unexpected profile {}'
143
+            logger.error(self, error_msg.format(user.user_id, new_profile))
149
             tg.flash(_('Unknown profile'), CST.STATUS_ERROR)
144
             tg.flash(_('Unknown profile'), CST.STATUS_ERROR)
150
             tg.redirect(redirect_url)
145
             tg.redirect(redirect_url)
151
 
146
 
163
         pass
158
         pass
164
 
159
 
165
 
160
 
166
-
167
 class UserPasswordAdminRestController(TIMRestController):
161
 class UserPasswordAdminRestController(TIMRestController):
168
-    """
169
-     CRUD Controller allowing to manage password of a given user
170
-    """
162
+    """CRUD Controller allowing to manage password of a given user."""
171
 
163
 
172
-    allow_only = predicates.in_any_group(Group.TIM_MANAGER_GROUPNAME, Group.TIM_ADMIN_GROUPNAME)
164
+    allow_only = predicates.in_any_group(
165
+            Group.TIM_MANAGER_GROUPNAME,
166
+            Group.TIM_ADMIN_GROUPNAME,
167
+        )
173
 
168
 
174
     def _before(self, *args, **kw):
169
     def _before(self, *args, **kw):
175
         """
170
         """
176
-        Instantiate the current workspace in tg.tmpl_context
171
+        Instantiate the current workspace in tg.tmpl_context.
172
+
177
         :param args:
173
         :param args:
178
         :param kw:
174
         :param kw:
179
         :return:
175
         :return:
186
         tg.tmpl_context.user_id = user_id
182
         tg.tmpl_context.user_id = user_id
187
         tg.tmpl_context.user = user
183
         tg.tmpl_context.user = user
188
 
184
 
189
-
190
     @tg.expose('tracim.templates.admin.user_password_edit')
185
     @tg.expose('tracim.templates.admin.user_password_edit')
191
     def edit(self):
186
     def edit(self):
192
         current_user = tmpl_context.current_user
187
         current_user = tmpl_context.current_user
193
         api = UserApi(current_user)
188
         api = UserApi(current_user)
194
         dictified_user = Context(CTX.USER).toDict(tmpl_context.user, 'user')
189
         dictified_user = Context(CTX.USER).toDict(tmpl_context.user, 'user')
195
-        return DictLikeClass(result = dictified_user)
190
+        return DictLikeClass(result=dictified_user)
196
 
191
 
197
     @tg.expose()
192
     @tg.expose()
198
     def put(self, new_password1, new_password2, next_url=''):
193
     def put(self, new_password1, new_password2, next_url=''):
207
             tg.flash(_('Empty password is not allowed.'), CST.STATUS_ERROR)
202
             tg.flash(_('Empty password is not allowed.'), CST.STATUS_ERROR)
208
             tg.redirect(next_url)
203
             tg.redirect(next_url)
209
 
204
 
210
-        if new_password1!=new_password2:
205
+        if new_password1 != new_password2:
211
             tg.flash(_('New passwords do not match.'), CST.STATUS_ERROR)
206
             tg.flash(_('New passwords do not match.'), CST.STATUS_ERROR)
212
             tg.redirect(next_url)
207
             tg.redirect(next_url)
213
 
208
 
223
 
218
 
224
     def _before(self, *args, **kw):
219
     def _before(self, *args, **kw):
225
         """
220
         """
226
-        Instantiate the current workspace in tg.tmpl_context
221
+        Instantiate the current workspace in tg.tmpl_context.
222
+
227
         :param args:
223
         :param args:
228
         :param kw:
224
         :param kw:
229
         :return:
225
         :return:
266
 
262
 
267
 
263
 
268
 class UserRestController(TIMRestController):
264
 class UserRestController(TIMRestController):
269
-    """
270
-     CRUD Controller allowing to manage Users
271
-    """
272
-    allow_only = predicates.in_any_group(Group.TIM_MANAGER_GROUPNAME, Group.TIM_ADMIN_GROUPNAME)
265
+    """CRUD Controller allowing to manage Users."""
266
+
267
+    allow_only = predicates.in_any_group(
268
+            Group.TIM_MANAGER_GROUPNAME,
269
+            Group.TIM_ADMIN_GROUPNAME,
270
+        )
273
 
271
 
274
     password = UserPasswordAdminRestController()
272
     password = UserPasswordAdminRestController()
275
     profile = UserProfileAdminRestController()
273
     profile = UserProfileAdminRestController()
284
     def current_item_id_key_in_context(cls):
282
     def current_item_id_key_in_context(cls):
285
         return 'user_id'
283
         return 'user_id'
286
 
284
 
287
-
288
     @tg.require(predicates.in_group(Group.TIM_MANAGER_GROUPNAME))
285
     @tg.require(predicates.in_group(Group.TIM_MANAGER_GROUPNAME))
289
     @tg.expose('tracim.templates.admin.user_getall')
286
     @tg.expose('tracim.templates.admin.user_getall')
290
     def get_all(self, *args, **kw):
287
     def get_all(self, *args, **kw):
297
         fake_api = Context(CTX.USERS).toDict({'current_user': current_user_content})
294
         fake_api = Context(CTX.USERS).toDict({'current_user': current_user_content})
298
 
295
 
299
         dictified_users = Context(CTX.USERS).toDict(users, 'users', 'user_nb')
296
         dictified_users = Context(CTX.USERS).toDict(users, 'users', 'user_nb')
300
-        return DictLikeClass(result = dictified_users, fake_api=fake_api)
297
+        return DictLikeClass(result=dictified_users, fake_api=fake_api)
301
 
298
 
302
     @tg.require(predicates.in_group(Group.TIM_MANAGER_GROUPNAME))
299
     @tg.require(predicates.in_group(Group.TIM_MANAGER_GROUPNAME))
303
     @tg.expose()
300
     @tg.expose()
359
     @classmethod
356
     @classmethod
360
     def generate_password(
357
     def generate_password(
361
             cls,
358
             cls,
362
-            password_length = PASSWORD_LENGTH,
363
-            password_chars = PASSWORD_CHARACTERS
364
-            ):
365
-
359
+            password_length=PASSWORD_LENGTH,
360
+            password_chars=PASSWORD_CHARACTERS,
361
+    ):
366
         # character list that will be contained into the password
362
         # character list that will be contained into the password
367
         char_list = []
363
         char_list = []
368
 
364
 
369
-        for j in range(0, password_length):
365
+        for _unused in range(password_length):
370
             # This puts a random char from the list above inside
366
             # This puts a random char from the list above inside
371
             # the list of chars and then merges them into a String
367
             # the list of chars and then merges them into a String
372
             char_list.append(random.choice(password_chars))
368
             char_list.append(random.choice(password_chars))
376
     @tg.expose('tracim.templates.admin.user_getone')
372
     @tg.expose('tracim.templates.admin.user_getone')
377
     def get_one(self, user_id):
373
     def get_one(self, user_id):
378
         current_user = tmpl_context.current_user
374
         current_user = tmpl_context.current_user
379
-        api = UserApi(current_user )
375
+        api = UserApi(current_user)
380
         # role_api = RoleApi(tg.tmpl_context.current_user)
376
         # role_api = RoleApi(tg.tmpl_context.current_user)
381
         # user_api = UserApi(tg.tmpl_context.current_user)
377
         # user_api = UserApi(tg.tmpl_context.current_user)
382
 
378
 
383
-        user = api.get_one(user_id) # FIXME
379
+        user = api.get_one(user_id)  # FIXME
384
 
380
 
385
         role_api = RoleApi(tg.tmpl_context.current_user)
381
         role_api = RoleApi(tg.tmpl_context.current_user)
386
         role_list = role_api.get_roles_for_select_field()
382
         role_list = role_api.get_roles_for_select_field()
391
                                          role_types=role_list)
387
                                          role_types=role_list)
392
         fake_api = Context(CTX.ADMIN_USER).toDict(fake_api_content)
388
         fake_api = Context(CTX.ADMIN_USER).toDict(fake_api_content)
393
 
389
 
394
-        return DictLikeClass(result = dictified_user, fake_api=fake_api)
395
-
390
+        return DictLikeClass(result=dictified_user, fake_api=fake_api)
396
 
391
 
397
     @tg.expose('tracim.templates.admin.user_edit')
392
     @tg.expose('tracim.templates.admin.user_edit')
398
     def edit(self, id):
393
     def edit(self, id):
420
             tg.redirect(next_url)
415
             tg.redirect(next_url)
421
         tg.redirect(self.url())
416
         tg.redirect(self.url())
422
 
417
 
423
-
424
     @tg.require(predicates.in_group(Group.TIM_ADMIN_GROUPNAME))
418
     @tg.require(predicates.in_group(Group.TIM_ADMIN_GROUPNAME))
425
     @tg.expose()
419
     @tg.expose()
426
     def enable(self, id, next_url=None):
420
     def enable(self, id, next_url=None):
432
         api.save(user)
426
         api.save(user)
433
 
427
 
434
         tg.flash(_('User {} enabled.').format(user.get_display_name()), CST.STATUS_OK)
428
         tg.flash(_('User {} enabled.').format(user.get_display_name()), CST.STATUS_OK)
435
-        if next_url=='user':
429
+        if next_url == 'user':
436
             tg.redirect(self.url(id=user.user_id))
430
             tg.redirect(self.url(id=user.user_id))
437
         tg.redirect(self.url())
431
         tg.redirect(self.url())
438
 
432
 
443
         current_user = tmpl_context.current_user
437
         current_user = tmpl_context.current_user
444
         api = UserApi(current_user)
438
         api = UserApi(current_user)
445
 
439
 
446
-        if current_user.user_id==id:
440
+        if current_user.user_id == id:
447
             tg.flash(_('You can\'t de-activate your own account'), CST.STATUS_ERROR)
441
             tg.flash(_('You can\'t de-activate your own account'), CST.STATUS_ERROR)
448
         else:
442
         else:
449
             user = api.get_one(id)
443
             user = api.get_one(id)
451
             api.save(user)
445
             api.save(user)
452
             tg.flash(_('User {} disabled').format(user.get_display_name()), CST.STATUS_OK)
446
             tg.flash(_('User {} disabled').format(user.get_display_name()), CST.STATUS_OK)
453
 
447
 
454
-        if next_url=='user':
448
+        if next_url == 'user':
455
             tg.redirect(self.url(id=user.user_id))
449
             tg.redirect(self.url(id=user.user_id))
456
         tg.redirect(self.url())
450
         tg.redirect(self.url())