Commit 20a97526 authored by Skia's avatar Skia

Make counter login forms

parent 3302fdc6
Pipeline #6 skipped
body { body {
background: #EEE; background: #EEE;
} }
div {
box-shadow: 2px 2px 10px #888888;
margin: 10px;
padding: 10px;
}
...@@ -16,6 +16,7 @@ class Counter(models.Model): ...@@ -16,6 +16,7 @@ class Counter(models.Model):
choices=[('BAR',_('Bar')), ('OFFICE',_('Office'))]) choices=[('BAR',_('Bar')), ('OFFICE',_('Office'))])
edit_groups = models.ManyToManyField(Group, related_name="editable_counters", blank=True) edit_groups = models.ManyToManyField(Group, related_name="editable_counters", blank=True)
view_groups = models.ManyToManyField(Group, related_name="viewable_counters", blank=True) view_groups = models.ManyToManyField(Group, related_name="viewable_counters", blank=True)
barmen_session = {}
def __getattribute__(self, name): def __getattribute__(self, name):
if name == "owner_group": if name == "owner_group":
......
{% extends "core/base.jinja" %} {% extends "core/base.jinja" %}
{% macro barman_logout_link(user) %}
<form method="post" action="{{ url('counter:logout', counter_id=counter.id) }}" class="inline">
{% csrf_token %}
<input type="hidden" name="user_id" value="{{ user.id }}">
<button type="submit" name="submit_param" value="submit_value" class="link-button">{{ user.get_display_name() }}</button>
</form>
{% endmacro %}
{% block content %} {% block content %}
<h3>Counter</h3> <h3>Counter</h3>
<h4>{{ counter }}</h4> <h4>{{ counter }}</h4>
<p><strong>Club: </strong> {{ counter.club }}</p> <p><strong>Club: </strong> {{ counter.club }}</p>
<p><strong>Products: </strong> {{ counter.products.all() }}</p> <p><strong>Products: </strong> {{ counter.products.all() }}</p>
<div>
<h3>Barman: </h3>
<ul>
{% for b in barmen %}
<li>{{ barman_logout_link(b) }}</li>
{% endfor %}
</ul>
<form method="post" action="{{ url('counter:login', counter_id=counter.id) }}">
{% csrf_token %}
{{ login_form.as_p() }}
<input type="submit" value="login" />
</form>
</div>
<div>
{% if barmen %}
<p>Enter client code:</p>
{% else %}
<p>Please, login</p>
{% endif %}
</div>
{% endblock %} {% endblock %}
......
...@@ -5,6 +5,7 @@ from counter.views import * ...@@ -5,6 +5,7 @@ from counter.views import *
urlpatterns = [ urlpatterns = [
url(r'^(?P<counter_id>[0-9]+)$', CounterDetail.as_view(), name='details'), url(r'^(?P<counter_id>[0-9]+)$', CounterDetail.as_view(), name='details'),
url(r'^(?P<counter_id>[0-9]+)/login$', CounterLogin.as_view(), name='login'), url(r'^(?P<counter_id>[0-9]+)/login$', CounterLogin.as_view(), name='login'),
url(r'^(?P<counter_id>[0-9]+)/logout$', CounterLogout.as_view(), name='logout'),
url(r'^admin/(?P<counter_id>[0-9]+)$', CounterEditView.as_view(), name='admin'), url(r'^admin/(?P<counter_id>[0-9]+)$', CounterEditView.as_view(), name='admin'),
url(r'^admin$', CounterListView.as_view(), name='admin_list'), url(r'^admin$', CounterListView.as_view(), name='admin_list'),
url(r'^admin/new$', CounterCreateView.as_view(), name='new'), url(r'^admin/new$', CounterCreateView.as_view(), name='new'),
......
...@@ -7,9 +7,10 @@ from django.core.urlresolvers import reverse_lazy ...@@ -7,9 +7,10 @@ from django.core.urlresolvers import reverse_lazy
from django.contrib.auth.forms import AuthenticationForm from django.contrib.auth.forms import AuthenticationForm
from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin
from core.models import User
from counter.models import Counter from counter.models import Counter
class CounterDetail(CanViewMixin, DetailView): class CounterDetail(DetailView):
""" """
The public (barman) view The public (barman) view
""" """
...@@ -17,13 +18,50 @@ class CounterDetail(CanViewMixin, DetailView): ...@@ -17,13 +18,50 @@ class CounterDetail(CanViewMixin, DetailView):
template_name = 'counter/counter_detail.jinja' template_name = 'counter/counter_detail.jinja'
pk_url_kwarg = "counter_id" 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): 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 permanent = False
def post(self): # TODO: finish that def post(self, request, *args, **kwargs):
print(self.request) self.counter_id = kwargs['counter_id']
form = AuthenticationForm(self.request, data=self.request.POST) # TODO: make some checks on the counter type
form = AuthenticationForm(request, data=request.POST)
if form.is_valid(): 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): class CounterListView(CanViewMixin, ListView):
""" """
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment