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

pizzas: use User for foreign keys

parent a8ba0374
......@@ -47,10 +47,10 @@ class OrderViewset(ModelViewSet):
if not event or event.has_ended:
return Order.objects.none()
return Order.objects.filter(member=self.request.user.member,
return Order.objects.filter(member=self.request.user,
paid=False,
pizza_event=event)
return Order.objects.filter(member=self.request.user.member,
return Order.objects.filter(member=self.request.user,
pizza_event=event)
def get_serializer_class(self):
......@@ -61,7 +61,7 @@ class OrderViewset(ModelViewSet):
def get_object(self):
if self.kwargs[self.lookup_field] == 'me':
order = get_object_or_404(self.get_queryset(),
member=self.request.user.member,
member=self.request.user,
pizza_event=PizzaEvent.current())
self.check_object_permissions(self.request, order)
return order
......@@ -72,7 +72,7 @@ class OrderViewset(ModelViewSet):
if serializer.validated_data.get('name'):
serializer.save(pizza_event=PizzaEvent.current())
else:
serializer.save(member=self.request.user.member,
serializer.save(member=self.request.user,
pizza_event=PizzaEvent.current())
except IntegrityError:
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.db import models
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
import events
import members
from utils.translation import ModelTranslateMeta, MultilingualField
......@@ -66,7 +66,7 @@ class Product(models.Model, metaclass=ModelTranslateMeta):
class Order(models.Model):
member = models.ForeignKey(
members.models.Member,
User,
on_delete=models.CASCADE,
blank=True,
null=True,
......@@ -103,4 +103,4 @@ class Order(models.Model):
return not self.paid and not self.pizza_event.has_ended
class Meta:
unique_together = ('pizza_event', 'member')
unique_together = ('pizza_event', 'member',)
......@@ -16,7 +16,7 @@ def index(request):
event = PizzaEvent.current()
try:
order = Order.objects.get(pizza_event=event,
member=request.user.member)
member=request.user)
except Order.DoesNotExist:
order = None
context = {'event': event, 'products': products, 'order': order}
......@@ -29,7 +29,7 @@ def orders(request, event_pk):
context = {'event': event,
'orders': Order.objects.filter(pizza_event=event)
.prefetch_related('member', 'product')
.order_by('member__user__first_name')}
.order_by('member__first_name')}
return render(request, 'pizzas/orders.html', context)
......@@ -105,7 +105,7 @@ def cancel_order(request):
if not order.can_be_changed:
messages.error(request,
_('You can no longer cancel.'))
elif order.member == request.user.member:
elif order.member == request.user:
order.delete()
messages.success(request, _("Your order has been cancelled."))
except Http404:
......@@ -138,7 +138,7 @@ def order(request):
try:
order_placed = Order.objects.get(pizza_event=event,
member=request.user.member)
member=request.user)
current_order_locked = not order_placed.can_be_changed
except Order.DoesNotExist:
current_order_locked = False
......@@ -148,9 +148,9 @@ def order(request):
if product:
try:
order = Order.objects.get(pizza_event=event,
member=request.user.member)
member=request.user)
except Order.DoesNotExist:
order = Order(pizza_event=event, member=request.user.member)
order = Order(pizza_event=event, member=request.user)
order.product = product
order.save()
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