Add basic right management to forum. Need to test it!

This commit is contained in:
Skia 2017-01-21 04:51:37 +01:00
parent 4dd6f01e60
commit ff77df3646
10 changed files with 79 additions and 99 deletions

View File

@ -38,7 +38,9 @@ class Command(BaseCommand):
Site(id=4000, domain=settings.SITH_URL, name=settings.SITH_NAME).save()
root_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
Group(name="Root").save()
Group(name="Not registered users").save()
Group(name="Public").save()
Group(name="Subscribers").save()
Group(name="Old subscribers").save()
Group(name="Accounting admin").save()
Group(name="Communication admin").save()
Group(name="Counter admin").save()
@ -46,6 +48,7 @@ class Command(BaseCommand):
Group(name="Banned from counters").save()
Group(name="Banned to subscribe").save()
Group(name="SAS admin").save()
Group(name="Forum admin").save()
self.reset_index("core", "auth")
root = User(id=0, username='root', last_name="", first_name="Bibou",
email="ae.info@utbm.fr",

View File

@ -204,6 +204,10 @@ class User(AbstractBaseUser):
return False
if group_id == settings.SITH_GROUP_PUBLIC_ID:
return True
if group_id == settings.SITH_GROUP_SUBSCRIBERS_ID:
return self.is_subscribed()
if group_id == settings.SITH_GROUP_OLD_SUBSCRIBERS_ID:
return self.was_subscribed()
if group_name == settings.SITH_MAIN_MEMBERS_GROUP: # We check the subscription if asked
return self.is_subscribed()
if group_name[-len(settings.SITH_BOARD_SUFFIX):] == settings.SITH_BOARD_SUFFIX:

View File

@ -2,6 +2,7 @@
from __future__ import unicode_literals
from django.db import migrations, models
import django.utils.timezone
from django.conf import settings
@ -16,36 +17,45 @@ class Migration(migrations.Migration):
migrations.CreateModel(
name='Forum',
fields=[
('id', models.AutoField(verbose_name='ID', auto_created=True, primary_key=True, serialize=False)),
('id', models.AutoField(auto_created=True, serialize=False, verbose_name='ID', primary_key=True)),
('name', models.CharField(verbose_name='name', max_length=64)),
('description', models.CharField(default='', verbose_name='description', max_length=256)),
('is_category', models.BooleanField(default=False, verbose_name='is a category')),
('edit_groups', models.ManyToManyField(to='core.Group', blank=True, related_name='editable_forums')),
('owner_group', models.ForeignKey(to='core.Group', default=4, related_name='owned_forums')),
('parent', models.ForeignKey(blank=True, null=True, to='forum.Forum', related_name='children')),
('view_groups', models.ManyToManyField(to='core.Group', blank=True, related_name='viewable_forums')),
('edit_groups', models.ManyToManyField(default=[4], related_name='editable_forums', blank=True, to='core.Group')),
('owner_group', models.ForeignKey(default=12, related_name='owned_forums', to='core.Group')),
('parent', models.ForeignKey(null=True, related_name='children', blank=True, to='forum.Forum')),
('view_groups', models.ManyToManyField(default=[2], related_name='viewable_forums', blank=True, to='core.Group')),
],
),
migrations.CreateModel(
name='ForumMessage',
fields=[
('id', models.AutoField(verbose_name='ID', auto_created=True, primary_key=True, serialize=False)),
('title', models.CharField(default='', blank=True, verbose_name='title', max_length=64)),
('message', models.TextField(verbose_name='message', default='')),
('author', models.ForeignKey(to=settings.AUTH_USER_MODEL, related_name='forum_messages')),
('id', models.AutoField(auto_created=True, serialize=False, verbose_name='ID', primary_key=True)),
('title', models.CharField(default='', verbose_name='title', blank=True, max_length=64)),
('message', models.TextField(default='', verbose_name='message')),
('date', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date')),
('author', models.ForeignKey(related_name='forum_messages', to=settings.AUTH_USER_MODEL)),
],
options={
'ordering': ['id'],
},
),
migrations.CreateModel(
name='ForumTopic',
fields=[
('id', models.AutoField(verbose_name='ID', auto_created=True, primary_key=True, serialize=False)),
('author', models.ForeignKey(to=settings.AUTH_USER_MODEL, related_name='forum_topics')),
('forum', models.ForeignKey(to='forum.Forum', related_name='topics')),
('id', models.AutoField(auto_created=True, serialize=False, verbose_name='ID', primary_key=True)),
('title', models.CharField(default='', verbose_name='title', max_length=64)),
('description', models.CharField(default='', verbose_name='description', max_length=256)),
('author', models.ForeignKey(related_name='forum_topics', to=settings.AUTH_USER_MODEL)),
('forum', models.ForeignKey(related_name='topics', to='forum.Forum')),
],
options={
'ordering': ['-id'],
},
),
migrations.AddField(
model_name='forummessage',
name='topic',
field=models.ForeignKey(to='forum.ForumTopic', related_name='messages'),
field=models.ForeignKey(related_name='messages', to='forum.ForumTopic'),
),
]

View File

@ -1,19 +0,0 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('forum', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='forumtopic',
name='title',
field=models.CharField(verbose_name='title', max_length=64, default=''),
),
]

View File

@ -1,24 +0,0 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
import django.utils.timezone
class Migration(migrations.Migration):
dependencies = [
('forum', '0002_forumtopic_title'),
]
operations = [
migrations.AlterModelOptions(
name='forumtopic',
options={'ordering': ['messages__date', '-id']},
),
migrations.AddField(
model_name='forummessage',
name='date',
field=models.DateTimeField(verbose_name='date', default=django.utils.timezone.now),
),
]

View File

@ -1,19 +0,0 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('forum', '0003_auto_20170121_0311'),
]
operations = [
migrations.AddField(
model_name='forumtopic',
name='description',
field=models.CharField(max_length=256, verbose_name='description', default=''),
),
]

View File

@ -17,10 +17,13 @@ class Forum(models.Model):
description = models.CharField(_('description'), max_length=256, default="")
is_category = models.BooleanField(_('is a category'), default=False)
parent = models.ForeignKey('Forum', related_name='children', null=True, blank=True)
owner_group = models.ForeignKey(Group, related_name="owned_forums",
default=settings.SITH_GROUP_COM_ADMIN_ID)
edit_groups = models.ManyToManyField(Group, related_name="editable_forums", blank=True)
view_groups = models.ManyToManyField(Group, related_name="viewable_forums", blank=True)
edit_groups = models.ManyToManyField(Group, related_name="editable_forums", blank=True,
default=[settings.SITH_GROUP_OLD_SUBSCRIBERS_ID])
view_groups = models.ManyToManyField(Group, related_name="viewable_forums", blank=True,
default=[settings.SITH_GROUP_PUBLIC_ID])
def is_owned_by(self, user):
return user.is_in_group(settings.SITH_GROUP_FORUM_ADMIN_ID)
def check_loop(self):
"""Raise a validation error when a loop is found within the parent list"""
@ -58,6 +61,15 @@ class ForumTopic(models.Model):
class Meta:
ordering = ['-id'] # TODO: add date message ordering
def is_owned_by(self, user):
return self.forum.is_owned_by(user) or user.id == self.author.id
def can_be_edited_by(self, user):
return user.can_edit(self.forum)
def can_be_viewed_by(self, user):
return user.can_view(self.forum)
def __str__(self):
return "%s" % (self.title)
@ -66,7 +78,7 @@ class ForumTopic(models.Model):
class ForumMessage(models.Model):
"""
"A ForumMessage object is a message in the forum" Cpt. Obvious
"A ForumMessage object represents a message in the forum" -- Cpt. Obvious
"""
topic = models.ForeignKey(ForumTopic, related_name='messages')
author = models.ForeignKey(User, related_name='forum_messages')
@ -77,6 +89,15 @@ class ForumMessage(models.Model):
class Meta:
ordering = ['id']
def is_owned_by(self, user):
return self.topic.is_owned_by(user) or user.id == self.author.id
def can_be_edited_by(self, user):
return user.can_edit(self.topic)
def can_be_viewed_by(self, user):
return user.can_view(self.topic)
def get_absolute_url(self):
return self.topic.get_absolute_url()

View File

@ -11,6 +11,5 @@ urlpatterns = [
url(r'^topic/(?P<topic_id>[0-9]+)$', ForumTopicDetailView.as_view(), name='view_topic'),
url(r'^topic/(?P<topic_id>[0-9]+)/edit$', ForumTopicEditView.as_view(), name='edit_topic'),
url(r'^topic/(?P<topic_id>[0-9]+)/new_message$', ForumMessageCreateView.as_view(), name='new_message'),
# url(r'^(?P<club_id>[0-9]+)/tools$', ClubToolsView.as_view(), name='tools'),
]

View File

@ -8,15 +8,16 @@ from django.conf import settings
from django import forms
from django.core.exceptions import PermissionDenied
from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin, CanCreateMixin, TabedViewMixin
from forum.models import Forum, ForumMessage, ForumTopic
class ForumMainView(ListView):
class ForumMainView(CanViewMixin, ListView):
queryset = Forum.objects.filter(parent=None)
template_name = "forum/main.jinja"
class ForumCreateView(CreateView):
class ForumCreateView(CanCreateMixin, CreateView):
model = Forum
fields = ['name', 'parent', 'is_category', 'owner_group', 'edit_groups', 'view_groups']
fields = ['name', 'parent', 'is_category', 'edit_groups', 'view_groups']
template_name = "core/create.jinja"
def get_initial(self):
@ -25,19 +26,19 @@ class ForumCreateView(CreateView):
init['parent'] = parent
return init
class ForumEditView(UpdateView):
class ForumEditView(CanEditPropMixin, UpdateView):
model = Forum
pk_url_kwarg = "forum_id"
fields = ['name', 'parent', 'is_category', 'owner_group', 'edit_groups', 'view_groups']
fields = ['name', 'parent', 'is_category', 'edit_groups', 'view_groups']
template_name = "core/edit.jinja"
success_url = reverse_lazy('forum:main')
class ForumDetailView(DetailView):
class ForumDetailView(CanViewMixin, DetailView):
model = Forum
template_name = "forum/forum.jinja"
pk_url_kwarg = "forum_id"
class ForumTopicCreateView(CreateView):
class ForumTopicCreateView(CanCreateMixin, CreateView):
model = ForumMessage
fields = ['title', 'message']
template_name = "core/create.jinja"
@ -55,19 +56,19 @@ class ForumTopicCreateView(CreateView):
form.instance.author = self.request.user
return super(ForumTopicCreateView, self).form_valid(form)
class ForumTopicEditView(UpdateView):
class ForumTopicEditView(CanEditPropMixin, UpdateView):
model = ForumTopic
fields = ['title', 'forum']
pk_url_kwarg = "topic_id"
template_name = "core/edit.jinja"
class ForumTopicDetailView(DetailView):
class ForumTopicDetailView(CanViewMixin, DetailView):
model = ForumTopic
pk_url_kwarg = "topic_id"
template_name = "forum/topic.jinja"
context_object_name = "topic"
class ForumMessageCreateView(CreateView):
class ForumMessageCreateView(CanCreateMixin, CreateView):
model = ForumMessage
fields = ['title', 'message']
template_name = "core/create.jinja"

View File

@ -261,13 +261,17 @@ SITH_SCHOOL_START_YEAR = 1999
SITH_GROUP_ROOT_ID = 1
SITH_GROUP_PUBLIC_ID = 2
SITH_GROUP_ACCOUNTING_ADMIN_ID = 3
SITH_GROUP_COM_ADMIN_ID = 4
SITH_GROUP_COUNTER_ADMIN_ID = 5
SITH_GROUP_BANNED_ALCOHOL_ID = 6
SITH_GROUP_BANNED_COUNTER_ID = 7
SITH_GROUP_BANNED_SUBSCRIPTION_ID = 8
SITH_GROUP_SAS_ADMIN_ID = 9
SITH_GROUP_SUBSCRIBERS_ID = 3
SITH_GROUP_OLD_SUBSCRIBERS_ID = 4
SITH_GROUP_ACCOUNTING_ADMIN_ID = 5
SITH_GROUP_COM_ADMIN_ID = 6
SITH_GROUP_COUNTER_ADMIN_ID = 7
SITH_GROUP_BANNED_ALCOHOL_ID = 8
SITH_GROUP_BANNED_COUNTER_ID = 9
SITH_GROUP_BANNED_SUBSCRIPTION_ID = 10
SITH_GROUP_SAS_ADMIN_ID = 11
SITH_GROUP_FORUM_ADMIN_ID = 12
SITH_CLUB_REFOUND_ID = 89
SITH_COUNTER_REFOUND_ID = 38