Basic group view and permissions management on the pages

This commit is contained in:
Skia 2015-11-26 17:40:31 +01:00
parent 48e3f88b87
commit b19ec084b6
11 changed files with 119 additions and 4 deletions

View File

@ -1,7 +1,8 @@
from django.contrib import admin from django.contrib import admin
from .models import User, Page from .models import User, Page, Group
admin.site.register(User) admin.site.register(User)
admin.site.register(Group)
admin.site.register(Page) admin.site.register(Page)

View File

@ -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]}}] [{"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"}]

View File

@ -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'),
),
]

View File

@ -1,5 +1,5 @@
from django.db import models 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.translation import ugettext_lazy as _
from django.utils import timezone from django.utils import timezone
from django.core import validators from django.core import validators
@ -125,6 +125,12 @@ class User(AbstractBaseUser, PermissionsMixin):
self.username = user_name self.username = user_name
return 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): class Page(models.Model):
""" """
@ -227,3 +233,4 @@ class Page(models.Model):
def get_display_name(self): def get_display_name(self):
return self.get_full_name() return self.get_full_name()

View File

@ -0,0 +1,16 @@
{% extends "core/base.html" %}
{% block content %}
<p><a href="{% url 'core:group_list' %}">Back to list</a></p>
<h2>Edit group</h2>
<form action="" method="post">
{% csrf_token %}
{{ form.as_p }}
<p><input type="submit" value="Update" /></p>
</form>
{% endblock %}

View File

@ -0,0 +1,15 @@
{% extends "core/base.html" %}
{% block title %}
Group list
{% endblock %}
{% block content %}
<h3>Group list</h3>
<ul>
{% for g in group_list %}
<li><a href="{% url 'core:group_edit' g.id %}">{{ g.name }}</a></li>
{% endfor %}
</ul>
{% endblock %}

View File

@ -17,6 +17,9 @@ urlpatterns = [
url(r'^reset/done/$', password_reset_complete, name='password_reset_complete'), url(r'^reset/done/$', password_reset_complete, name='password_reset_complete'),
url(r'^register$', register, name='register'), url(r'^register$', register, name='register'),
url(r'^group/$', GroupListView.as_view(), name='group_list'),
url(r'^group/(?P<group_id>[0-9]+)/$', GroupEditView.as_view(), name='group_edit'),
url(r'^user/$', UserListView.as_view(), name='user_list'), url(r'^user/$', UserListView.as_view(), name='user_list'),
url(r'^user/(?P<user_id>[0-9]+)/$', UserView.as_view(), name='user_profile'), url(r'^user/(?P<user_id>[0-9]+)/$', UserView.as_view(), name='user_profile'),
url(r'^user/(?P<user_id>[0-9]+)/edit$', UserUpdateProfileView.as_view(), name='user_edit'), url(r'^user/(?P<user_id>[0-9]+)/edit$', UserUpdateProfileView.as_view(), name='user_edit'),

View File

@ -1,3 +1,4 @@
from .user import * from .user import *
from .page import * from .page import *
from .site import * from .site import *
from .group import *

View File

@ -4,7 +4,7 @@ from django.contrib.auth import logout, login, authenticate
from django.forms import CheckboxSelectMultiple from django.forms import CheckboxSelectMultiple
import logging import logging
from core.models import User, Page from core.models import User, Page, Group
class RegisteringForm(UserCreationForm): class RegisteringForm(UserCreationForm):
error_css_class = 'error' error_css_class = 'error'
@ -33,3 +33,13 @@ class UserGroupsForm(forms.ModelForm):
'user_permissions': CheckboxSelectMultiple, '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,
}

19
core/views/group.py Normal file
View File

@ -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

View File

@ -2,6 +2,8 @@
from django.shortcuts import render, redirect, get_object_or_404 from django.shortcuts import render, redirect, get_object_or_404
from django.views.generic import ListView, DetailView from django.views.generic import ListView, DetailView
from django.views.generic.edit import UpdateView 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 from core.models import Page
@ -15,6 +17,10 @@ class PageListView(ListView):
class PageView(DetailView): class PageView(DetailView):
model = Page 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): def get_object(self):
self.page = Page.get_page_by_full_name(self.kwargs['page_name']) self.page = Page.get_page_by_full_name(self.kwargs['page_name'])
return self.page return self.page