views.py 5.33 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
from django.views.generic.edit import UpdateView, CreateView, DeleteView, ProcessFormView, FormMixin
Skia's avatar
Skia committed
4
5
6
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 django.utils import timezone
from django.conf import settings
Skia's avatar
Skia committed
10
from django import forms
Skia's avatar
Skia committed
11
12

from datetime import timedelta
Skia's avatar
Skia committed
13
14

from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin
Skia's avatar
Skia committed
15
from subscription.models import Subscriber
Skia's avatar
Skia committed
16
from accounting.models import Customer
Skia's avatar
Skia committed
17
18
from counter.models import Counter

Skia's avatar
Skia committed
19
20
21
22
class GetUserForm(forms.Form):
    username = forms.CharField(label="Name", max_length=64, required=False)

class CounterMain(DetailView, FormMixin):
Skia's avatar
Skia committed
23
24
25
    """
    The public (barman) view
    """
Skia's avatar
Skia committed
26
    model = Counter
Skia's avatar
Skia committed
27
    template_name = 'counter/counter_main.jinja'
Skia's avatar
Skia committed
28
    pk_url_kwarg = "counter_id"
Skia's avatar
Skia committed
29
    form_class = GetUserForm
Skia's avatar
Skia committed
30

Skia's avatar
Skia committed
31
    def get_context_data(self, **kwargs):
Skia's avatar
Skia committed
32
33
34
35
36
        """
        Get the barman list for the template

        Also handle the timeout
        """
Skia's avatar
Skia committed
37
38
39
40
        kwargs = super(CounterMain, self).get_context_data(**kwargs)
        kwargs['login_form'] = AuthenticationForm()
        kwargs['form'] = self.get_form()
        print(kwargs)
Skia's avatar
Skia committed
41
        if str(self.object.id) in list(Counter.barmen_session.keys()):
Skia's avatar
Skia committed
42
            if (timezone.now() - Counter.barmen_session[str(self.object.id)]['time']) < timedelta(minutes=settings.SITH_BARMAN_TIMEOUT):
Skia's avatar
Skia committed
43
                kwargs['barmen'] = []
Skia's avatar
Skia committed
44
                for b in Counter.barmen_session[str(self.object.id)]['users']:
Skia's avatar
Skia committed
45
                    kwargs['barmen'].append(Subscriber.objects.filter(id=b).first())
Skia's avatar
Skia committed
46
47
48
                Counter.barmen_session[str(self.object.id)]['time'] = timezone.now()
            else:
                Counter.barmen_session[str(self.object.id)]['users'] = {}
Skia's avatar
Skia committed
49
        else:
Skia's avatar
Skia committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
            kwargs['barmen'] = []
        return kwargs

class CounterClick(DetailView, ProcessFormView, FormMixin):
    """
    The click view
    """
    model = Counter # TODO change that to a basket class
    template_name = 'counter/counter_click.jinja'
    pk_url_kwarg = "counter_id"
    form_class = GetUserForm

    def post(self, request, *args, **kwargs):
        # TODO: handle the loading of a user, to display the click view
        # TODO: Do the form and the template for the click view
        return super(CounterClick, self).post(request, *args, **kwargs)

    def get_success_url(self):
        return reverse_lazy('counter:click', args=self.args, kwargs=self.kwargs)
Skia's avatar
Skia committed
69

Skia's avatar
Skia committed
70
class CounterLogin(RedirectView):
Skia's avatar
Skia committed
71
72
73
74
75
    """
    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
76
    permanent = False
Skia's avatar
Skia committed
77
    def post(self, request, *args, **kwargs):
Skia's avatar
Skia committed
78
79
80
        """
        Register the logged user as barman for this counter
        """
Skia's avatar
Skia committed
81
82
83
        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
84
        if form.is_valid():
Skia's avatar
Skia committed
85
            user = Subscriber.objects.filter(username=form.cleaned_data['username']).first()
Skia's avatar
Skia committed
86
            if self.counter_id not in Counter.barmen_session.keys():
Skia's avatar
Skia committed
87
                Counter.barmen_session[self.counter_id] = {'users': {user.id}, 'time': timezone.now()}
Skia's avatar
Skia committed
88
            else:
Skia's avatar
Skia committed
89
                Counter.barmen_session[self.counter_id]['users'].add(user.id)
Skia's avatar
Skia committed
90
91
92
93
94
95
96
97
98
99
        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):
Skia's avatar
Skia committed
100
101
102
        """
        Unregister the user from the barman
        """
Skia's avatar
Skia committed
103
        self.counter_id = kwargs['counter_id']
Skia's avatar
Skia committed
104
        Counter.barmen_session[str(self.counter_id)]['users'].remove(int(request.POST['user_id']))
Skia's avatar
Skia committed
105
106
107
108
        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
109
110
111
112
113
114
115
116

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

Skia's avatar
Skia committed
117
118
class CounterEditView(CanEditMixin, UpdateView):
    """
Skia's avatar
Skia committed
119
    Edit a counter's main informations (for the counter's admin)
Skia's avatar
Skia committed
120
121
122
123
124
125
126
127
128
    """
    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
129
    Create a counter (for the admins)
Skia's avatar
Skia committed
130
131
132
133
134
135
136
137
    """
    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
138
    Delete a counter (for the admins)
Skia's avatar
Skia committed
139
140
141
142
143
    """
    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
144
145