def test_clean(self): """Check if clean method works""" new = PizzaEvent( event=self.event2, start=self.pizzaEvent.start + datetime.timedelta(minutes=10), end=timezone.now() - datetime.timedelta(hours=100), ) with self.subTest(msg="end before start"): with self.assertRaises(ValidationError): new.clean()
def test_validate_unique(self): """Check if uniqueness validation is correct""" self.pizzaEvent.start = self.pizzaEvent.start + datetime.timedelta( minutes=10) with self.subTest(msg="saving works"): self.pizzaEvent.validate_unique() new = PizzaEvent( event=self.event2, start=self.pizzaEvent.start + datetime.timedelta(minutes=10), end=timezone.now() + datetime.timedelta(hours=100), ) with self.subTest(msg="overlapping event"): with self.assertRaises(ValidationError): new.validate_unique()
def list(self, request, *args, **kwargs): if PizzaEvent.current() or request.user.has_perm( "pizzas.change_product"): queryset = self.get_queryset() if not request.user.has_perm("pizzas.order_restricted_products"): queryset = queryset.exclude(restricted=True) serializer = serializers.PizzaSerializer(queryset, many=True) return Response(serializer.data) raise PermissionDenied
def perform_update(self, serializer): order = serializer.save() if "payment" in serializer.validated_data and can_change_order( self.request.member, PizzaEvent.current()): self._update_payment( order, serializer.validated_data["payment"]["type"], self.request.user, )
def get_object(self): if self.kwargs.get(self.lookup_field) == "me": order = get_object_or_404( self.get_queryset(), member=self.request.member, pizza_event=PizzaEvent.current(), ) self.check_object_permissions(self.request, order) return order return super().get_object()
def event(self, request): event = PizzaEvent.current() if event: context = {"request": request} serializer = serializers.PizzaEventSerializer(event, context=context) return Response(serializer.data) raise NotFound
def perform_create(self, serializer): try: if serializer.validated_data.get("name"): serializer.save(pizza_event=PizzaEvent.current()) else: if can_change_order(self.request.member, PizzaEvent.current()): order = serializer.save(pizza_event=PizzaEvent.current()) if "payment" in serializer.validated_data: payment_type = serializer.validated_data["payment"][ "type"] else: payment_type = Payment.NONE self._update_payment(order, payment_type, self.request.user) else: serializer.save(member=self.request.member, pizza_event=PizzaEvent.current()) except IntegrityError as e: raise ValidationError( "Something went wrong when saving the order" + str(e))
def get_queryset(self): event = PizzaEvent.current() if can_change_order(self.request.member, event): return Order.objects.filter(pizza_event=event) if self.action == "update" or self.action == "destroy": if not event or event.has_ended: return Order.objects.none() return Order.objects.filter( member=self.request.member, payment=None, pizza_event=event, ) return Order.objects.filter(member=self.request.member, pizza_event=event)
def test_current(self): """Test the classmethod that fetches a currently active pizzaevent""" with self.subTest(msg="Single event, active"): self.assertEqual(self.pizzaEvent, PizzaEvent.current()) with self.subTest(mgs="Single event, not active"): self.pizzaEvent.start = timezone.now() + datetime.timedelta( hours=10) self.pizzaEvent.end = timezone.now() + datetime.timedelta( hours=100) self.pizzaEvent.save() self.assertIsNone(PizzaEvent.current()) second_pizzaevent = PizzaEvent.objects.create( start=timezone.now() + datetime.timedelta(hours=100), end=timezone.now() + datetime.timedelta(hours=1000), event=self.event2, ) with self.subTest(msg="two events, not active"): self.assertIsNone(PizzaEvent.current()) self.pizzaEvent.start = timezone.now() - datetime.timedelta(hours=1) self.pizzaEvent.end = timezone.now() + datetime.timedelta(hours=1) self.pizzaEvent.save() with self.subTest(msg="two events, one active"): self.assertEqual(self.pizzaEvent, PizzaEvent.current()) second_pizzaevent.start = timezone.now() + datetime.timedelta(hours=2) second_pizzaevent.save() with self.subTest(msg="two events, within 8 hours"): self.assertEqual(self.pizzaEvent, PizzaEvent.current()) self.pizzaEvent.end = timezone.now() - datetime.timedelta(minutes=10) self.pizzaEvent.save() with self.subTest(msg="two events, within 8 hours, first ended"): self.assertEqual(second_pizzaevent, PizzaEvent.current())
def get_serializer_class(self): event = PizzaEvent.current() if can_change_order(self.request.member, event): return serializers.AdminOrderSerializer return serializers.OrderSerializer