From 20a97526d49bcda6bf8a750d36c590886b08f762 Mon Sep 17 00:00:00 2001 From: Skia Date: Tue, 12 Apr 2016 10:00:47 +0200 Subject: [PATCH] Make counter login forms --- core/static/core/style.css | 6 +++ counter/models.py | 1 + .../templates/counter/counter_detail.jinja | 29 +++++++++++ counter/urls.py | 1 + counter/views.py | 48 +++++++++++++++++-- 5 files changed, 80 insertions(+), 5 deletions(-) diff --git a/core/static/core/style.css b/core/static/core/style.css index 5be8aa85..5da7a456 100644 --- a/core/static/core/style.css +++ b/core/static/core/style.css @@ -1,3 +1,9 @@ body { background: #EEE; } + +div { + box-shadow: 2px 2px 10px #888888; + margin: 10px; + padding: 10px; +} diff --git a/counter/models.py b/counter/models.py index c13159fc..e8462758 100644 --- a/counter/models.py +++ b/counter/models.py @@ -16,6 +16,7 @@ class Counter(models.Model): choices=[('BAR',_('Bar')), ('OFFICE',_('Office'))]) edit_groups = models.ManyToManyField(Group, related_name="editable_counters", blank=True) view_groups = models.ManyToManyField(Group, related_name="viewable_counters", blank=True) + barmen_session = {} def __getattribute__(self, name): if name == "owner_group": diff --git a/counter/templates/counter/counter_detail.jinja b/counter/templates/counter/counter_detail.jinja index f0c6b657..f7abe160 100644 --- a/counter/templates/counter/counter_detail.jinja +++ b/counter/templates/counter/counter_detail.jinja @@ -1,10 +1,39 @@ {% extends "core/base.jinja" %} +{% macro barman_logout_link(user) %} +
+ {% csrf_token %} + + +
+{% endmacro %} + {% block content %}

Counter

{{ counter }}

Club: {{ counter.club }}

Products: {{ counter.products.all() }}

+ +
+

Barman:

+ +
+ {% csrf_token %} + {{ login_form.as_p() }} + +
+
+
+ {% if barmen %} +

Enter client code:

+ {% else %} +

Please, login

+ {% endif %} +
{% endblock %} diff --git a/counter/urls.py b/counter/urls.py index 2f242606..915acf3a 100644 --- a/counter/urls.py +++ b/counter/urls.py @@ -5,6 +5,7 @@ from counter.views import * urlpatterns = [ url(r'^(?P[0-9]+)$', CounterDetail.as_view(), name='details'), url(r'^(?P[0-9]+)/login$', CounterLogin.as_view(), name='login'), + url(r'^(?P[0-9]+)/logout$', CounterLogout.as_view(), name='logout'), url(r'^admin/(?P[0-9]+)$', CounterEditView.as_view(), name='admin'), url(r'^admin$', CounterListView.as_view(), name='admin_list'), url(r'^admin/new$', CounterCreateView.as_view(), name='new'), diff --git a/counter/views.py b/counter/views.py index ddbd3a7f..d68dbb3d 100644 --- a/counter/views.py +++ b/counter/views.py @@ -7,9 +7,10 @@ from django.core.urlresolvers import reverse_lazy from django.contrib.auth.forms import AuthenticationForm from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin +from core.models import User from counter.models import Counter -class CounterDetail(CanViewMixin, DetailView): +class CounterDetail(DetailView): """ The public (barman) view """ @@ -17,13 +18,50 @@ class CounterDetail(CanViewMixin, DetailView): template_name = 'counter/counter_detail.jinja' pk_url_kwarg = "counter_id" + 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 + class CounterLogin(RedirectView): + """ + Handle the login of a barman + + Logged barmen are stored in the class-wide variable 'barmen_session', in the Counter model + """ permanent = False - def post(self): # TODO: finish that - print(self.request) - form = AuthenticationForm(self.request, data=self.request.POST) + 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) if form.is_valid(): - print("Barman logged") + 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) class CounterListView(CanViewMixin, ListView): """