Skip to content

Commit

Permalink
Article visible for - model
Browse files Browse the repository at this point in the history
  • Loading branch information
berk76 committed Mar 13, 2024
1 parent 2755440 commit 3162174
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 2 deletions.
3 changes: 2 additions & 1 deletion svjis/articles/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ 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}),
'body': forms.widgets.Textarea(attrs={'class': 'common-textarea', 'rows': '20', 'cols': '80', 'wrap': True}),
'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'}),
}


Expand Down
Original file line number Diff line number Diff line change
@@ -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'),
),
]
4 changes: 3 additions & 1 deletion svjis/articles/models.py
Original file line number Diff line number Diff line change
@@ -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 _


Expand All @@ -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}"
Expand Down
15 changes: 15 additions & 0 deletions svjis/articles/templates/redaction_article_edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ <h1 class="page-title">{% trans 'Article' %}</h1>
{{ form.published }}
</p>
</fieldset>
<fieldset>
<legend>{% trans 'Visible for' %}</legend>
<p>
<label class="common-label" for="{{ form.visible_for_all.auto_id }}">{{ form.visible_for_all.label }}</label>
{{ form.visible_for_all }}
</p>
{% if group_list %}
{% for obj in group_list %}
<p>
<label class="common-label" id="{{ obj.name }}-label" for="{{ obj.name }}-input">{{ obj.name }}</label>
<input class="common-input" id="{{ obj.name }}-input" type="checkbox" name="{{ obj.name }}" {% if obj.checked %}checked{% endif %}/>
</p>
{% endfor %}
{% endif %}
</fieldset>
<p>
<input class="my-button" id="submit" type="submit" value="{% trans 'Save' %}" />
</p>
Expand Down
16 changes: 16 additions & 0 deletions svjis/articles/views_redaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 3162174

Please sign in to comment.