Verified Commit f1e48bba authored by Sébastiaan Versteeg's avatar Sébastiaan Versteeg
Browse files

Make it possible for pizza admins to create orders for users other than themselves

parent 7e908c53
from django.utils.translation import ugettext_lazy as _
from rest_framework import serializers from rest_framework import serializers
from rest_framework.exceptions import ValidationError
from pizzas.models import Product, PizzaEvent, Order from pizzas.models import Product, PizzaEvent, Order
...@@ -29,3 +31,13 @@ class AdminOrderSerializer(serializers.ModelSerializer): ...@@ -29,3 +31,13 @@ class AdminOrderSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Order model = Order
fields = ('pk', 'paid', 'product', 'name', 'member') fields = ('pk', 'paid', 'product', 'name', 'member')
def validate(self, attrs):
if attrs['member'] and attrs['name']:
raise ValidationError({
'member': _('Either specify a member or a name'),
'name': _('Either specify a member or a name'),
})
if not (attrs['member'] or attrs['name']):
attrs['member'] = self.context['request'].member
return super().validate(attrs)
...@@ -72,7 +72,10 @@ class OrderViewset(ModelViewSet): ...@@ -72,7 +72,10 @@ 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.member, if self.request.user.has_perm('pizzas.change_order'):
pizza_event=PizzaEvent.current()) serializer.save(pizza_event=PizzaEvent.current())
else:
serializer.save(member=self.request.member,
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')
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