Skip to content

Commit

Permalink
Remove local/global concept from permission model
Browse files Browse the repository at this point in the history
  • Loading branch information
ellmetha committed Mar 20, 2019
1 parent 5239672 commit 13d18a9
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 80 deletions.
4 changes: 1 addition & 3 deletions machina/apps/forum/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,8 @@ def editpermissions_group_view(self, request, group_id, forum_id=None):

def _get_permissions_form(self, request, permission_model, filter_kwargs):
# Fetch the permissions
forum = filter_kwargs.get('forum', None)
perm_type_filter = {'is_local': True} if forum else {'is_global': True}
editable_permissions = sorted(
ForumPermission.objects.filter(**perm_type_filter), key=lambda p: p.name,
ForumPermission.objects.all(), key=lambda p: p.name,
)
granted_permissions = (
permission_model.objects.filter(
Expand Down
2 changes: 1 addition & 1 deletion machina/apps/forum/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def __init__(self, *args, **kwargs):
)
for scope in PermissionConfig.scopes:
codenames = [
x['fields']['codename'] for x in PermissionConfig.permissions if x['scope'] == scope
x['codename'] for x in PermissionConfig.permissions if x['scope'] == scope
]
permissions = filter(lambda v: v[0] in codenames, self.permissions_dict.items())
for codename, p in permissions:
Expand Down
29 changes: 0 additions & 29 deletions machina/apps/forum_permission/abstract_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,6 @@ class AbstractForumPermission(models.Model):
max_length=150, verbose_name=_('Permission codename'), unique=True, db_index=True,
)

is_global = models.BooleanField(
verbose_name=_('Global permission'),
help_text=_('This permission can be granted globally to all the forums'),
default=False, db_index=True,
)
is_local = models.BooleanField(
verbose_name=_('Local permission'),
help_text=_('This permission can be granted individually for each forum'),
default=True, db_index=True,
)

class Meta:
abstract = True
app_label = 'forum_permission'
Expand All @@ -53,12 +42,6 @@ class Meta:
def __str__(self):
return '{} - {}'.format(self.codename, self.name)

def clean(self):
""" Validates the current instance. """
super().clean()
if not self.is_global and not self.is_local:
raise ValidationError(_('A forum permission should be at least either global or local'))

@cached_property
def name(self):
""" Returns the name of the considered permission. """
Expand All @@ -84,18 +67,6 @@ class BaseAuthForumPermission(models.Model):
class Meta:
abstract = True

def clean(self):
""" Validates the current instance. """
super().clean()
if self.forum is None and not self.permission.is_global:
raise ValidationError(
_(
'The following permission cannot be granted globally: {}'.format(
self.permission,
),
),
)


class AbstractUserForumPermission(BaseAuthForumPermission):
""" Represents a per-user forum object permission. """
Expand Down
3 changes: 1 addition & 2 deletions machina/apps/forum_permission/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ class ForumPermissionAdmin(admin.ModelAdmin):
""" The Forum Permission model admin. """

search_fields = ('codename', )
list_display = ('name', 'codename', 'is_global', 'is_local', )
list_editables = ('is_global', 'is_local', )
list_display = ('name', 'codename', )


class GroupForumPermissionAdmin(admin.ModelAdmin):
Expand Down
42 changes: 20 additions & 22 deletions machina/apps/forum_permission/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,107 +23,105 @@ class PermissionConfig:
permissions = [
# Forums
{
'fields': {'codename': 'can_see_forum', 'is_local': True, 'is_global': True, },
'codename': 'can_see_forum',
'label': _('Can see forum'),
'scope': 'forum',
},
{
'fields': {'codename': 'can_read_forum', 'is_local': True, 'is_global': True, },
'codename': 'can_read_forum',
'label': _('Can read forum'),
'scope': 'forum',
},

# Topics & posts
{
'fields': {'codename': 'can_start_new_topics', 'is_local': True, 'is_global': True, },
'codename': 'can_start_new_topics',
'label': _('Can start new topics'),
'scope': 'conversation',
},
{
'fields': {'codename': 'can_reply_to_topics', 'is_local': True, 'is_global': True, },
'codename': 'can_reply_to_topics',
'label': _('Can reply to topics'),
'scope': 'conversation',
},
{
'fields': {'codename': 'can_post_announcements', 'is_local': True, 'is_global': True, },
'codename': 'can_post_announcements',
'label': _('Can post announcements'),
'scope': 'conversation',
},
{
'fields': {'codename': 'can_post_stickies', 'is_local': True, 'is_global': True, },
'codename': 'can_post_stickies',
'label': _('Can post stickies'),
'scope': 'conversation',
},
{
'fields': {'codename': 'can_delete_own_posts', 'is_local': True, 'is_global': True, },
'codename': 'can_delete_own_posts',
'label': _('Can delete own posts'),
'scope': 'conversation',
},
{
'fields': {'codename': 'can_edit_own_posts', 'is_local': True, 'is_global': True, },
'codename': 'can_edit_own_posts',
'label': _('Can edit own posts'),
'scope': 'conversation',
},
{
'fields': {
'codename': 'can_post_without_approval', 'is_local': True, 'is_global': True, },
'codename': 'can_post_without_approval',
'label': _('Can post without approval'),
'scope': 'conversation',
},

# Polls
{
'fields': {'codename': 'can_create_polls', 'is_local': True, 'is_global': True, },
'codename': 'can_create_polls',
'label': _('Can create polls'),
'scope': 'polls',
},
{
'fields': {'codename': 'can_vote_in_polls', 'is_local': True, 'is_global': True, },
'codename': 'can_vote_in_polls',
'label': _('Can vote in polls'),
'scope': 'polls',
},

# Attachments
{
'fields': {'codename': 'can_attach_file', 'is_local': True, 'is_global': True, },
'codename': 'can_attach_file',
'label': _('Can attach file'),
'scope': 'attachments',
},
{
'fields': {'codename': 'can_download_file', 'is_local': True, 'is_global': True, },
'codename': 'can_download_file',
'label': _('Can download file'),
'scope': 'attachments',
},

# Moderation
{
'fields': {'codename': 'can_lock_topics', 'is_local': True, 'is_global': False, },
'codename': 'can_lock_topics',
'label': _('Can lock topics'),
'scope': 'moderation',
},
{
'fields': {'codename': 'can_move_topics', 'is_local': True, 'is_global': False, },
'codename': 'can_move_topics',
'label': _('Can move topics'),
'scope': 'moderation',
},
{
'fields': {'codename': 'can_edit_posts', 'is_local': True, 'is_global': False, },
'codename': 'can_edit_posts',
'label': _('Can edit posts'),
'scope': 'moderation',
},
{
'fields': {'codename': 'can_delete_posts', 'is_local': True, 'is_global': False, },
'codename': 'can_delete_posts',
'label': _('Can delete posts'),
'scope': 'moderation',
},
{
'fields': {'codename': 'can_approve_posts', 'is_local': True, 'is_global': False, },
'codename': 'can_approve_posts',
'label': _('Can approve posts'),
'scope': 'moderation',
},
{
'fields': {
'codename': 'can_reply_to_locked_topics', 'is_local': True, 'is_global': False, },
'codename': 'can_reply_to_locked_topics',
'label': _('Can add posts in locked topics'),
'scope': 'moderation',
},
Expand All @@ -140,5 +138,5 @@ def get(self, key, default=None):
@property
def _permissions_dict(self):
if not hasattr(self, '_permissions_per_codename'):
self._permissions_per_codename = {p['fields']['codename']: p for p in self.permissions}
self._permissions_per_codename = {p['codename']: p for p in self.permissions}
return self._permissions_per_codename
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 2.1.7 on 2019-03-20 03:40

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('forum_permission', '0003_remove_forumpermission_name'),
]

operations = [
migrations.RemoveField(
model_name='forumpermission',
name='is_global',
),
migrations.RemoveField(
model_name='forumpermission',
name='is_local',
),
]
6 changes: 1 addition & 5 deletions machina/apps/forum_permission/receivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@
def create_permissions():
""" Creates all the permissions from the permission configuration. """
for config in PermissionConfig.permissions:
try:
gp = ForumPermission.objects.get(codename=config['fields']['codename'])
except ForumPermission.DoesNotExist:
gp = ForumPermission(**config['fields'])
gp.save()
ForumPermission.objects.get_or_create(codename=config['codename'])


@receiver(post_migrate)
Expand Down
19 changes: 1 addition & 18 deletions tests/unit/apps/forum_permission/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,14 @@
UserForumPermission = get_model('forum_permission', 'UserForumPermission')


@pytest.mark.django_db
class TestForumPermission(object):
def test_cannot_be_cleaned_without_local_or_global_flag(self):
# Run & check
with pytest.raises(ValidationError):
perm = ForumPermissionFactory.build(is_local=False, is_global=False)
perm.clean()


@pytest.mark.django_db
class TestUserForumPermission(object):
def test_cannot_target_an_anonymous_user_and_a_registered_user(self):
# Setup
user = UserFactory.create()
# Run & check
with pytest.raises(ValidationError):
perm = ForumPermissionFactory.create(is_local=True, is_global=True)
perm = ForumPermissionFactory.create()
user_perm = UserForumPermissionFactory.build(
permission=perm, user=user, anonymous_user=True)
user_perm.clean()

def test_cannot_be_saved_without_forum_if_the_permission_is_not_global(self):
# Run & check
with pytest.raises(ValidationError):
perm = ForumPermissionFactory.create(is_global=False)
user_perm = UserForumPermissionFactory.build(
permission=perm)
user_perm.clean()

0 comments on commit 13d18a9

Please sign in to comment.