views.py 5.38 KB
Newer Older
1
from django.contrib import messages
2
from django.contrib.auth.decorators import login_required, permission_required
Thom Wiggers's avatar
Thom Wiggers committed
3
from django.urls import reverse
4
5
from django.http import Http404, HttpResponseRedirect, JsonResponse
from django.shortcuts import get_object_or_404, render
6
from django.utils.translation import ugettext_lazy as _
7
from django.views.decorators.http import require_http_methods
8
9

from .forms import AddOrderForm
10
from .models import Order, PizzaEvent, Product
11
12
13
14


@login_required
def index(request):
Wietse Kuipers's avatar
Wietse Kuipers committed
15
    products = Product.objects.filter(available=True).order_by('name')
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
    event = PizzaEvent.current()
    try:
        order = Order.objects.get(pizza_event=event,
                                  member=request.user.member)
    except Order.DoesNotExist:
        order = None
    context = {'event': event, 'products': products, 'order': order}
    return render(request, 'pizzas/index.html', context)


@permission_required('pizzas.change_order')
def orders(request, event_pk):
    event = get_object_or_404(PizzaEvent, pk=event_pk)
    context = {'event': event,
               'orders': Order.objects.filter(pizza_event=event)
                                      .prefetch_related('member', 'product')}
    return render(request, 'pizzas/orders.html', context)


Jim Driessen's avatar
Jim Driessen committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
@permission_required('pizzas.change_order')
def overview(request, event_pk):
    event = get_object_or_404(PizzaEvent, pk=event_pk)

    product_list = {}
    total_money = 0
    total_products = 0
    orders = Order.objects.filter(pizza_event=event)\
                          .prefetch_related('product')

    for order in orders:
        if order.product.id not in product_list:
            product_list[order.product.id] = {
                'name': order.product.name,
                'price': order.product.price,
                'amount': 0,
                'total': 0
            }
        product_list[order.product.id]['amount'] += 1
        product_list[order.product.id]['total'] += order.product.price
        total_products += 1
        total_money += order.product.price

    context = {
        'event': event,
        'product_list': product_list,
        'total_money': total_money,
        'total_products': total_products
    }

    return render(request, 'pizzas/overview.html', context)


68
69
70
71
72
73
74
75
@require_http_methods(["POST"])
@permission_required('pizzas.change_order')
def toggle_orderpayment(request):
    if 'order' not in request.POST:
        return JsonResponse({'error': _("You must supply an order pk.")})
    try:
        order = get_object_or_404(Order, pk=int(request.POST['order']))
    except Http404:
76
        return JsonResponse({'error': _("Your order could not be found.")})
77
78
79
80
81
82
83
84
85
86
87
88
89
    order.paid = not order.paid
    order.save()
    return JsonResponse({'success': 1, 'paid': int(order.paid)})


@require_http_methods(["POST"])
@permission_required('pizzas.change_order')
def delete_order(request):
    if 'order' in request.POST:
        try:
            order = get_object_or_404(Order, pk=int(request.POST['order']))
            order.delete()
        except Http404:
90
            messages.error(request, _("Your order could not be found."))
91
92
93
94
95
96
97
98
99
100
101
    event = PizzaEvent.current()
    if event:
        return HttpResponseRedirect(reverse('pizzas:orders', args=[event.pk]))
    return HttpResponseRedirect(reverse('pizzas:index'))


@require_http_methods(["POST"])
def cancel_order(request):
    if 'order' in request.POST:
        try:
            order = get_object_or_404(Order, pk=int(request.POST['order']))
102
103
104
105
            if not order.can_be_changed:
                messages.error(request,
                               _('You can no longer cancel.'))
            elif order.member == request.user.member:
106
                order.delete()
107
                messages.success(request, _("Your order has been cancelled."))
108
        except Http404:
109
            messages.error(request, _("Your order could not be found."))
110
111
112
113
114
115
116
117
118
119
120
    return HttpResponseRedirect(reverse('pizzas:index'))


@permission_required('pizzas.change_order')
def add_order(request, event_pk):
    event = get_object_or_404(PizzaEvent, pk=event_pk)
    if request.POST:
        form = AddOrderForm(request.POST)
        order = form.save(commit=False)
        order.pizza_event = event
        order.save()
121
        messages.success(request, _("Your order was successful."))
122
123
124
125
126
127
128
129
130
131
        return HttpResponseRedirect(reverse('pizzas:orders', args=[event.pk]))
    context = {
        'event': event,
        'form': AddOrderForm(),
    }
    return render(request, 'pizzas/add_order.html', context)


@login_required
def order(request):
132
133
134
135
136
137
138
139
140
141
142
143
    event = PizzaEvent.current()
    if not event:
        return HttpResponseRedirect(reverse('pizzas:index'))

    try:
        order_placed = Order.objects.get(pizza_event=event,
                                         member=request.user.member)
        current_order_locked = not order_placed.can_be_changed
    except Order.DoesNotExist:
        current_order_locked = False

    if 'product' in request.POST and not current_order_locked:
144
145
146
147
148
149
150
151
152
153
        product = Product.objects.get(pk=int(request.POST['product']))
        if product:
            try:
                order = Order.objects.get(pizza_event=event,
                                          member=request.user.member)
            except Order.DoesNotExist:
                order = Order(pizza_event=event, member=request.user.member)
            order.product = product
            order.save()
    return HttpResponseRedirect(reverse('pizzas:index'))