8
8
9
9
from collections import OrderedDict
10
10
11
- from django import forms
12
11
from django .conf .urls import url
13
12
from django .contrib import admin
14
13
from django .contrib .admin import helpers
15
- from django .contrib .admin .widgets import ForeignKeyRawIdWidget
16
14
from django .contrib .auth import get_user_model
17
15
from django .contrib .auth .models import Group
18
16
from django .forms .forms import NON_FIELD_ERRORS
23
21
from django .urls import reverse
24
22
from django .utils .translation import ugettext_lazy as _
25
23
from mptt .exceptions import InvalidMove
26
- from mptt .forms import TreeNodeChoiceField
27
24
28
25
from machina .core .db .models import get_model
29
26
from machina .core .loading import get_class
36
33
GroupForumPermission = get_model ('forum_permission' , 'GroupForumPermission' )
37
34
UserForumPermission = get_model ('forum_permission' , 'UserForumPermission' )
38
35
39
- PermissionConfig = get_class ('forum_permission.defaults' , 'PermissionConfig' )
36
+ PermissionsForm = get_class ('forum.forms' , 'PermissionsForm' )
37
+ PickForumForm = get_class ('forum.forms' , 'PickForumForm' )
38
+ PickGroupForm = get_class ('forum.forms' , 'PickGroupForm' )
39
+ PickUserForm = get_class ('forum.forms' , 'PickUserForm' )
40
40
41
- PERM_GRANTED = 'granted'
42
- PERM_NOT_GRANTED = 'not-granted'
43
- PERM_NOT_SET = 'not-set'
41
+ PermissionConfig = get_class ('forum_permission.defaults' , 'PermissionConfig' )
44
42
45
43
46
44
class ForumAdmin (admin .ModelAdmin ):
@@ -327,11 +325,11 @@ def _get_permissions_form(self, request, permission_model, filter_kwargs):
327
325
permissions_dict = OrderedDict ()
328
326
for p in editable_permissions :
329
327
if p .codename in granted_permissions :
330
- perm_state = PERM_GRANTED
328
+ perm_state = PermissionsForm . PERM_GRANTED
331
329
elif p .codename in non_granted_permissions :
332
- perm_state = PERM_NOT_GRANTED
330
+ perm_state = PermissionsForm . PERM_NOT_GRANTED
333
331
else :
334
- perm_state = PERM_NOT_SET
332
+ perm_state = PermissionsForm . PERM_NOT_SET
335
333
permissions_dict [p .codename ] = (p , perm_state )
336
334
337
335
if request .method == 'POST' :
@@ -343,17 +341,17 @@ def _get_permissions_form(self, request, permission_model, filter_kwargs):
343
341
permission = permissions_dict [codename ][0 ], ** filter_kwargs
344
342
)
345
343
except permission_model .DoesNotExist :
346
- if value == PERM_NOT_SET :
344
+ if value == PermissionsForm . PERM_NOT_SET :
347
345
continue
348
346
perm = permission_model .objects .create (
349
347
permission = permissions_dict [codename ][0 ], ** filter_kwargs
350
348
)
351
349
352
- if value == PERM_NOT_SET :
350
+ if value == PermissionsForm . PERM_NOT_SET :
353
351
perm .delete ()
354
352
continue
355
353
356
- perm .has_perm = (value == PERM_GRANTED )
354
+ perm .has_perm = (value == PermissionsForm . PERM_GRANTED )
357
355
perm .save ()
358
356
359
357
self .message_user (request , _ ('Permissions successfully applied' ))
@@ -395,85 +393,4 @@ def _copy_forum_permissions(self, forum_from, forum_to):
395
393
new_perm .save ()
396
394
397
395
398
- class PickUserForm (forms .Form ):
399
- """ Form allowing to pick a user to edit their permissions. """
400
-
401
- user = UserForumPermission ._meta .get_field ('user' ).formfield ()
402
- anonymous_user = forms .BooleanField (
403
- label = _ ('Anonymous' ),
404
- initial = False ,
405
- help_text = _ (
406
- 'Please select this option if you want to edit the permissions of the anonymous user'
407
- ),
408
- )
409
-
410
- def __init__ (self , * args , ** kwargs ):
411
- admin_site = kwargs .pop ('admin_site' )
412
- super ().__init__ (* args , ** kwargs )
413
-
414
- self .fields ['user' ].required = False
415
- self .fields ['user' ].widget = ForeignKeyRawIdWidget (
416
- UserForumPermission ._meta .get_field ('user' ).remote_field , admin_site ,
417
- )
418
-
419
- self .fields ['anonymous_user' ].required = False
420
-
421
- def clean (self ):
422
- cleaned_data = super ().clean ()
423
- user = cleaned_data .get ('user' , None )
424
- anonymous_user = cleaned_data .get ('anonymous_user' , None )
425
- if user and anonymous_user :
426
- self ._errors [NON_FIELD_ERRORS ] = self .error_class (
427
- [_ ('Choose either a user ID or select the anonymous user' ), ],
428
- )
429
- return cleaned_data
430
-
431
-
432
- class PickGroupForm (forms .Form ):
433
- """ Form allowing to pick a group to edit its permissions. """
434
-
435
- group = GroupForumPermission ._meta .get_field ('group' ).formfield ()
436
-
437
- def __init__ (self , * args , ** kwargs ):
438
- admin_site = kwargs .pop ('admin_site' )
439
- super ().__init__ (* args , ** kwargs )
440
-
441
- self .fields ['group' ].required = False
442
- self .fields ['group' ].widget = ForeignKeyRawIdWidget (
443
- GroupForumPermission ._meta .get_field ('group' ).remote_field , admin_site ,
444
- )
445
-
446
-
447
- class PickForumForm (forms .Form ):
448
- """ Form allowing to pick a specific forum. """
449
-
450
- forum = TreeNodeChoiceField (queryset = Forum .objects .all (), required = False )
451
-
452
-
453
- class PermissionsForm (forms .Form ):
454
- """ Form allowing to edit permissions. """
455
-
456
- def __init__ (self , * args , ** kwargs ):
457
- self .permissions_dict = kwargs .pop ('permissions_dict' , {})
458
- super ().__init__ (* args , ** kwargs )
459
-
460
- # Initializes permission fields
461
- f_choices = (
462
- (PERM_NOT_SET , _ ('Not set' )),
463
- (PERM_GRANTED , _ ('Granted' )),
464
- (PERM_NOT_GRANTED , _ ('Not granted' )),
465
- )
466
- for scope in PermissionConfig .scopes :
467
- codenames = [
468
- x ['fields' ]['codename' ] for x in PermissionConfig .permissions if x ['scope' ] == scope
469
- ]
470
- permissions = filter (lambda v : v [0 ] in codenames , self .permissions_dict .items ())
471
- for codename , p in permissions :
472
- self .fields [codename ] = forms .ChoiceField (
473
- label = p [0 ].name , choices = f_choices , required = False , widget = forms .RadioSelect ,
474
- )
475
- self .fields [codename ].initial = p [1 ]
476
- self .fields [codename ].scope = scope
477
-
478
-
479
396
admin .site .register (Forum , ForumAdmin )
0 commit comments