diff --git a/svjis/articles/forms.py b/svjis/articles/forms.py index 6b4d1cc4..3a515fc4 100644 --- a/svjis/articles/forms.py +++ b/svjis/articles/forms.py @@ -18,7 +18,7 @@ class Meta: class ArticleForm(forms.ModelForm): class Meta: model = models.Article - fields = ("header", "perex", "body", "menu", "allow_comments", "published",) + fields = ("header", "perex", "body", "menu", "allow_comments", "published", "visible_for_all") widgets = { 'header': forms.widgets.TextInput(attrs={'class': 'common-input', 'size': '50'}), 'perex': forms.widgets.Textarea(attrs={'class': 'common-textarea', 'rows': '10', 'cols': '80', 'wrap': True}), @@ -26,6 +26,7 @@ class Meta: 'menu': forms.widgets.Select(attrs={'class': 'common-input'}), 'allow_comments': forms.widgets.CheckboxInput(attrs={'class': 'common-input'}), 'published': forms.widgets.CheckboxInput(attrs={'class': 'common-input'}), + 'visible_for_all': forms.widgets.CheckboxInput(attrs={'class': 'common-input'}), } diff --git a/svjis/articles/migrations/0005_article_visible_for_all_article_visible_for_group.py b/svjis/articles/migrations/0005_article_visible_for_all_article_visible_for_group.py new file mode 100644 index 00000000..10348a15 --- /dev/null +++ b/svjis/articles/migrations/0005_article_visible_for_all_article_visible_for_group.py @@ -0,0 +1,24 @@ +# Generated by Django 5.0.3 on 2024-03-13 13:00 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('articles', '0004_board_building_buildingentrance_buildingunit_and_more'), + ('auth', '0012_alter_user_first_name_max_length'), + ] + + operations = [ + migrations.AddField( + model_name='article', + name='visible_for_all', + field=models.BooleanField(default=False, verbose_name='Visible for all'), + ), + migrations.AddField( + model_name='article', + name='visible_for_group', + field=models.ManyToManyField(to='auth.group'), + ), + ] diff --git a/svjis/articles/models.py b/svjis/articles/models.py index c4c96ee8..823cff85 100644 --- a/svjis/articles/models.py +++ b/svjis/articles/models.py @@ -1,6 +1,6 @@ import os from django.db import models -from django.contrib.auth.models import User +from django.contrib.auth.models import User, Group from django.utils.translation import gettext_lazy as _ @@ -16,6 +16,8 @@ class Article(models.Model): body = models.TextField(_("Body (markdown)")) menu = models.ForeignKey("ArticleMenu", on_delete=models.CASCADE, null=False, blank=False) allow_comments = models.BooleanField(_("Allow comments"), default=False) + visible_for_all = models.BooleanField(_("Visible for all"), default=False) + visible_for_group = models.ManyToManyField(Group) def __str__(self): return f"Article: {self.header}" diff --git a/svjis/articles/templates/redaction_article_edit.html b/svjis/articles/templates/redaction_article_edit.html index 47bbbbec..a3902312 100644 --- a/svjis/articles/templates/redaction_article_edit.html +++ b/svjis/articles/templates/redaction_article_edit.html @@ -35,6 +35,21 @@
diff --git a/svjis/articles/views_redaction.py b/svjis/articles/views_redaction.py index 1e319f5d..7c13f69e 100644 --- a/svjis/articles/views_redaction.py +++ b/svjis/articles/views_redaction.py @@ -2,6 +2,7 @@ from django.shortcuts import render, redirect, get_object_or_404 from django.contrib import messages from django.contrib.auth.decorators import permission_required +from django.contrib.auth.models import Group from django.core.paginator import Paginator, InvalidPage from django.db.models import Q from django.conf import settings @@ -133,9 +134,15 @@ def redaction_article_edit_view(request, pk): else: form = forms.ArticleForm + group_list = [] + for g in Group.objects.all(): + item = {'name': g.name, 'checked': g in form.instance.visible_for_group.all() if pk != 0 else False} + group_list.append(item) + ctx = utils.get_context() ctx['aside_menu_name'] = _("Redaction") ctx['form'] = form + ctx['group_list'] = group_list ctx['asset_form'] = forms.ArticleAssetForm ctx['pk'] = pk ctx['aside_menu_items'] = get_side_menu('article', request.user) @@ -158,6 +165,15 @@ def redaction_article_save_view(request): if pk == 0: obj.author = request.user obj.save() + + # Set groups + group_list = obj.visible_for_group.all() + for g in Group.objects.all(): + gr_set = request.POST.get(g.name, False) == 'on' + if gr_set and g not in group_list: + obj.visible_for_group.add(g) + if not gr_set and g in group_list: + obj.visible_for_group.remove(g) else: for error in form.errors: messages.error(request, error)