diff --git a/forum/migrations/0002_auto_20170128_1958.py b/forum/migrations/0002_auto_20170128_1958.py new file mode 100644 index 00000000..bfc0c90a --- /dev/null +++ b/forum/migrations/0002_auto_20170128_1958.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('forum', '0001_initial'), + ] + + operations = [ + migrations.RemoveField( + model_name='forum', + name='owner_group', + ), + migrations.RemoveField( + model_name='forumtopic', + name='title', + ), + ] diff --git a/forum/migrations/0003_forum_owner_club.py b/forum/migrations/0003_forum_owner_club.py new file mode 100644 index 00000000..e4a0510d --- /dev/null +++ b/forum/migrations/0003_forum_owner_club.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('club', '0006_auto_20161229_0040'), + ('forum', '0002_auto_20170128_1958'), + ] + + operations = [ + migrations.AddField( + model_name='forum', + name='owner_club', + field=models.ForeignKey(related_name='owned_forums', verbose_name='owner club', to='club.Club', default=1), + ), + ] diff --git a/forum/models.py b/forum/models.py index a69e5b0d..781b90aa 100644 --- a/forum/models.py +++ b/forum/models.py @@ -8,6 +8,7 @@ from django.core.urlresolvers import reverse from django.utils import timezone from core.models import User, MetaGroup, Group, SithFile +from club.models import Club class Forum(models.Model): """ @@ -17,13 +18,45 @@ 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_club = models.ForeignKey(Club, related_name="owned_forums", verbose_name=_("owner club"), + default=settings.SITH_MAIN_CLUB_ID) 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 clean(self): + self.check_loop() + + def save(self, *args, **kwargs): + copy_rights = False + if self.id is None: + copy_rights = True + super(Forum, self).save(*args, **kwargs) + if copy_rights: + self.copy_rights() + + def apply_rights_recursively(self): + children = self.children.all() + for c in children: + c.copy_rights() + c.apply_rights_recursively() + + def copy_rights(self): + """Copy, if possible, the rights of the parent folder""" + if self.parent is not None: + self.owner_club = self.parent.owner_club + self.edit_groups = self.parent.edit_groups.all() + self.view_groups = self.parent.view_groups.all() + self.save() + def is_owned_by(self, user): - return user.is_in_group(settings.SITH_GROUP_FORUM_ADMIN_ID) + if user.is_in_group(settings.SITH_GROUP_FORUM_ADMIN_ID): + return True + m = self.owner_club.get_membership_for(user) + if m: + return m.role > settings.SITH_MAXIMUM_FREE_ROLE + return False def check_loop(self): """Raise a validation error when a loop is found within the parent list""" @@ -35,9 +68,6 @@ class Forum(models.Model): objs.append(cur) cur = cur.parent - def clean(self): - self.check_loop() - def __str__(self): return "%s" % (self.name) @@ -73,7 +103,6 @@ class Forum(models.Model): class ForumTopic(models.Model): forum = models.ForeignKey(Forum, related_name='topics') author = models.ForeignKey(User, related_name='forum_topics') - title = models.CharField(_("title"), default="", max_length=64) description = models.CharField(_('description'), max_length=256, default="") class Meta: @@ -94,6 +123,10 @@ class ForumTopic(models.Model): def get_absolute_url(self): return reverse('forum:view_topic', kwargs={'topic_id': self.id}) + @property + def title(self): + return self.messages.order_by('date').first().title + class ForumMessage(models.Model): """ "A ForumMessage object represents a message in the forum" -- Cpt. Obvious diff --git a/forum/templates/forum/forum.jinja b/forum/templates/forum/forum.jinja index 624b13f2..747acf1c 100644 --- a/forum/templates/forum/forum.jinja +++ b/forum/templates/forum/forum.jinja @@ -11,9 +11,14 @@ > {{ forum }}
+ {% if user.is_in_group(settings.SITH_GROUP_FORUM_ADMIN_ID) or user.is_in_group(settings.SITH_GROUP_COM_ADMIN_ID) %}
+ New forum
+ {% endif %}
+ New topic
+
{{ t.description }}
- {% if user.can_edit(t) %} + {% if user.is_owner(t) %} @@ -45,7 +50,6 @@+ {% if user.is_in_group(settings.SITH_GROUP_FORUM_ADMIN_ID) or user.is_in_group(settings.SITH_GROUP_COM_ADMIN_ID) %} + New forum + {% endif %} +
{% for f in forum_list %}