Commit bed79e25 authored by Tom van Bussel's avatar Tom van Bussel
Browse files

pizzas: use User for foreign keys

parent a8ba0374
...@@ -47,10 +47,10 @@ class OrderViewset(ModelViewSet): ...@@ -47,10 +47,10 @@ class OrderViewset(ModelViewSet):
if not event or event.has_ended: if not event or event.has_ended:
return Order.objects.none() return Order.objects.none()
return Order.objects.filter(member=self.request.user.member, return Order.objects.filter(member=self.request.user,
paid=False, paid=False,
pizza_event=event) pizza_event=event)
return Order.objects.filter(member=self.request.user.member, return Order.objects.filter(member=self.request.user,
pizza_event=event) pizza_event=event)
def get_serializer_class(self): def get_serializer_class(self):
...@@ -61,7 +61,7 @@ class OrderViewset(ModelViewSet): ...@@ -61,7 +61,7 @@ class OrderViewset(ModelViewSet):
def get_object(self): def get_object(self):
if self.kwargs[self.lookup_field] == 'me': if self.kwargs[self.lookup_field] == 'me':
order = get_object_or_404(self.get_queryset(), order = get_object_or_404(self.get_queryset(),
member=self.request.user.member, member=self.request.user,
pizza_event=PizzaEvent.current()) pizza_event=PizzaEvent.current())
self.check_object_permissions(self.request, order) self.check_object_permissions(self.request, order)
return order return order
...@@ -72,7 +72,7 @@ class OrderViewset(ModelViewSet): ...@@ -72,7 +72,7 @@ class OrderViewset(ModelViewSet):
if serializer.validated_data.get('name'): if serializer.validated_data.get('name'):
serializer.save(pizza_event=PizzaEvent.current()) serializer.save(pizza_event=PizzaEvent.current())
else: else:
serializer.save(member=self.request.user.member, serializer.save(member=self.request.user,
pizza_event=PizzaEvent.current()) pizza_event=PizzaEvent.current())
except IntegrityError: except IntegrityError:
raise ValidationError('Something went wrong when saving the order') raise ValidationError('Something went wrong when saving the order')
# -*- coding: utf-8 -*-
# Generated by Django 1.11 on 2017-10-12 09:59
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 = [
('pizzas', '0002_auto_20170120_0837'),
]
operations = [
migrations.AlterUniqueTogether(
name='order',
unique_together=set(),
),
migrations.AddField(
model_name='order',
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 09:59
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):
Order = apps.get_model('pizzas', 'Order')
for order in Order.objects.all():
if order.member:
order.member_old = order.member
order.save(update_fields=('member_old',))
class Migration(migrations.Migration):
dependencies = [
('pizzas', '0003_0_user_foreign_keys'),
]
operations = [
migrations.RunPython(
code=forward_func,
),
]
# -*- coding: utf-8 -*-
# Generated by Django 1.11 on 2017-10-12 09:59
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 = [
('pizzas', '0003_1_user_foreign_keys'),
]
operations = [
migrations.RemoveField(
model_name='order',
name='member',
),
migrations.AddField(
model_name='order',
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 09:59
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):
Order = apps.get_model('pizzas', 'Order')
for order in Order.objects.all():
if order.member_old:
order.member = order.member_old.user
order.save(update_fields=('member',))
class Migration(migrations.Migration):
dependencies = [
('pizzas', '0003_2_user_foreign_keys'),
]
operations = [
migrations.RunPython(
code=forward_func,
),
]
# -*- coding: utf-8 -*-
# Generated by Django 1.11 on 2017-10-12 09:59
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 = [
('pizzas', '0003_3_user_foreign_keys'),
]
operations = [
migrations.RemoveField(
model_name='order',
name='member_old',
),
migrations.AlterUniqueTogether(
name='order',
unique_together=set([('pizza_event', 'member')]),
),
]
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.db import models from django.db import models
from django.utils import timezone from django.utils import timezone
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
import events import events
import members
from utils.translation import ModelTranslateMeta, MultilingualField from utils.translation import ModelTranslateMeta, MultilingualField
...@@ -66,7 +66,7 @@ class Product(models.Model, metaclass=ModelTranslateMeta): ...@@ -66,7 +66,7 @@ class Product(models.Model, metaclass=ModelTranslateMeta):
class Order(models.Model): class Order(models.Model):
member = models.ForeignKey( member = models.ForeignKey(
members.models.Member, User,
on_delete=models.CASCADE, on_delete=models.CASCADE,
blank=True, blank=True,
null=True, null=True,
...@@ -103,4 +103,4 @@ class Order(models.Model): ...@@ -103,4 +103,4 @@ class Order(models.Model):
return not self.paid and not self.pizza_event.has_ended return not self.paid and not self.pizza_event.has_ended
class Meta: class Meta:
unique_together = ('pizza_event', 'member') unique_together = ('pizza_event', 'member',)
...@@ -16,7 +16,7 @@ def index(request): ...@@ -16,7 +16,7 @@ def index(request):
event = PizzaEvent.current() event = PizzaEvent.current()
try: try:
order = Order.objects.get(pizza_event=event, order = Order.objects.get(pizza_event=event,
member=request.user.member) member=request.user)
except Order.DoesNotExist: except Order.DoesNotExist:
order = None order = None
context = {'event': event, 'products': products, 'order': order} context = {'event': event, 'products': products, 'order': order}
...@@ -29,7 +29,7 @@ def orders(request, event_pk): ...@@ -29,7 +29,7 @@ def orders(request, event_pk):
context = {'event': event, context = {'event': event,
'orders': Order.objects.filter(pizza_event=event) 'orders': Order.objects.filter(pizza_event=event)
.prefetch_related('member', 'product') .prefetch_related('member', 'product')
.order_by('member__user__first_name')} .order_by('member__first_name')}
return render(request, 'pizzas/orders.html', context) return render(request, 'pizzas/orders.html', context)
...@@ -105,7 +105,7 @@ def cancel_order(request): ...@@ -105,7 +105,7 @@ def cancel_order(request):
if not order.can_be_changed: if not order.can_be_changed:
messages.error(request, messages.error(request,
_('You can no longer cancel.')) _('You can no longer cancel.'))
elif order.member == request.user.member: elif order.member == request.user:
order.delete() order.delete()
messages.success(request, _("Your order has been cancelled.")) messages.success(request, _("Your order has been cancelled."))
except Http404: except Http404:
...@@ -138,7 +138,7 @@ def order(request): ...@@ -138,7 +138,7 @@ def order(request):
try: try:
order_placed = Order.objects.get(pizza_event=event, order_placed = Order.objects.get(pizza_event=event,
member=request.user.member) member=request.user)
current_order_locked = not order_placed.can_be_changed current_order_locked = not order_placed.can_be_changed
except Order.DoesNotExist: except Order.DoesNotExist:
current_order_locked = False current_order_locked = False
...@@ -148,9 +148,9 @@ def order(request): ...@@ -148,9 +148,9 @@ def order(request):
if product: if product:
try: try:
order = Order.objects.get(pizza_event=event, order = Order.objects.get(pizza_event=event,
member=request.user.member) member=request.user)
except Order.DoesNotExist: except Order.DoesNotExist:
order = Order(pizza_event=event, member=request.user.member) order = Order(pizza_event=event, member=request.user)
order.product = product order.product = product
order.save() order.save()
return HttpResponseRedirect(reverse('pizzas:index')) return HttpResponseRedirect(reverse('pizzas:index'))
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