Skip to content

Commit 60f75e4

Browse files
committed
Implement permissions on news tags
This makes it possible to limit which organisations can use specific tags in news, and verify those as news is submitted. Administrators can, as always, override. In passing also add a sortkey field to newstags to make them, well, sortable.
1 parent 7b96690 commit 60f75e4

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

pgweb/news/admin.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class NewsArticleAdmin(PgwebAdmin):
1414

1515
class NewsTagAdmin(PgwebAdmin):
1616
list_display = ('urlname', 'name', 'description')
17+
filter_horizontal = ('allowed_orgs', )
1718

1819

1920
admin.site.register(NewsArticle, NewsArticleAdmin)

pgweb/news/forms.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,21 @@ def described_checkboxes(self):
2626
'tags': {t.id: t.description for t in NewsTag.objects.all()}
2727
}
2828

29+
def clean(self):
30+
data = super().clean()
31+
32+
for t in data['tags']:
33+
# Check each tag for permissions. This is not very db-efficient, but people
34+
# don't save news articles that often...
35+
if t.allowed_orgs.exists() and not t.allowed_orgs.filter(pk=data['org'].pk).exists():
36+
self.add_error('tags',
37+
'The organisation {} is not allowed to use the tag {}.'.format(
38+
data['org'],
39+
t,
40+
))
41+
42+
return data
43+
2944
class Meta:
3045
model = NewsArticle
3146
exclude = ('submitter', 'modstate', 'tweeted')
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# -*- coding: utf-8 -*-
2+
# Generated by Django 1.11.27 on 2020-07-04 15:47
3+
from __future__ import unicode_literals
4+
5+
from django.db import migrations, models
6+
7+
8+
class Migration(migrations.Migration):
9+
10+
dependencies = [
11+
('core', '0002_block_oauth'),
12+
('news', '0004_modstate'),
13+
]
14+
15+
operations = [
16+
migrations.AddField(
17+
model_name='newstag',
18+
name='allowed_orgs',
19+
field=models.ManyToManyField(blank=True, help_text='Organisations allowed to use this tag', to='core.Organisation'),
20+
),
21+
migrations.AddField(
22+
model_name='newstag',
23+
name='sortkey',
24+
field=models.IntegerField(default=100),
25+
),
26+
migrations.AlterModelOptions(
27+
name='newstag',
28+
options={'ordering': ('sortkey', 'urlname', )},
29+
)
30+
]

pgweb/news/models.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ class NewsTag(models.Model):
88
urlname = models.CharField(max_length=20, null=False, blank=False, unique=True)
99
name = models.CharField(max_length=32, null=False, blank=False)
1010
description = models.CharField(max_length=200, null=False, blank=False)
11+
allowed_orgs = models.ManyToManyField(Organisation, blank=True,
12+
help_text="Organisations allowed to use this tag")
13+
sortkey = models.IntegerField(null=False, blank=False, default=100)
1114

1215
def __str__(self):
1316
return self.name
1417

1518
class Meta:
16-
ordering = ('urlname', )
19+
ordering = ('sortkey', 'urlname', )
1720

1821

1922
class NewsArticle(TristateModerateModel):

0 commit comments

Comments
 (0)