views.py 3.69 KB
Newer Older
Skia's avatar
Skia committed
1
from django.shortcuts import render
Skia's avatar
Skia committed
2
from django.views.generic import ListView, DetailView, RedirectView
Skia's avatar
Skia committed
3
4
5
6
from django.views.generic.edit import UpdateView, CreateView, DeleteView
from django.forms.models import modelform_factory
from django.forms import CheckboxSelectMultiple
from django.core.urlresolvers import reverse_lazy
Skia's avatar
Skia committed
7
from django.contrib.auth.forms import AuthenticationForm
Skia's avatar
Skia committed
8
9

from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin
Skia's avatar
Skia committed
10
from core.models import User
Skia's avatar
Skia committed
11
12
from counter.models import Counter

Skia's avatar
Skia committed
13
class CounterDetail(DetailView):
Skia's avatar
Skia committed
14
15
16
    """
    The public (barman) view
    """
Skia's avatar
Skia committed
17
18
19
    model = Counter
    template_name = 'counter/counter_detail.jinja'
    pk_url_kwarg = "counter_id"
Skia's avatar
Skia committed
20

Skia's avatar
Skia committed
21
22
23
24
25
26
27
28
29
30
31
    def get_context_data(self, **kwargs):
        context = super(CounterDetail, self).get_context_data(**kwargs)
        context['login_form'] = AuthenticationForm()
        if str(self.object.id) in list(Counter.barmen_session.keys()):
            context['barmen'] = []
            for b in Counter.barmen_session[str(self.object.id)]:
                context['barmen'].append(User.objects.filter(id=b).first())
        else:
            context['barmen'] = []
        return context

Skia's avatar
Skia committed
32
class CounterLogin(RedirectView):
Skia's avatar
Skia committed
33
34
35
36
37
    """
    Handle the login of a barman

    Logged barmen are stored in the class-wide variable 'barmen_session', in the Counter model
    """
Skia's avatar
Skia committed
38
    permanent = False
Skia's avatar
Skia committed
39
40
41
42
    def post(self, request, *args, **kwargs):
        self.counter_id = kwargs['counter_id']
# TODO: make some checks on the counter type
        form = AuthenticationForm(request, data=request.POST)
Skia's avatar
Skia committed
43
        if form.is_valid():
Skia's avatar
Skia committed
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
            user = User.objects.filter(username=form.cleaned_data['username']).first()
            if self.counter_id not in Counter.barmen_session.keys():
                Counter.barmen_session[self.counter_id] = {user.id} # TODO add timeout
            else:
                Counter.barmen_session[self.counter_id].add(user.id)
        else:
            print("Error logging the barman") # TODO handle that nicely
        return super(CounterLogin, self).post(request, *args, **kwargs)

    def get_redirect_url(self, *args, **kwargs):
        return reverse_lazy('counter:details', args=args, kwargs=kwargs)

class CounterLogout(RedirectView):
    permanent = False
    def post(self, request, *args, **kwargs):
        self.counter_id = kwargs['counter_id']
        Counter.barmen_session[self.counter_id].remove(int(request.POST['user_id']))
        return super(CounterLogout, self).post(request, *args, **kwargs)

    def get_redirect_url(self, *args, **kwargs):
        return reverse_lazy('counter:details', args=args, kwargs=kwargs)
Skia's avatar
Skia committed
65
66
67
68
69
70
71
72

class CounterListView(CanViewMixin, ListView):
    """
    A list view for the admins
    """
    model = Counter
    template_name = 'counter/counter_list.jinja'

Skia's avatar
Skia committed
73
74
class CounterEditView(CanEditMixin, UpdateView):
    """
Skia's avatar
Skia committed
75
    Edit a counter's main informations (for the counter's admin)
Skia's avatar
Skia committed
76
77
78
79
80
81
82
83
84
    """
    model = Counter
    form_class = modelform_factory(Counter, fields=['name', 'club', 'type', 'products'],
            widgets={'products':CheckboxSelectMultiple})
    pk_url_kwarg = "counter_id"
    template_name = 'counter/counter_edit.jinja'

class CounterCreateView(CanEditMixin, CreateView):
    """
Skia's avatar
Skia committed
85
    Create a counter (for the admins)
Skia's avatar
Skia committed
86
87
88
89
90
91
92
93
    """
    model = Counter
    form_class = modelform_factory(Counter, fields=['name', 'club', 'type', 'products'],
            widgets={'products':CheckboxSelectMultiple})
    template_name = 'counter/counter_edit.jinja'

class CounterDeleteView(CanEditMixin, DeleteView):
    """
Skia's avatar
Skia committed
94
    Delete a counter (for the admins)
Skia's avatar
Skia committed
95
96
97
98
99
    """
    model = Counter
    pk_url_kwarg = "counter_id"
    template_name = 'core/delete_confirm.jinja'
    success_url = reverse_lazy('counter:admin_list')
Skia's avatar
Skia committed
100
101