Commit a8ba0374 authored by Tom van Bussel's avatar Tom van Bussel

events: use User for foreign keys

parent 114140de
# -*- coding: utf-8 -*-
from django.contrib import admin
from django.contrib.auth.models import User
from django.http import HttpResponseRedirect
from django.template.defaultfilters import date as _date
from django.urls import reverse
......@@ -72,12 +73,9 @@ class EventAdmin(DoNextModelAdmin):
title=obj.title)
def has_change_permission(self, request, event=None):
try:
if (event is not None and
not services.is_organiser(request.user, event)):
return False
except Member.DoesNotExist:
pass
if (event is not None and
not services.is_organiser(request.user, event)):
return False
return super().has_change_permission(request, event)
def event_date(self, obj):
......@@ -181,5 +179,6 @@ class RegistrationAdmin(DoNextModelAdmin):
kwargs['queryset'] = models.Event.objects.filter(
pk=int(request.GET['event_pk']))
elif db_field.name == 'member':
kwargs['queryset'] = Member.active_members.all()
# TODO: Member.active_members.all()
kwargs['queryset'] = User.objects.all()
return super().formfield_for_foreignkey(db_field, request, **kwargs)
......@@ -95,10 +95,10 @@ def export(request, event_id):
_('Date'): timezone.localtime(
registration.date).strftime("%Y-%m-%d %H:%m"),
_('Present'): _('Yes') if registration.present else '',
_('Phone number'): (registration.member.phone_number
_('Phone number'): (registration.member.member.phone_number
if registration.member
else ''),
_('Email'): (registration.member.user.email
_('Email'): (registration.member.email
if registration.member
else ''),
_('Status'): status,
......@@ -142,9 +142,9 @@ def export(request, event_id):
def export_email(request, event_id):
event = get_object_or_404(Event, pk=event_id)
registrations = event.registration_set.filter(
date_cancelled=None).prefetch_related('member__user')
date_cancelled=None)
registrations = registrations[:event.max_participants]
addresses = [r.member.user.email for r in registrations if r.member]
addresses = [r.member.email for r in registrations if r.member]
no_addresses = [r.name for r in registrations if not r.member]
return render(request, 'events/admin/email_export.html',
{'event': event, 'addresses': addresses,
......
......@@ -155,16 +155,16 @@ class EventRetrieveSerializer(serializers.ModelSerializer):
def _user_registration(self, instance):
try:
reg = instance.registration_set.get(
member=self.context['request'].user.member)
member=self.context['request'].user)
return RegistrationListSerializer(reg, context=self.context).data
except Registration.DoesNotExist:
return None
def _registration_allowed(self, instance):
member = self.context['request'].user.member
return (member is not None and
member.current_membership is not None and
member.can_attend_events)
user = self.context['request'].user
return (user.member is not None and
user.member.current_membership is not None and
user.member.can_attend_events)
def _has_fields(self, instance):
return instance.has_fields()
......@@ -223,18 +223,18 @@ class RegistrationListSerializer(serializers.ModelSerializer):
def _member(self, instance):
if instance.member:
return instance.member.user.pk
return instance.member.pk
return None
def _name(self, instance):
if instance.member:
return instance.member.display_name()
return instance.member.member.display_name()
return instance.name
def _photo(self, instance):
if instance.member and instance.member.photo:
if instance.member and instance.member.member.photo:
return self.context['request'].build_absolute_uri(
'%s%s' % (settings.MEDIA_URL, instance.member.photo))
'%s%s' % (settings.MEDIA_URL, instance.member.member.photo))
else:
return self.context['request'].build_absolute_uri(
static('members/images/default-avatar.jpg'))
......@@ -278,11 +278,11 @@ class RegistrationSerializer(serializers.ModelSerializer):
def _name(self, instance):
if instance.member:
return instance.member.display_name()
return instance.member.member.display_name()
return instance.name
def _photo(self, instance):
if instance.member and instance.member.photo:
if instance.member and instance.member.member.photo:
return self.context['request'].build_absolute_uri(
'%s%s' % (settings.MEDIA_URL, instance.member.photo))
else:
......@@ -294,7 +294,7 @@ class RegistrationSerializer(serializers.ModelSerializer):
try:
if instance:
self.information_fields = services.registration_fields(
instance.member.user, instance.event)
instance.member, instance.event)
except RegistrationError:
pass
......
......@@ -138,7 +138,7 @@ class RegistrationViewSet(GenericViewSet, RetrieveModelMixin,
def get_object(self):
instance = super().get_object()
if (instance.member.user.pk != self.request.user.pk and
if (instance.member.pk != self.request.user.pk and
not services.is_organiser(self.request.user,
instance.event)):
raise NotFound()
......@@ -154,17 +154,17 @@ class RegistrationViewSet(GenericViewSet, RetrieveModelMixin,
def perform_update(self, serializer):
super().perform_update(serializer)
registration = serializer.instance
services.update_registration(registration.member.user,
services.update_registration(registration.member,
registration.event,
serializer.field_values())
serializer.information_fields = services.registration_fields(
registration.member.user, registration.event)
registration.member, registration.event)
def destroy(self, request, pk=None, **kwargs):
registration = self.get_object()
try:
services.cancel_registration(request,
registration.member.user,
registration.member,
registration.event)
return Response(status=204)
except RegistrationError as e:
......
......@@ -4,6 +4,7 @@ from django.utils import translation
from django.utils.translation import ugettext_lazy as _
from events.models import Registration
from members.models import Member
from thaliawebsite.templatetags import baseurl
......@@ -20,7 +21,12 @@ def notify_first_waiting(request, event):
text_template = get_template('events/member_email.txt')
with translation.override(first_waiting_member.language):
if first_waiting_member.member:
language = first_waiting_member.member.language
else:
language = Member._meta.get_field('language').default
with translation.override(language):
subject = _("[THALIA] Notification about your "
"registration for '{}'").format(
event.title)
......@@ -34,7 +40,7 @@ def notify_first_waiting(request, event):
EmailMessage(
subject,
text_message,
to=[first_waiting_member.user.email]
to=[first_waiting_member.email]
).send()
......
# -*- coding: utf-8 -*-
# Generated by Django 1.11 on 2017-10-12 08:31
from __future__ import unicode_literals
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('events', '0019_event_send_cancel_email'),
]
operations = [
migrations.AlterUniqueTogether(
name='registration',
unique_together=set(),
),
migrations.AddField(
model_name='registration',
name='member_old',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='members.Member'),
),
]
# -*- coding: utf-8 -*-
# Generated by Django 1.11 on 2017-10-12 08:31
from __future__ import unicode_literals
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
def forward_func(apps, schema_editor):
Registration = apps.get_model('events', 'Registration')
for registration in Registration.objects.all():
if registration.member:
registration.member_old = registration.member
registration.save(update_fields=('member_old',))
class Migration(migrations.Migration):
dependencies = [
('events', '0020_0_user_foreign_keys'),
]
operations = [
migrations.RunPython(
code=forward_func,
),
]
# -*- coding: utf-8 -*-
# Generated by Django 1.11 on 2017-10-12 08:31
from __future__ import unicode_literals
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('events', '0020_1_user_foreign_keys'),
]
operations = [
migrations.RemoveField(
model_name='registration',
name='member',
),
migrations.AddField(
model_name='registration',
name='member',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
),
]
# -*- coding: utf-8 -*-
# Generated by Django 1.11 on 2017-10-12 08:31
from __future__ import unicode_literals
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
def forward_func(apps, schema_editor):
Registration = apps.get_model('events', 'Registration')
for registration in Registration.objects.all():
if registration.member_old:
registration.member = registration.member_old.user
registration.save(update_fields=('member',))
class Migration(migrations.Migration):
dependencies = [
('events', '0020_2_user_foreign_keys'),
]
operations = [
migrations.RunPython(
code=forward_func,
),
]
# -*- coding: utf-8 -*-
# Generated by Django 1.11 on 2017-10-12 08:31
from __future__ import unicode_literals
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('events', '0020_3_user_foreign_keys'),
]
operations = [
migrations.RemoveField(
model_name='registration',
name='member_old',
),
migrations.AlterUniqueTogether(
name='registration',
unique_together=set([('member', 'event', 'name', 'date_cancelled')]),
),
]
from django.contrib.auth.models import User
from django.core import validators
from django.core.exceptions import ValidationError
from django.db import models
......@@ -273,11 +274,11 @@ class Registration(models.Model):
event = models.ForeignKey(Event, models.CASCADE)
member = models.ForeignKey(
'members.Member', models.CASCADE,
User, models.CASCADE,
blank=True,
null=True,
limit_choices_to=(Q(user__membership__until__isnull=True) |
Q(user__membership__until__gt=timezone.now().date()))
limit_choices_to=(Q(membership__until__isnull=True) |
Q(membership__until__gt=timezone.now().date()))
)
name = models.CharField(
......
......@@ -11,7 +11,7 @@ def is_user_registered(event, user):
return None
return event.registrations.filter(
member=user.member,
member=user,
date_cancelled=None).count() > 0
......@@ -26,7 +26,7 @@ def event_permissions(user, event):
try:
registration = Registration.objects.get(
event=event,
member=user.member
member=user
)
except Registration.DoesNotExist:
pass
......@@ -66,7 +66,7 @@ def create_registration(user, event):
try:
registration = Registration.objects.get(
event=event,
member=user.member
member=user
)
except Registration.DoesNotExist:
pass
......@@ -74,7 +74,7 @@ def create_registration(user, event):
if registration is None:
return Registration.objects.create(
event=event,
member=user.member
member=user
)
elif registration.date_cancelled is not None:
if registration.is_late_cancellation():
......@@ -98,7 +98,7 @@ def cancel_registration(request, user, event):
try:
registration = Registration.objects.get(
event=event,
member=user.member
member=user
)
except Registration.DoesNotExist:
pass
......@@ -126,7 +126,7 @@ def update_registration(user, event, field_values):
try:
registration = Registration.objects.get(
event=event,
member=user.member
member=user
)
except Registration.DoesNotExist:
pass
......@@ -159,7 +159,7 @@ def registration_fields(user, event):
try:
registration = Registration.objects.get(
event=event,
member=user.member
member=user
)
except Registration.DoesNotExist:
pass
......
......@@ -26,7 +26,7 @@
{% for registration in registrations %}
<tr class="{% if forloop.first %}row1 {% endif %}">
{% if registration.member %}
<td><a href="{{ registration.member.get_absolute_url }}">{{ registration.member.get_full_name }}</a></td>
<td><a href="{{ registration.member.member.get_absolute_url }}">{{ registration.member.get_full_name }}</a></td>
{% else %}
<td>{{ registration.name }}</td>
{% endif %}
......
......@@ -195,23 +195,23 @@
</div>
</a>
{% else %}
<a href="{% url 'members:profile' pk=registration.member.user.pk %}">
<a href="{% url 'members:profile' pk=registration.member.pk %}">
<div class="post-inner">
<div class="inner-img">
{% if not registration.member.photo %}
<img alt="{{ member.display_name }}" src="{% static 'members/images/default-avatar.jpg' %}" height="220" width="220" />
{% if not registration.member.member.photo %}
<img alt="{{ registration.member.member.display_name }}" src="{% static 'members/images/default-avatar.jpg' %}" height="220" width="220" />
{% else %}
<img alt="{{ member.display_name }}" src="{% thumbnail registration.member.photo '220x220' %}" width="220" height="220" />
<img alt="{{ registration.member.member.display_name }}" src="{% thumbnail registration.member.member.photo '220x220' %}" width="220" height="220" />
{% endif %}
</div>
<div class="post-overlay">
<div class="post-overlay-meta">
<h2>{{ registration.member.display_name }}</h2>
<p>{% trans "cohort"|capfirst %}: {{ registration.member.starting_year }}</p>
<h2>{{ registration.member.member.display_name }}</h2>
<p>{% trans "cohort"|capfirst %}: {{ registration.member.member.starting_year }}</p>
</div>
</div>
<div class="post-body avatar-subtitle">
{{ registration.member.display_name }}
{{ registration.member.member.display_name }}
</div>
</div>
</a>
......
{% load i18n %}{% blocktrans with event_title=event.title baseurl=base_url event_url=event.get_absolute_url registration_date=registration.date|date:"SHORT_DATETIME_FORMAT" cancel_deadline=event.cancel_deadline|date:"SHORT_DATETIME_FORMAT" name=member.user.first_name %}Hi {{ name }},
{% load i18n %}{% blocktrans with event_title=event.title baseurl=base_url event_url=event.get_absolute_url registration_date=registration.date|date:"SHORT_DATETIME_FORMAT" cancel_deadline=event.cancel_deadline|date:"SHORT_DATETIME_FORMAT" name=member.first_name %}Hi {{ name }},
You registered for the event '{{ event_title }}' on {{ registration_date }} and unfortunately you were placed on the waiting list.
However someone just unregistered and we would like to let you know that you'll be able to attend now!
......
......@@ -2,5 +2,5 @@
A member that was registered for the event '{{ event.title }}' that you're organising has cancelled their registration after the deadline.
Name: {{ registration.member.display_name }}
Name: {{ registration.member.member.display_name }}
Registration date: {{ registration.date|date:"SHORT_DATETIME_FORMAT" }}
import datetime
from django.contrib.auth.models import User
from django.test import TestCase
from django.utils import timezone
from rest_framework.test import APIClient
......@@ -10,7 +11,6 @@ from events.models import (Event, Registration,
BooleanRegistrationInformation,
IntegerRegistrationInformation,
TextRegistrationInformation)
from members.models import Member
class RegistrationApiTest(TestCase):
......@@ -35,11 +35,11 @@ class RegistrationApiTest(TestCase):
map_location='test map location',
price=0.00,
fine=0.00)
cls.member = Member.objects.filter(user__last_name="Wiggers").first()
cls.member = User.objects.filter(last_name="Wiggers").first()
def setUp(self):
self.client = APIClient()
self.client.force_login(self.member.user)
self.client.force_login(self.member)
def test_registration_register_not_required(self):
response = self.client.post('/api/v1/events/1/registrations/',
......
import datetime
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from django.test import TestCase
from django.utils import timezone
......@@ -7,7 +8,6 @@ from django.utils import timezone
from activemembers.models import Committee
from events.models import Event, Registration
from mailinglists.models import MailingList
from members.models import Member
class EventTest(TestCase):
......@@ -40,7 +40,7 @@ class EventTest(TestCase):
map_location='test map location',
price=0.00,
fine=5.00)
cls.member = Member.objects.all()[0]
cls.member = User.objects.all()[0]
def setUp(self):
self.mailinglist.refresh_from_db()
......@@ -281,7 +281,7 @@ class RegistrationTest(TestCase):
map_location='test map location',
price=0.00,
fine=0.00)
cls.member = Member.objects.all()[0]
cls.member = User.objects.all()[0]
cls.r1 = Registration.objects.create(event=cls.event,
member=cls.member)
cls.r2 = Registration.objects.create(event=cls.event,
......
import datetime
from django.contrib.auth.models import Permission
from django.contrib.auth.models import Permission, User
from django.core import mail
from django.test import Client, TestCase
from django.utils import timezone
......@@ -12,7 +12,6 @@ from events.models import (Event, Registration,
IntegerRegistrationInformation,
TextRegistrationInformation)
from mailinglists.models import MailingList
from members.models import Member
class AdminTest(TestCase):
......@@ -38,26 +37,25 @@ class AdminTest(TestCase):
map_location='test map location',
price=0.00,
fine=0.00)
cls.member = Member.objects.filter(user__last_name="Wiggers").first()
cls.user = cls.member.user
cls.member = User.objects.filter(last_name="Wiggers").first()
cls.permission_change_event = Permission.objects.get(
content_type__model='event',
codename='change_event')
cls.permission_override_orga = Permission.objects.get(
content_type__model='event',
codename='override_organiser')
cls.member.user.user_permissions.add(cls.permission_change_event)
cls.member.user.is_superuser = False
cls.member.user.save()
cls.member.user_permissions.add(cls.permission_change_event)
cls.member.is_superuser = False
cls.member.save()
def setUp(self):
self.client.force_login(self.member.user)
self.client.force_login(self.member)
def _remove_event_permission(self):
self.member.user.user_permissions.remove(self.permission_change_event)
self.member.user_permissions.remove(self.permission_change_event)
def _add_override_organiser_permission(self):
self.member.user.user_permissions.add(self.permission_override_orga)
self.member.user_permissions.add(self.permission_override_orga)
def test_admin_details_need_change_event_access(self):
"""I need the event.change_event permission to do stuff"""
......@@ -72,7 +70,7 @@ class AdminTest(TestCase):
def test_admin_details_organiser_allowed(self):
CommitteeMembership.objects.create(
member=self.user,
member=self.member,
committee=self.committee)
response = self.client.get('/events/admin/1/')
self.assertEqual(200, response.status_code)
......@@ -88,7 +86,7 @@ class AdminTest(TestCase):
If I'm an organiser I should be allowed access
"""
CommitteeMembership.objects.create(
member=self.user,
member=self.member,
committee=self.committee)
response = self.client.get('/admin/events/event/1/change/')
self.assertEqual(200, response.status_code)
......@@ -110,15 +108,15 @@ class AdminTest(TestCase):
"""
self._remove_event_permission()
CommitteeMembership.objects.create(
member=self.user,
member=self.member,
committee=self.committee)
response = self.client.get('/admin/events/event/1/change/')
self.assertEqual(403, response.status_code)
def test_modeladmin_change_superuser_allowed(self):
"""Superuser should be allowed access always"""
self.member.user.is_superuser = True
self.member.user.save()
self.member.is_superuser = True
self.member.save()
response = self.client.get('/admin/events/event/1/change/')
self.assertEqual(200, response.status_code)
......@@ -158,11 +156,11 @@ class RegistrationTest(TestCase):
map_location='test map location',
price=0.00,
fine=0.00)
cls.member = Member.objects.filter(user__last_name="Wiggers").first()
cls.member = User.objects.filter(last_name="Wiggers").first()
def setUp(self):
self.client = Client()
self.client.force_login(self.member.user)
self.client.force_login(self.member)
def test_registration_register_not_required(self):
response = self.client.post('/events/1/registration/register/',
......
......@@ -46,7 +46,7 @@ class EventDetail(DetailView):
try:
context['registration'] = Registration.objects.get(
event=event,
member=self.request.user.member
member=self.request.user
)
except (Registration.DoesNotExist, AttributeError):
pass
......
Markdown is supported
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