admin_views.py 3.19 KB
Newer Older
1
2
"""Admin views provided by the pizzas package"""
from django.shortcuts import get_object_or_404
3
from django.utils.decorators import method_decorator
4
5
6
7
from django.utils.text import capfirst
from django.utils.translation import ugettext_lazy as _
from django.views.generic import TemplateView

8
from events.decorators import organiser_only
9
10
11
12
from payments.models import Payment
from pizzas.models import PizzaEvent, Order


13
@method_decorator(organiser_only, name='dispatch')
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class PizzaOrderSummary(TemplateView):
    template_name = 'pizzas/admin/summary.html'
    admin = None

    def get_context_data(self, **kwargs):
        event = get_object_or_404(PizzaEvent, pk=kwargs.get('pk'))

        context = super().get_context_data(**kwargs)
        context.update({
            **self.admin.admin_site.each_context(self.request),
            'has_delete_permission': False,
            'has_editable_inline_admin_formsets': False,
            'app_label': 'pizzas',
            'opts': PizzaEvent._meta,
            'is_popup': False,
            'save_as': False,
            'save_on_top': False,
31
            'title': capfirst(_('order summary')),
32
33
34
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
            'original': capfirst(_('summary')),
            'pizza_event': event
        })

        product_list = {}
        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

        product_list = sorted(product_list.values(), key=lambda x: x['name'])

        context.update({
            'event': event,
            'product_list': product_list,
            'total_money': sum(map(lambda x: x['total'], product_list)),
            'total_products': len(orders)
        })

        return context


63
@method_decorator(organiser_only, name='dispatch')
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
class PizzaOrderDetails(TemplateView):
    template_name = 'pizzas/admin/orders.html'
    admin = None

    def get_context_data(self, **kwargs):
        event = get_object_or_404(PizzaEvent, pk=kwargs.get('pk'))

        context = super().get_context_data(**kwargs)
        context.update({
            **self.admin.admin_site.each_context(self.request),
            'has_delete_permission': False,
            'has_editable_inline_admin_formsets': False,
            'app_label': 'pizzas',
            'opts': PizzaEvent._meta,
            'is_popup': False,
            'save_as': False,
            'save_on_top': False,
81
            'title': capfirst(_('order overview')),
82
83
84
85
86
87
88
89
90
91
92
93
94
95
            'original': str(event),
            'pizza_event': event
        })

        context.update({
            'event': event,
            'payment': Payment,
            'orders': (Order.objects
                       .filter(pizza_event=event)
                       .prefetch_related('member', 'product')
                       .order_by('member__first_name'))
        })

        return context