Unverified Commit 8726ad82 authored by Thom Wiggers's avatar Thom Wiggers 📐
Browse files

Make Events.organiser NOT NULL

Makes sure the organiser is always set.
parent d7d62d20
...@@ -72,7 +72,6 @@ class EventAdmin(DoNextModelAdmin): ...@@ -72,7 +72,6 @@ class EventAdmin(DoNextModelAdmin):
def has_change_permission(self, request, event=None): def has_change_permission(self, request, event=None):
try: try:
if (not request.user.is_superuser and event is not None and if (not request.user.is_superuser and event is not None and
event.organiser is not None and
not request.user.has_perm('events.override_organiser')): not request.user.has_perm('events.override_organiser')):
committees = request.user.member.get_committees().filter( committees = request.user.member.get_committees().filter(
Q(pk=event.organiser.pk)).count() Q(pk=event.organiser.pk)).count()
......
# -*- coding: utf-8 -*-
# Generated by Django 1.10.6 on 2017-04-05 17:04
from __future__ import unicode_literals
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('events', '0014_auto_20170403_1823'),
]
operations = [
migrations.AlterField(
model_name='event',
name='organiser',
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.PROTECT, to='activemembers.Committee', verbose_name='organiser'),
preserve_default=False,
),
]
...@@ -41,8 +41,7 @@ class Event(models.Model, metaclass=ModelTranslateMeta): ...@@ -41,8 +41,7 @@ class Event(models.Model, metaclass=ModelTranslateMeta):
organiser = models.ForeignKey( organiser = models.ForeignKey(
'activemembers.Committee', 'activemembers.Committee',
models.SET_NULL, models.PROTECT,
null=True,
verbose_name=_("organiser") verbose_name=_("organiser")
) )
......
...@@ -4,6 +4,7 @@ from django.core.exceptions import ValidationError ...@@ -4,6 +4,7 @@ from django.core.exceptions import ValidationError
from django.test import TestCase from django.test import TestCase
from django.utils import timezone from django.utils import timezone
from activemembers.models import Committee
from events.models import Event, Registration from events.models import Event, Registration
from members.models import Member from members.models import Member
...@@ -11,13 +12,14 @@ from members.models import Member ...@@ -11,13 +12,14 @@ from members.models import Member
class EventTest(TestCase): class EventTest(TestCase):
"""Tests events""" """Tests events"""
fixtures = ['members.json'] fixtures = ['members.json', 'committees.json']
@classmethod @classmethod
def setUpTestData(cls): def setUpTestData(cls):
cls.event = Event.objects.create( cls.event = Event.objects.create(
title_nl='testevene', title_nl='testevene',
title_en='testevent', title_en='testevent',
organiser=Committee.objects.get(pk=1),
description_en='desc', description_en='desc',
description_nl='besch', description_nl='besch',
start=(timezone.now() + datetime.timedelta(hours=1)), start=(timezone.now() + datetime.timedelta(hours=1)),
...@@ -159,13 +161,14 @@ class EventTest(TestCase): ...@@ -159,13 +161,14 @@ class EventTest(TestCase):
class RegistrationTest(TestCase): class RegistrationTest(TestCase):
"""Tests event registrations""" """Tests event registrations"""
fixtures = ['members.json'] fixtures = ['members.json', 'committees.json']
@classmethod @classmethod
def setUpTestData(cls): def setUpTestData(cls):
cls.event = Event.objects.create( cls.event = Event.objects.create(
title_nl='testevene', title_nl='testevene',
title_en='testevent', title_en='testevent',
organiser=Committee.objects.get(pk=1),
description_en='desc', description_en='desc',
description_nl='besch', description_nl='besch',
start=timezone.now(), start=timezone.now(),
......
...@@ -16,8 +16,10 @@ class AdminTest(TestCase): ...@@ -16,8 +16,10 @@ class AdminTest(TestCase):
@classmethod @classmethod
def setUpTestData(cls): def setUpTestData(cls):
cls.committee = Committee.objects.get(pk=1)
cls.event = Event.objects.create( cls.event = Event.objects.create(
pk=1, pk=1,
organiser=cls.committee,
title_nl='testevenement', title_nl='testevenement',
title_en='testevent', title_en='testevent',
description_en='desc', description_en='desc',
...@@ -40,7 +42,6 @@ class AdminTest(TestCase): ...@@ -40,7 +42,6 @@ class AdminTest(TestCase):
cls.member.user.user_permissions.add(cls.permission_change_event) cls.member.user.user_permissions.add(cls.permission_change_event)
cls.member.user.is_superuser = False cls.member.user.is_superuser = False
cls.member.user.save() cls.member.user.save()
cls.committee = Committee.objects.get(pk=1)
def setUp(self): def setUp(self):
self.client.force_login(self.member.user) self.client.force_login(self.member.user)
...@@ -58,95 +59,64 @@ class AdminTest(TestCase): ...@@ -58,95 +59,64 @@ class AdminTest(TestCase):
self.assertEqual(302, response.status_code) self.assertEqual(302, response.status_code)
self.assertTrue(response.url.startswith('/login/')) self.assertTrue(response.url.startswith('/login/'))
def test_admin_details_no_organiser_allowed_access(self):
"""If an event has no organiser, then I should be allowed access"""
response = self.client.get('/events/admin/1/')
self.assertEqual(200, response.status_code)
def test_admin_details_organiser_denied(self): def test_admin_details_organiser_denied(self):
self.event.organiser = self.committee
self.event.save()
response = self.client.get('/events/admin/1/') response = self.client.get('/events/admin/1/')
self.assertEqual(403, response.status_code) self.assertEqual(403, response.status_code)
def test_admin_details_organiser_allowed(self): def test_admin_details_organiser_allowed(self):
self.event.organiser = self.committee
CommitteeMembership.objects.create( CommitteeMembership.objects.create(
member=self.member, member=self.member,
committee=self.committee) committee=self.committee)
self.event.save()
response = self.client.get('/events/admin/1/') response = self.client.get('/events/admin/1/')
self.assertEqual(200, response.status_code) self.assertEqual(200, response.status_code)
def test_admin_details_override_organiser_allowed(self): def test_admin_details_override_organiser_allowed(self):
self._add_override_organiser_permission() self._add_override_organiser_permission()
self.event.organiser = self.committee
self.event.save()
response = self.client.get('/events/admin/1/') response = self.client.get('/events/admin/1/')
self.assertEqual(200, response.status_code) self.assertEqual(200, response.status_code)
def test_modeladmin_change_no_organiser_allowed(self):
response = self.client.get('/admin/events/event/1/change/')
self.assertEqual(200, response.status_code)
def test_modeladmin_change_organiser_allowed(self): def test_modeladmin_change_organiser_allowed(self):
"""Test the ModelAdmin change page """Change event as an organiser
If I'm an organiser I should be allowed access If I'm an organiser I should be allowed access
""" """
self.event.organiser = self.committee
CommitteeMembership.objects.create( CommitteeMembership.objects.create(
member=self.member, member=self.member,
committee=self.committee) committee=self.committee)
self.event.save()
response = self.client.get('/admin/events/event/1/change/') response = self.client.get('/admin/events/event/1/change/')
self.assertEqual(200, response.status_code) self.assertEqual(200, response.status_code)
def test_modeladmin_change_override_organiser_allowed(self): def test_modeladmin_change_override_organiser_allowed(self):
"""Test the ModelAdmin change page """Test the override organiser permission for changing events
If I'm allowed to override organiser restrictions.. If I'm allowed to override organiser restrictions..
""" """
self._add_override_organiser_permission() self._add_override_organiser_permission()
self.event.organiser = self.committee
self.event.save()
response = self.client.get('/admin/events/event/1/change/') response = self.client.get('/admin/events/event/1/change/')
self.assertEqual(200, response.status_code) self.assertEqual(200, response.status_code)
def test_modeladmin_change_organiser_no_permissions_denied(self): def test_modeladmin_change_organiser_no_permissions_denied(self):
"""Test the ModelAdmin change page """Committee members without change permissions are banned
If I'm an organiser, but don't have perms I should not If I'm an organiser, but don't have perms I should not
be allowed access be allowed access
""" """
self._remove_event_permission() self._remove_event_permission()
self.event.organiser = self.committee
CommitteeMembership.objects.create( CommitteeMembership.objects.create(
member=self.member, member=self.member,
committee=self.committee) committee=self.committee)
self.event.save()
response = self.client.get('/admin/events/event/1/change/') response = self.client.get('/admin/events/event/1/change/')
self.assertEqual(403, response.status_code) self.assertEqual(403, response.status_code)
def test_modeladmin_change_superuser_allowed(self): def test_modeladmin_change_superuser_allowed(self):
"""Test the ModelAdmin change page """Superuser should be allowed access always"""
If I'm an organiser I should be allowed access
"""
self.event.organiser = self.committee
self.event.save()
self.member.user.is_superuser = True self.member.user.is_superuser = True
self.member.user.save() self.member.user.save()
response = self.client.get('/admin/events/event/1/change/') response = self.client.get('/admin/events/event/1/change/')
self.assertEqual(200, response.status_code) self.assertEqual(200, response.status_code)
def test_modeladmin_change_organiser_denied(self): def test_modeladmin_change_organiser_denied(self):
"""Test the ModelAdmin change page """If I'm not an organiser I should not be allowed access"""
If I'm not an organiser I should not be allowed access
"""
self.event.organiser = self.committee
self.event.save()
response = self.client.get('/admin/events/event/1/change/') response = self.client.get('/admin/events/event/1/change/')
self.assertEqual(403, response.status_code) self.assertEqual(403, response.status_code)
...@@ -154,12 +124,13 @@ class AdminTest(TestCase): ...@@ -154,12 +124,13 @@ class AdminTest(TestCase):
class RegistrationTest(TestCase): class RegistrationTest(TestCase):
"""Tests for registration view""" """Tests for registration view"""
fixtures = ['members.json'] fixtures = ['members.json', 'committees.json']
@classmethod @classmethod
def setUpTestData(cls): def setUpTestData(cls):
cls.event = Event.objects.create( cls.event = Event.objects.create(
pk=1, pk=1,
organiser=Committee.objects.get(pk=1),
title_nl='testevene', title_nl='testevene',
title_en='testevent', title_en='testevent',
description_en='desc', description_en='desc',
......
...@@ -24,7 +24,7 @@ from .models import Event, Registration, RegistrationInformationField ...@@ -24,7 +24,7 @@ from .models import Event, Registration, RegistrationInformationField
def admin_details(request, event_id): def admin_details(request, event_id):
event = get_object_or_404(Event, pk=event_id) event = get_object_or_404(Event, pk=event_id)
if (not request.user.is_superuser and event.organiser is not None and if (not request.user.is_superuser and
not request.user.has_perm('events.override_organiser')): not request.user.has_perm('events.override_organiser')):
committees = request.user.member.get_committees().filter( committees = request.user.member.get_committees().filter(
pk=event.organiser.pk).count() pk=event.organiser.pk).count()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment