Make some more validation on the token click form

This commit is contained in:
Skia 2016-08-01 19:59:22 +02:00
parent 4d8e7b0875
commit 2cf39671e2
10 changed files with 108 additions and 32 deletions

View File

@ -71,7 +71,7 @@ Welcome to the wiki page!
p.set_lock(root)
p.save()
p.set_lock(root)
PageRev(page=p, title="", author=root, content="").save()
PageRev(page=p, title="Laverie", author=root, content="Fonctionnement de la laverie").save()
# Here we add a lot of test datas, that are not necessary for the Sith, but that provide a basic development environment
if not options['prod']:

View File

@ -16,7 +16,6 @@ header {
right : 8%;
left: 40%;
background-color:#DDD;
height: 3em;
}
header a {
display: inline-block;

View File

@ -17,7 +17,7 @@
<li><a href="{{ url('subscription:subscription') }}">{% trans %}Subscriptions{% endtrans %}</a></li>
{% endif %}
<h5>{% trans %}Launderette{% endtrans %}</h5>
{% if user.is_in_group(settings.SITH_GROUPS['launderette-admin']['name']) %}
{% if user.is_in_group(settings.SITH_GROUPS['launderette-admin']['name']) or user.is_in_group(settings.SITH_GROUPS['root']['name']) %}
<li><a href="{{ url('launderette:launderette_list') }}">{% trans %}Launderette{% endtrans %}</a></li>
{% endif %}
</ul>

View File

@ -60,7 +60,7 @@ class CanEditPropMixin(View):
class CanEditMixin(View):
"""
This view makes exactly the same this as its direct parent, but checks the group on the edit_groups field of the
This view makes exactly the same thing as its direct parent, but checks the group on the edit_groups field of the
object
"""
def dispatch(self, request, *arg, **kwargs):
@ -78,7 +78,7 @@ class CanEditMixin(View):
class CanViewMixin(View):
"""
This view still makes exactly the same this as its direct parent, but checks the group on the view_groups field of
This view still makes exactly the same thing as its direct parent, but checks the group on the view_groups field of
the object
"""
def dispatch(self, request, *arg, **kwargs):

View File

@ -327,8 +327,10 @@ class CounterEditView(CanEditPropMixin, UpdateView):
Edit a counter's main informations (for the counter's admin)
"""
model = Counter
form_class = modelform_factory(Counter, fields=['name', 'club', 'type', 'products'],
widgets={'products':CheckboxSelectMultiple})
form_class = modelform_factory(Counter, fields=['name', 'club', 'type', 'sellers', 'products'],
widgets={
'products':CheckboxSelectMultiple,
'sellers':CheckboxSelectMultiple})
pk_url_kwarg = "counter_id"
template_name = 'counter/counter_edit.jinja'

View File

@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('launderette', '0005_auto_20160801_1634'),
]
operations = [
migrations.AlterField(
model_name='token',
name='borrow_date',
field=models.DateTimeField(blank=True, verbose_name='borrow date', null=True),
),
migrations.AlterField(
model_name='token',
name='user',
field=models.ForeignKey(blank=True, to='subscription.Subscriber', related_name='tokens', verbose_name='user'),
),
]

View File

@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('launderette', '0006_auto_20160801_1928'),
]
operations = [
migrations.AlterField(
model_name='token',
name='user',
field=models.ForeignKey(verbose_name='user', related_name='tokens', blank=True, null=True, to='subscription.Subscriber'),
),
]

View File

@ -4,7 +4,7 @@ from django.conf import settings
from django.core.urlresolvers import reverse
from core.models import User
from counter.models import Counter
from counter.models import Counter, Product
from subscription.models import Subscriber
from subscription.views import get_subscriber
@ -26,8 +26,8 @@ class Launderette(models.Model):
return False
def can_be_edited_by(self, user):
sub = get_subscriber(request.user)
return sub in self.sellers.all()
sub = get_subscriber(user)
return sub in self.counter.sellers.all()
def can_be_viewed_by(self, user):
return user.is_in_group(settings.SITH_MAIN_MEMBERS_GROUP)
@ -65,8 +65,8 @@ class Token(models.Model):
name = models.CharField(_('name'), max_length=5)
launderette = models.ForeignKey(Launderette, related_name='tokens', verbose_name=_('launderette'))
type = models.CharField(_('type'), max_length=10, choices=[('WASHING', _('Washing')), ('DRYING', _('Drying'))])
borrow_date = models.DateTimeField(_('borrow date'), null=True)
user = models.ForeignKey(Subscriber, related_name='tokens', verbose_name=_('user'))
borrow_date = models.DateTimeField(_('borrow date'), null=True, blank=True)
user = models.ForeignKey(Subscriber, related_name='tokens', verbose_name=_('user'), null=True, blank=True)
class Meta:
verbose_name = _('Token')

View File

@ -8,7 +8,7 @@
<h3>{% trans counter_name=counter %}{{ counter_name }} counter{% endtrans %}</h3>
<div>
Counter: {{ counter }}
<form method="post" action="{{ url('launderette:click', launderette_id=launderette.id, user_id=customer.user.id) }}" class="inline" style="display:inline">
<form method="post" action="" class="inline" style="display:inline">
{% csrf_token %}
{{ form.as_p() }}
<p><input type="submit" value="{% trans %}Go{% endtrans %}" /></p>

View File

@ -4,7 +4,7 @@ import pytz
from django.shortcuts import render
from django.views.generic import ListView, DetailView, RedirectView, TemplateView
from django.views.generic.edit import UpdateView, CreateView, DeleteView, ProcessFormView, FormMixin
from django.views.generic.edit import UpdateView, CreateView, DeleteView, BaseFormView
from django.forms.models import modelform_factory
from django.forms import CheckboxSelectMultiple
from django.utils.translation import ugettext as _
@ -13,6 +13,8 @@ from django.core.urlresolvers import reverse_lazy
from django.conf import settings
from django.db import transaction
from django import forms
from django.template import defaultfilters
from django.utils import formats
from core.models import Page
from club.models import Club
@ -145,7 +147,7 @@ class LaunderetteDetailView(CanEditPropMixin, DetailView):
pk_url_kwarg = "launderette_id"
template_name = 'launderette/launderette_detail.jinja'
class LaunderetteMainClickView(DetailView, ProcessFormView, FormMixin):
class LaunderetteMainClickView(CanEditMixin, BaseFormView, DetailView):
"""The click page of the launderette"""
model = Launderette
pk_url_kwarg = "launderette_id"
@ -156,47 +158,68 @@ class LaunderetteMainClickView(DetailView, ProcessFormView, FormMixin):
"""
We handle here the login form for the barman
"""
if self.request.method == 'POST':
self.object = self.get_object()
kwargs = super(LaunderetteMainClickView, self).get_context_data(**kwargs)
kwargs['counter'] = self.object.counter
kwargs['form'] = self.get_form()
kwargs['barmen'] = [self.request.user]
if 'last_basket' in self.request.session.keys():
kwargs['last_basket'] = self.request.session.pop('last_basket')
kwargs['last_customer'] = self.request.session.pop('last_customer')
kwargs['last_total'] = self.request.session.pop('last_total')
kwargs['new_customer_amount'] = self.request.session.pop('new_customer_amount')
kwargs['last_basket'] = self.request.session.pop('last_basket', None)
kwargs['last_customer'] = self.request.session.pop('last_customer', None)
kwargs['last_total'] = self.request.session.pop('last_total', None)
kwargs['new_customer_amount'] = self.request.session.pop('new_customer_amount', None)
return kwargs
def form_valid(self, form):
"""
We handle here the redirection, passing the user id of the asked customer
"""
self.object = self.get_object()
self.kwargs['user_id'] = form.cleaned_data['user_id']
return super(LaunderetteMainClickView, self).form_valid(form)
def get_success_url(self):
return reverse_lazy('launderette:click', args=self.args, kwargs=self.kwargs)
class LaunderetteClickView(CanEditMixin, DetailView):
class ClickTokenForm(forms.BaseForm):
pass
class LaunderetteClickView(CanEditMixin, DetailView, BaseFormView):
"""The click page of the launderette"""
model = Launderette
pk_url_kwarg = "launderette_id"
template_name = 'launderette/launderette_click.jinja'
def generate_form(self):
def get_form_class(self):
fields = OrderedDict()
for s in self.subscriber.slots.all():
fields["%s-%s-%s-%s" % (s.user, s.start_date, s.type, s.machine)] = forms.CharField(max_length=5,
label="%s - %s" % (s.get_type_display(), s.start_date))
return type('ClickForm', (forms.BaseForm,), { 'base_fields': fields })()
kwargs = {}
def clean_field_factory(field_name, slot):
def clean_field(self2):
t_name = str(self2.data[field_name])
if t_name != "":
t = Token.objects.filter(name=str(self2.data[field_name]), type=slot.type, launderette=self.object).first()
if t is None:
raise forms.ValidationError(_("Token not found"))
return clean_field
for s in self.subscriber.slots.filter(token=None).all():
field_name = "slot-%s" % (str(s.id))
fields[field_name] = forms.CharField(max_length=5, required=False,
label="%s - %s" % (s.get_type_display(), defaultfilters.date(s.start_date, "j N Y H:i")))
# XXX l10n settings.DATETIME_FORMAT did'nt work here :/
kwargs["clean_"+field_name] = clean_field_factory(field_name, s)
def clean_form(self2):
raise forms.ValidationError(_("Not enough money"))
return self2.cleaned_data
kwargs['base_fields'] = fields
kwargs['clean'] = clean_form
return type('ClickForm', (ClickTokenForm,), kwargs)
def get(self, request, *args, **kwargs):
"""Simple get view"""
self.customer = Customer.objects.filter(user__id=self.kwargs['user_id']).first()
self.subscriber = get_subscriber(self.customer.user)
request.session['not_enough'] = False
return super(LaunderetteClickView, self).get(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
@ -204,21 +227,30 @@ class LaunderetteClickView(CanEditMixin, DetailView):
self.object = self.get_object()
self.customer = Customer.objects.filter(user__id=self.kwargs['user_id']).first()
self.subscriber = get_subscriber(self.customer.user)
request.session['not_enough'] = False
context = self.get_context_data(object=self.object)
return self.render_to_response(context)
return super(LaunderetteClickView, self).post(request, *args, **kwargs)
def form_valid(self, form):
"""
We handle here the redirection, passing the user id of the asked customer
"""
self.request.session['last_basket'] = ["GUY"]
return super(LaunderetteClickView, self).form_valid(form)
def get_context_data(self, **kwargs):
"""
We handle here the login form for the barman
"""
kwargs = super(LaunderetteClickView, self).get_context_data(**kwargs)
if 'form' not in kwargs.keys():
kwargs['form'] = self.get_form()
kwargs['counter'] = self.object.counter
kwargs['customer'] = self.customer
kwargs['form'] = self.generate_form()
return kwargs
def get_success_url(self):
self.kwargs.pop('user_id', None)
return reverse_lazy('launderette:main_click', args=self.args, kwargs=self.kwargs)
class MachineEditView(CanEditPropMixin, UpdateView):