Unverified Commit 617caaa5 authored by Thom Wiggers's avatar Thom Wiggers 📐
Browse files

Only order available products

Implements a model manager which filters for only available products.
Also refactors the order function
parent cd4cd76e
......@@ -14,7 +14,7 @@ from pizzas.api import serializers
class PizzaViewset(GenericViewSet, ListModelMixin):
queryset = Product.objects.filter(available=True)
queryset = Product.available_products.all()
permission_classes = (permissions.IsAuthenticated,)
serializer_class = serializers.PizzaSerializer
......
from django.forms import ModelForm
from .models import Order
from .models import Order, Product
class AddOrderForm(ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['product'].queryset = Product.available_products.all()
class Meta:
model = Order
fields = ['name', 'product']
# -*- coding: utf-8 -*-
# Generated by Django 1.11.8 on 2017-12-13 18:54
from __future__ import unicode_literals
from django.db import migrations
import django.db.models.manager
class Migration(migrations.Migration):
dependencies = [
('pizzas', '0004_auto_20171013_1408'),
]
operations = [
migrations.AlterModelManagers(
name='product',
managers=[
('available_products', django.db.models.manager.Manager()),
],
),
]
......@@ -51,7 +51,17 @@ class PizzaEvent(models.Model):
return 'Pizzas for ' + str(self.event)
class AvailableProductManager(models.Manager):
"""Only shows available products"""
def get_queryset(self):
return super().get_queryset().filter(available=True)
class Product(models.Model, metaclass=ModelTranslateMeta):
objects = models.Manager()
available_products = AvailableProductManager()
name = models.CharField(max_length=50)
description = MultilingualField(models.TextField)
price = models.DecimalField(max_digits=5, decimal_places=2)
......
......@@ -12,7 +12,7 @@ from .models import Order, PizzaEvent, Product
@login_required
def index(request):
products = Product.objects.filter(available=True).order_by('name')
products = Product.available_products.order_by('name')
event = PizzaEvent.current()
try:
order = Order.objects.get(pizza_event=event,
......@@ -123,20 +123,21 @@ def order(request):
return HttpResponseRedirect(reverse('pizzas:index'))
try:
order_placed = Order.objects.get(pizza_event=event,
member=request.member)
current_order_locked = not order_placed.can_be_changed
order = Order.objects.get(pizza_event=event,
member=request.member)
current_order_locked = not order.can_be_changed
except Order.DoesNotExist:
order = None
current_order_locked = False
if 'product' in request.POST and not current_order_locked:
product = Product.objects.get(pk=int(request.POST['product']))
if product:
try:
order = Order.objects.get(pizza_event=event,
member=request.member)
except Order.DoesNotExist:
order = Order(pizza_event=event, member=request.member)
order.product = product
order.save()
try:
product = Product.available_products.get(
pk=int(request.POST['product']))
except Product.DoesNotExist:
raise Http404('Pizza does not exist')
if not order:
order = Order(pizza_event=event, member=request.member)
order.product = product
order.save()
return HttpResponseRedirect(reverse('pizzas:index'))
Supports Markdown
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