Make counter login forms

This commit is contained in:
Skia 2016-04-12 10:00:47 +02:00
parent 3302fdc6f2
commit 20a97526d4
5 changed files with 80 additions and 5 deletions

View File

@ -1,3 +1,9 @@
body {
background: #EEE;
}
div {
box-shadow: 2px 2px 10px #888888;
margin: 10px;
padding: 10px;
}

View File

@ -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":

View File

@ -1,10 +1,39 @@
{% 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 %}
<h3>Counter</h3>
<h4>{{ counter }}</h4>
<p><strong>Club: </strong> {{ counter.club }}</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 %}

View File

@ -5,6 +5,7 @@ from counter.views import *
urlpatterns = [
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]+)/logout$', CounterLogout.as_view(), name='logout'),
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/new$', CounterCreateView.as_view(), name='new'),

View File

@ -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):
"""