Browse Source

Cleans up following PEP8 rules

Adrien Panay 7 years ago
parent
commit
f1a8e80c73
1 changed files with 58 additions and 64 deletions
  1. 58 64
      tracim/tracim/controllers/admin/user.py

+ 58 - 64
tracim/tracim/controllers/admin/user.py View File

@@ -1,26 +1,14 @@
1 1
 # -*- coding: utf-8 -*-
2
-import uuid
3 2
 import random
4 3
 
5 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 7
 import tg
14 8
 from tg import predicates
15 9
 from tg import tmpl_context
16 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 12
 from tracim.controllers import TIMRestController
25 13
 from tracim.controllers.user import UserWorkspaceRestController
26 14
 
@@ -28,19 +16,20 @@ from tracim.lib import CST
28 16
 from tracim.lib import helpers as h
29 17
 from tracim.lib.base import logger
30 18
 from tracim.lib.email import get_email_manager
31
-from tracim.lib.user import UserApi
32 19
 from tracim.lib.group import GroupApi
20
+from tracim.lib.user import UserApi
33 21
 from tracim.lib.userworkspace import RoleApi
34 22
 from tracim.lib.workspace import WorkspaceApi
35 23
 
36 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 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 34
     allow_only = predicates.in_group(Group.TIM_ADMIN_GROUPNAME)
46 35
 
@@ -51,14 +40,15 @@ class UserProfileAdminRestController(TIMRestController):
51 40
     @property
52 41
     def allowed_profiles(self):
53 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 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 52
         :param args:
63 53
         :param kw:
64 54
         :return:
@@ -72,10 +62,14 @@ class UserProfileAdminRestController(TIMRestController):
72 62
         tg.tmpl_context.user = user
73 63
 
74 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 74
         return self.put(new_role)
81 75
 
@@ -87,11 +81,10 @@ class UserProfileAdminRestController(TIMRestController):
87 81
 
88 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 85
             tg.flash(_('You can\'t change your own profile'), CST.STATUS_ERROR)
92 86
             tg.redirect(self.parent_controller.url())
93 87
 
94
-
95 88
         redirect_url = self.parent_controller.url(skip_id=True)
96 89
 
97 90
         if new_profile not in self.allowed_profiles:
@@ -102,9 +95,10 @@ class UserProfileAdminRestController(TIMRestController):
102 95
         pod_manager_group = group_api.get_one(Group.TIM_MANAGER)
103 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 102
             if pod_user_group not in user.groups:
109 103
                 user.groups.append(pod_user_group)
110 104
 
@@ -120,7 +114,7 @@ class UserProfileAdminRestController(TIMRestController):
120 114
 
121 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 118
             if pod_user_group not in user.groups:
125 119
                 user.groups.append(pod_user_group)
126 120
             if pod_manager_group not in user.groups:
@@ -133,8 +127,7 @@ class UserProfileAdminRestController(TIMRestController):
133 127
 
134 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 131
             if pod_user_group not in user.groups:
139 132
                 user.groups.append(pod_user_group)
140 133
             if pod_manager_group not in user.groups:
@@ -145,7 +138,9 @@ class UserProfileAdminRestController(TIMRestController):
145 138
             flash_message = _('User {} is now an administrator').format(user.get_display_name())
146 139
 
147 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 144
             tg.flash(_('Unknown profile'), CST.STATUS_ERROR)
150 145
             tg.redirect(redirect_url)
151 146
 
@@ -163,17 +158,18 @@ class UserProfileAdminRestController(TIMRestController):
163 158
         pass
164 159
 
165 160
 
166
-
167 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 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 173
         :param args:
178 174
         :param kw:
179 175
         :return:
@@ -186,13 +182,12 @@ class UserPasswordAdminRestController(TIMRestController):
186 182
         tg.tmpl_context.user_id = user_id
187 183
         tg.tmpl_context.user = user
188 184
 
189
-
190 185
     @tg.expose('tracim.templates.admin.user_password_edit')
191 186
     def edit(self):
192 187
         current_user = tmpl_context.current_user
193 188
         api = UserApi(current_user)
194 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 192
     @tg.expose()
198 193
     def put(self, new_password1, new_password2, next_url=''):
@@ -207,7 +202,7 @@ class UserPasswordAdminRestController(TIMRestController):
207 202
             tg.flash(_('Empty password is not allowed.'), CST.STATUS_ERROR)
208 203
             tg.redirect(next_url)
209 204
 
210
-        if new_password1!=new_password2:
205
+        if new_password1 != new_password2:
211 206
             tg.flash(_('New passwords do not match.'), CST.STATUS_ERROR)
212 207
             tg.redirect(next_url)
213 208
 
@@ -223,7 +218,8 @@ class UserWorkspaceRestController(TIMRestController):
223 218
 
224 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 223
         :param args:
228 224
         :param kw:
229 225
         :return:
@@ -266,10 +262,12 @@ class UserWorkspaceRestController(TIMRestController):
266 262
 
267 263
 
268 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 272
     password = UserPasswordAdminRestController()
275 273
     profile = UserProfileAdminRestController()
@@ -284,7 +282,6 @@ class UserRestController(TIMRestController):
284 282
     def current_item_id_key_in_context(cls):
285 283
         return 'user_id'
286 284
 
287
-
288 285
     @tg.require(predicates.in_group(Group.TIM_MANAGER_GROUPNAME))
289 286
     @tg.expose('tracim.templates.admin.user_getall')
290 287
     def get_all(self, *args, **kw):
@@ -297,7 +294,7 @@ class UserRestController(TIMRestController):
297 294
         fake_api = Context(CTX.USERS).toDict({'current_user': current_user_content})
298 295
 
299 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 299
     @tg.require(predicates.in_group(Group.TIM_MANAGER_GROUPNAME))
303 300
     @tg.expose()
@@ -359,14 +356,13 @@ class UserRestController(TIMRestController):
359 356
     @classmethod
360 357
     def generate_password(
361 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 362
         # character list that will be contained into the password
367 363
         char_list = []
368 364
 
369
-        for j in range(0, password_length):
365
+        for _unused in range(password_length):
370 366
             # This puts a random char from the list above inside
371 367
             # the list of chars and then merges them into a String
372 368
             char_list.append(random.choice(password_chars))
@@ -376,11 +372,11 @@ class UserRestController(TIMRestController):
376 372
     @tg.expose('tracim.templates.admin.user_getone')
377 373
     def get_one(self, user_id):
378 374
         current_user = tmpl_context.current_user
379
-        api = UserApi(current_user )
375
+        api = UserApi(current_user)
380 376
         # role_api = RoleApi(tg.tmpl_context.current_user)
381 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 381
         role_api = RoleApi(tg.tmpl_context.current_user)
386 382
         role_list = role_api.get_roles_for_select_field()
@@ -391,8 +387,7 @@ class UserRestController(TIMRestController):
391 387
                                          role_types=role_list)
392 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 392
     @tg.expose('tracim.templates.admin.user_edit')
398 393
     def edit(self, id):
@@ -420,7 +415,6 @@ class UserRestController(TIMRestController):
420 415
             tg.redirect(next_url)
421 416
         tg.redirect(self.url())
422 417
 
423
-
424 418
     @tg.require(predicates.in_group(Group.TIM_ADMIN_GROUPNAME))
425 419
     @tg.expose()
426 420
     def enable(self, id, next_url=None):
@@ -432,7 +426,7 @@ class UserRestController(TIMRestController):
432 426
         api.save(user)
433 427
 
434 428
         tg.flash(_('User {} enabled.').format(user.get_display_name()), CST.STATUS_OK)
435
-        if next_url=='user':
429
+        if next_url == 'user':
436 430
             tg.redirect(self.url(id=user.user_id))
437 431
         tg.redirect(self.url())
438 432
 
@@ -443,7 +437,7 @@ class UserRestController(TIMRestController):
443 437
         current_user = tmpl_context.current_user
444 438
         api = UserApi(current_user)
445 439
 
446
-        if current_user.user_id==id:
440
+        if current_user.user_id == id:
447 441
             tg.flash(_('You can\'t de-activate your own account'), CST.STATUS_ERROR)
448 442
         else:
449 443
             user = api.get_one(id)
@@ -451,6 +445,6 @@ class UserRestController(TIMRestController):
451 445
             api.save(user)
452 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 449
             tg.redirect(self.url(id=user.user_id))
456 450
         tg.redirect(self.url())