From b19ec084b6dae54dffd25d9e957e87fd6a00c54d Mon Sep 17 00:00:00 2001 From: Skia Date: Thu, 26 Nov 2015 17:40:31 +0100 Subject: [PATCH] Basic group view and permissions management on the pages --- core/admin.py | 3 +- core/fixtures/groups.json | 2 +- core/migrations/0007_auto_20151126_1613.py | 37 ++++++++++++++++++++++ core/models.py | 9 +++++- core/templates/core/group_edit.html | 16 ++++++++++ core/templates/core/group_list.html | 15 +++++++++ core/urls.py | 3 ++ core/views/__init__.py | 1 + core/views/forms.py | 12 ++++++- core/views/group.py | 19 +++++++++++ core/views/page.py | 6 ++++ 11 files changed, 119 insertions(+), 4 deletions(-) create mode 100644 core/migrations/0007_auto_20151126_1613.py create mode 100644 core/templates/core/group_edit.html create mode 100644 core/templates/core/group_list.html create mode 100644 core/views/group.py diff --git a/core/admin.py b/core/admin.py index 5a9d70ec..699e9e85 100644 --- a/core/admin.py +++ b/core/admin.py @@ -1,7 +1,8 @@ from django.contrib import admin -from .models import User, Page +from .models import User, Page, Group admin.site.register(User) +admin.site.register(Group) admin.site.register(Page) diff --git a/core/fixtures/groups.json b/core/fixtures/groups.json index 8ac77e46..282d67ad 100644 --- a/core/fixtures/groups.json +++ b/core/fixtures/groups.json @@ -1 +1 @@ -[{"pk": 1, "model": "auth.group", "fields": {"name": "root", "permissions": [1, 2, 3, 7, 8, 9, 4, 5, 6, 10, 11, 12, 19, 20, 21, 16, 17, 18, 13, 14, 15]}}] \ No newline at end of file +[{"fields": {"permissions": [1, 2, 3, 7, 8, 9, 4, 5, 6, 10, 11, 12, 19, 20, 21, 22, 25, 23, 24, 16, 17, 18, 13, 14, 15]}, "pk": 1, "model": "core.group"}] \ No newline at end of file diff --git a/core/migrations/0007_auto_20151126_1613.py b/core/migrations/0007_auto_20151126_1613.py new file mode 100644 index 00000000..f2a6058e --- /dev/null +++ b/core/migrations/0007_auto_20151126_1613.py @@ -0,0 +1,37 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('auth', '0006_require_contenttypes_0002'), + ('core', '0006_auto_20151125_0855'), + ] + + operations = [ + migrations.CreateModel( + name='Group', + fields=[ + ('group_ptr', models.OneToOneField(auto_created=True, to='auth.Group', primary_key=True, serialize=False, parent_link=True)), + ], + bases=('auth.group',), + ), + migrations.AlterField( + model_name='page', + name='edit_group', + field=models.ForeignKey(default=1, to='core.Group', related_name='editable_pages'), + ), + migrations.AlterField( + model_name='page', + name='owner_group', + field=models.ForeignKey(default=1, to='core.Group', related_name='owned_pages'), + ), + migrations.AlterField( + model_name='page', + name='view_group', + field=models.ForeignKey(default=1, to='core.Group', related_name='viewable_pages'), + ), + ] diff --git a/core/models.py b/core/models.py index 373fbfb4..059cdbb3 100644 --- a/core/models.py +++ b/core/models.py @@ -1,5 +1,5 @@ from django.db import models -from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, UserManager, Group +from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, UserManager, Group as AuthGroup from django.utils.translation import ugettext_lazy as _ from django.utils import timezone from django.core import validators @@ -125,6 +125,12 @@ class User(AbstractBaseUser, PermissionsMixin): self.username = user_name return user_name +class Group(AuthGroup): + def get_absolute_url(self): + """ + This is needed for black magic powered UpdateView's children + """ + return reverse('core:group_edit', kwargs={'group_id': self.pk}) class Page(models.Model): """ @@ -227,3 +233,4 @@ class Page(models.Model): def get_display_name(self): return self.get_full_name() + diff --git a/core/templates/core/group_edit.html b/core/templates/core/group_edit.html new file mode 100644 index 00000000..570bf419 --- /dev/null +++ b/core/templates/core/group_edit.html @@ -0,0 +1,16 @@ +{% extends "core/base.html" %} + +{% block content %} +

Back to list

+

Edit group

+
+ {% csrf_token %} + {{ form.as_p }} +

+
+{% endblock %} + + + + + diff --git a/core/templates/core/group_list.html b/core/templates/core/group_list.html new file mode 100644 index 00000000..c0c9f67d --- /dev/null +++ b/core/templates/core/group_list.html @@ -0,0 +1,15 @@ +{% extends "core/base.html" %} + +{% block title %} +Group list +{% endblock %} + +{% block content %} +

Group list

+ +{% endblock %} + diff --git a/core/urls.py b/core/urls.py index d3cddfb9..460640b6 100644 --- a/core/urls.py +++ b/core/urls.py @@ -17,6 +17,9 @@ urlpatterns = [ url(r'^reset/done/$', password_reset_complete, name='password_reset_complete'), url(r'^register$', register, name='register'), + url(r'^group/$', GroupListView.as_view(), name='group_list'), + url(r'^group/(?P[0-9]+)/$', GroupEditView.as_view(), name='group_edit'), + url(r'^user/$', UserListView.as_view(), name='user_list'), url(r'^user/(?P[0-9]+)/$', UserView.as_view(), name='user_profile'), url(r'^user/(?P[0-9]+)/edit$', UserUpdateProfileView.as_view(), name='user_edit'), diff --git a/core/views/__init__.py b/core/views/__init__.py index 99f66a98..364ef9cc 100644 --- a/core/views/__init__.py +++ b/core/views/__init__.py @@ -1,3 +1,4 @@ from .user import * from .page import * from .site import * +from .group import * diff --git a/core/views/forms.py b/core/views/forms.py index 7df4c019..29911193 100644 --- a/core/views/forms.py +++ b/core/views/forms.py @@ -4,7 +4,7 @@ from django.contrib.auth import logout, login, authenticate from django.forms import CheckboxSelectMultiple import logging -from core.models import User, Page +from core.models import User, Page, Group class RegisteringForm(UserCreationForm): error_css_class = 'error' @@ -33,3 +33,13 @@ class UserGroupsForm(forms.ModelForm): 'user_permissions': CheckboxSelectMultiple, } +class GroupEditForm(forms.ModelForm): + error_css_class = 'error' + required_css_class = 'required' + class Meta: + model = Group + fields = ['name', 'permissions',] + widgets = { + 'permissions': CheckboxSelectMultiple, + } + diff --git a/core/views/group.py b/core/views/group.py new file mode 100644 index 00000000..1a83d63a --- /dev/null +++ b/core/views/group.py @@ -0,0 +1,19 @@ +from django.views.generic.edit import UpdateView +from django.views.generic import ListView + +from core.models import Group +from core.views.forms import GroupEditForm + +class GroupListView(ListView): + """ + Displays the group list + """ + model = Group + template_name = "core/group_list.html" + +class GroupEditView(UpdateView): + model = Group + pk_url_kwarg = "group_id" + template_name = "core/group_edit.html" + form_class = GroupEditForm + diff --git a/core/views/page.py b/core/views/page.py index ffcb8411..6bffcab3 100644 --- a/core/views/page.py +++ b/core/views/page.py @@ -2,6 +2,8 @@ from django.shortcuts import render, redirect, get_object_or_404 from django.views.generic import ListView, DetailView from django.views.generic.edit import UpdateView +from django.contrib.auth.decorators import login_required, permission_required +from django.utils.decorators import method_decorator from core.models import Page @@ -15,6 +17,10 @@ class PageListView(ListView): class PageView(DetailView): model = Page + @method_decorator(permission_required('core.can_view')) + def dispatch(self, *args, **kwargs): + return super(PageView, self).dispatch(*args, **kwargs) + def get_object(self): self.page = Page.get_page_by_full_name(self.kwargs['page_name']) return self.page