diff --git a/core/management/commands/populate.py b/core/management/commands/populate.py index d32560e5..ee34c071 100644 --- a/core/management/commands/populate.py +++ b/core/management/commands/populate.py @@ -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", diff --git a/core/models.py b/core/models.py index 8a892730..bd3d3753 100644 --- a/core/models.py +++ b/core/models.py @@ -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: diff --git a/forum/migrations/0001_initial.py b/forum/migrations/0001_initial.py index da8dbdfc..4b53912e 100644 --- a/forum/migrations/0001_initial.py +++ b/forum/migrations/0001_initial.py @@ -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'), ), ] diff --git a/forum/migrations/0002_forumtopic_title.py b/forum/migrations/0002_forumtopic_title.py deleted file mode 100644 index 17407dad..00000000 --- a/forum/migrations/0002_forumtopic_title.py +++ /dev/null @@ -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=''), - ), - ] diff --git a/forum/migrations/0003_auto_20170121_0311.py b/forum/migrations/0003_auto_20170121_0311.py deleted file mode 100644 index f66151e2..00000000 --- a/forum/migrations/0003_auto_20170121_0311.py +++ /dev/null @@ -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), - ), - ] diff --git a/forum/migrations/0004_forumtopic_description.py b/forum/migrations/0004_forumtopic_description.py deleted file mode 100644 index b1e3fe96..00000000 --- a/forum/migrations/0004_forumtopic_description.py +++ /dev/null @@ -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=''), - ), - ] diff --git a/forum/models.py b/forum/models.py index 0ae36af0..59605114 100644 --- a/forum/models.py +++ b/forum/models.py @@ -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() diff --git a/forum/urls.py b/forum/urls.py index 8c55bd1c..ef1c180e 100644 --- a/forum/urls.py +++ b/forum/urls.py @@ -11,6 +11,5 @@ urlpatterns = [ url(r'^topic/(?P[0-9]+)$', ForumTopicDetailView.as_view(), name='view_topic'), url(r'^topic/(?P[0-9]+)/edit$', ForumTopicEditView.as_view(), name='edit_topic'), url(r'^topic/(?P[0-9]+)/new_message$', ForumMessageCreateView.as_view(), name='new_message'), - # url(r'^(?P[0-9]+)/tools$', ClubToolsView.as_view(), name='tools'), ] diff --git a/forum/views.py b/forum/views.py index fef22b34..e1126bef 100644 --- a/forum/views.py +++ b/forum/views.py @@ -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" diff --git a/sith/settings.py b/sith/settings.py index c5696f4f..5bbcacdf 100644 --- a/sith/settings.py +++ b/sith/settings.py @@ -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