From 2cf39671e269b5ba4d07384525f0ecb3e6afeb1c Mon Sep 17 00:00:00 2001 From: Skia Date: Mon, 1 Aug 2016 19:59:22 +0200 Subject: [PATCH] Make some more validation on the token click form --- core/management/commands/populate.py | 2 +- core/static/core/style.css | 1 - core/templates/core/user_tools.jinja | 2 +- core/views/__init__.py | 4 +- counter/views.py | 6 +- .../migrations/0006_auto_20160801_1928.py | 24 +++++++ .../migrations/0007_auto_20160801_1929.py | 19 +++++ launderette/models.py | 10 +-- .../launderette/launderette_click.jinja | 2 +- launderette/views.py | 70 ++++++++++++++----- 10 files changed, 108 insertions(+), 32 deletions(-) create mode 100644 launderette/migrations/0006_auto_20160801_1928.py create mode 100644 launderette/migrations/0007_auto_20160801_1929.py diff --git a/core/management/commands/populate.py b/core/management/commands/populate.py index 25b21c63..ed1d0bc5 100644 --- a/core/management/commands/populate.py +++ b/core/management/commands/populate.py @@ -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']: diff --git a/core/static/core/style.css b/core/static/core/style.css index 55ae5f08..0ee66260 100644 --- a/core/static/core/style.css +++ b/core/static/core/style.css @@ -16,7 +16,6 @@ header { right : 8%; left: 40%; background-color:#DDD; - height: 3em; } header a { display: inline-block; diff --git a/core/templates/core/user_tools.jinja b/core/templates/core/user_tools.jinja index e88ccb16..6b759946 100644 --- a/core/templates/core/user_tools.jinja +++ b/core/templates/core/user_tools.jinja @@ -17,7 +17,7 @@
  • {% trans %}Subscriptions{% endtrans %}
  • {% endif %}
    {% trans %}Launderette{% endtrans %}
    - {% 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']) %}
  • {% trans %}Launderette{% endtrans %}
  • {% endif %} diff --git a/core/views/__init__.py b/core/views/__init__.py index 1b3acc6c..81891dd7 100644 --- a/core/views/__init__.py +++ b/core/views/__init__.py @@ -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): diff --git a/counter/views.py b/counter/views.py index f0fbebcf..61754525 100644 --- a/counter/views.py +++ b/counter/views.py @@ -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' diff --git a/launderette/migrations/0006_auto_20160801_1928.py b/launderette/migrations/0006_auto_20160801_1928.py new file mode 100644 index 00000000..fdc11e17 --- /dev/null +++ b/launderette/migrations/0006_auto_20160801_1928.py @@ -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'), + ), + ] diff --git a/launderette/migrations/0007_auto_20160801_1929.py b/launderette/migrations/0007_auto_20160801_1929.py new file mode 100644 index 00000000..1cb8e9be --- /dev/null +++ b/launderette/migrations/0007_auto_20160801_1929.py @@ -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'), + ), + ] diff --git a/launderette/models.py b/launderette/models.py index 4a9b3d33..aeacce4a 100644 --- a/launderette/models.py +++ b/launderette/models.py @@ -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') diff --git a/launderette/templates/launderette/launderette_click.jinja b/launderette/templates/launderette/launderette_click.jinja index 810c70d0..3ae92017 100644 --- a/launderette/templates/launderette/launderette_click.jinja +++ b/launderette/templates/launderette/launderette_click.jinja @@ -8,7 +8,7 @@

    {% trans counter_name=counter %}{{ counter_name }} counter{% endtrans %}

    Counter: {{ counter }} -
    + {% csrf_token %} {{ form.as_p() }}

    diff --git a/launderette/views.py b/launderette/views.py index cf847cb6..e5cd7f17 100644 --- a/launderette/views.py +++ b/launderette/views.py @@ -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):