Events are configurable
This commit mostly reproduces the structure of gestioncof's events, renames some stuff and adds a generic export view.
This commit is contained in:
parent
6e9dc03bc7
commit
d5e9d09044
4 changed files with 407 additions and 14 deletions
|
@ -1,3 +1,4 @@
|
|||
import csv
|
||||
from unittest import mock
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
|
@ -5,7 +6,14 @@ from django.contrib.auth.models import Permission
|
|||
from django.test import Client, TestCase
|
||||
from django.urls import reverse
|
||||
|
||||
from events.models import Event
|
||||
from events.models import (
|
||||
Event,
|
||||
ExtraField,
|
||||
ExtraFieldContent,
|
||||
Option,
|
||||
OptionChoice,
|
||||
Registration,
|
||||
)
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
|
@ -23,7 +31,7 @@ def make_staff_user(name):
|
|||
return user
|
||||
|
||||
|
||||
class CSVExportTest(TestCase):
|
||||
class MessagePatch:
|
||||
def setUp(self):
|
||||
# Signals handlers on login/logout send messages.
|
||||
# Due to the way the Django' test Client performs login, this raise an
|
||||
|
@ -32,11 +40,14 @@ class CSVExportTest(TestCase):
|
|||
patcher_messages.start()
|
||||
self.addCleanup(patcher_messages.stop)
|
||||
|
||||
|
||||
class CSVExportAccessTest(MessagePatch, TestCase):
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
|
||||
self.staff = make_staff_user("staff")
|
||||
self.u1 = make_user("toto")
|
||||
self.u2 = make_user("titi")
|
||||
self.event = Event.objects.create(title="test_event", location="somewhere")
|
||||
self.event.subscribers.set([self.u1, self.u2])
|
||||
self.url = reverse("events:csv-participants", args=[self.event.id])
|
||||
|
||||
def test_get(self):
|
||||
|
@ -57,3 +68,68 @@ class CSVExportTest(TestCase):
|
|||
client.force_login(self.u1)
|
||||
r = client.get(self.url)
|
||||
self.assertEqual(r.status_code, 403)
|
||||
|
||||
|
||||
class CSVExportContentTest(MessagePatch, TestCase):
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
|
||||
self.event = Event.objects.create(title="test_event", location="somewhere")
|
||||
self.url = reverse("events:csv-participants", args=[self.event.id])
|
||||
|
||||
self.u1 = User.objects.create_user(
|
||||
username="toto_foo", first_name="toto", last_name="foo", email="toto@a.b"
|
||||
)
|
||||
self.u2 = User.objects.create_user(
|
||||
username="titi_bar", first_name="titi", last_name="bar", email="titi@a.b"
|
||||
)
|
||||
self.staff = make_staff_user("staff")
|
||||
self.client = Client()
|
||||
self.client.force_login(self.staff)
|
||||
|
||||
def test_simple_event(self):
|
||||
self.event.subscribers.set([self.u1, self.u2])
|
||||
|
||||
participants = self.client.get(self.url).content.decode("utf-8")
|
||||
participants = [
|
||||
line for line in csv.reader(participants.split("\n")) if line != []
|
||||
]
|
||||
self.assertEqual(len(participants), 3)
|
||||
self.assertEqual(participants[1], ["toto_foo", "toto@a.b", "toto", "foo"])
|
||||
self.assertEqual(participants[2], ["titi_bar", "titi@a.b", "titi", "bar"])
|
||||
|
||||
def test_complex_event(self):
|
||||
registration = Registration.objects.create(event=self.event, user=self.u1)
|
||||
# Set up some options
|
||||
option1 = Option.objects.create(
|
||||
event=self.event, name="abc", multi_choices=False
|
||||
)
|
||||
option2 = Option.objects.create(
|
||||
event=self.event, name="def", multi_choices=True
|
||||
)
|
||||
OptionChoice.objects.bulk_create(
|
||||
[
|
||||
OptionChoice(option=option1, choice="a"),
|
||||
OptionChoice(option=option1, choice="b"),
|
||||
OptionChoice(option=option1, choice="c"),
|
||||
OptionChoice(option=option2, choice="d"),
|
||||
OptionChoice(option=option2, choice="e"),
|
||||
OptionChoice(option=option2, choice="f"),
|
||||
]
|
||||
)
|
||||
registration.options_choices.set(
|
||||
OptionChoice.objects.filter(choice__in=["d", "f"])
|
||||
)
|
||||
registration.options_choices.add(OptionChoice.objects.get(choice="a"))
|
||||
# And an extra field
|
||||
field = ExtraField.objects.create(event=self.event, name="remarks")
|
||||
ExtraFieldContent.objects.create(
|
||||
field=field, registration=registration, content="hello"
|
||||
)
|
||||
|
||||
participants = self.client.get(self.url).content.decode("utf-8")
|
||||
participants = list(csv.reader(participants.split("\n")))
|
||||
self.assertEqual(
|
||||
["toto_foo", "toto@a.b", "toto", "foo", "a", "d & f", "hello"],
|
||||
participants[1],
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue