From c099cc489be40a64ccfc94cf4c03c1e0d50aa148 Mon Sep 17 00:00:00 2001 From: Skia Date: Sun, 17 Jul 2016 11:38:19 +0200 Subject: [PATCH] Improve refilling form to handle the checks and the banks --- counter/migrations/0004_auto_20160717_0933.py | 24 +++++++++++++++++++ counter/models.py | 5 ++-- counter/templates/counter/counter_click.jinja | 2 +- counter/views.py | 22 +++++++++++++---- sith/settings_sample.py | 7 ++++++ 5 files changed, 53 insertions(+), 7 deletions(-) create mode 100644 counter/migrations/0004_auto_20160717_0933.py diff --git a/counter/migrations/0004_auto_20160717_0933.py b/counter/migrations/0004_auto_20160717_0933.py new file mode 100644 index 00000000..e50304b2 --- /dev/null +++ b/counter/migrations/0004_auto_20160717_0933.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('counter', '0003_customer_amount'), + ] + + operations = [ + migrations.AddField( + model_name='refilling', + name='bank', + field=models.CharField(verbose_name='bank', default='other', max_length=255, choices=[('other', 'Autre'), ('la-poste', 'La Poste'), ('credit-agricole', 'Credit Agricole'), ('credit-mutuel', 'Credit Mutuel')]), + ), + migrations.AlterField( + model_name='refilling', + name='payment_method', + field=models.CharField(verbose_name='payment method', default='cash', max_length=255, choices=[('cheque', 'Chèque'), ('cash', 'Espèce')]), + ), + ] diff --git a/counter/models.py b/counter/models.py index ce65f018..69337754 100644 --- a/counter/models.py +++ b/counter/models.py @@ -123,8 +123,9 @@ class Refilling(models.Model): customer = models.ForeignKey(Customer, related_name="refill_customers", blank=False) date = models.DateTimeField(_('date'), auto_now=True) payment_method = models.CharField(_('payment method'), max_length=255, - choices=settings.SITH_COUNTER_PAYMENT_METHOD) - # TODO: add the bank if the payment is made by cheque + choices=settings.SITH_COUNTER_PAYMENT_METHOD, default='cash') + bank = models.CharField(_('bank'), max_length=255, + choices=settings.SITH_COUNTER_BANK, default='other') def __str__(self): return "Refilling: %.2f for %s" % (self.amount, self.customer.user.get_display_name()) diff --git a/counter/templates/counter/counter_click.jinja b/counter/templates/counter/counter_click.jinja index 8fdb0b47..d478c6f9 100644 --- a/counter/templates/counter/counter_click.jinja +++ b/counter/templates/counter/counter_click.jinja @@ -29,8 +29,8 @@
Refilling
{% csrf_token %} + {{ refill_form.as_p() }} -
diff --git a/counter/views.py b/counter/views.py index 8d252c63..e778cfcf 100644 --- a/counter/views.py +++ b/counter/views.py @@ -44,6 +44,13 @@ class GetUserForm(forms.Form): cleaned_data['user_id'] = user.user.id return cleaned_data +class RefillForm(forms.ModelForm): + error_css_class = 'error' + required_css_class = 'required' + class Meta: + model = Refilling + fields = ['amount', 'payment_method', 'bank'] + class CounterMain(DetailView, ProcessFormView, FormMixin): """ The public (barman) view @@ -101,6 +108,7 @@ class CounterClick(DetailView): request.session['basket'] = {} request.session['basket_total'] = 0 request.session['not_enough'] = False + self.refill_form = None ret = super(CounterClick, self).get(request, *args, **kwargs) if len(Counter.get_barmen_list(self.object.id)) < 1: # Check that at least one barman is logged in return self.cancel(request) # Otherwise, go to main view @@ -110,6 +118,7 @@ class CounterClick(DetailView): """ Handle the many possibilities of the post request """ self.object = self.get_object() self.customer = Customer.objects.filter(user__id=self.kwargs['user_id']).first() + self.refill_form = None if len(Counter.get_barmen_list(self.object.id)) < 1: # Check that at least one barman is logged in return self.cancel(request) if 'basket' not in request.session.keys(): @@ -243,16 +252,21 @@ class CounterClick(DetailView): operator = self.customer.user else: operator = Counter.get_random_barman(self.object.id) - amount = float(request.POST['amount']) - s = Refilling(counter=self.object, operator=operator, customer=self.customer, - amount=amount, payment_method="cash") - s.save() + form = RefillForm(request.POST) + if form.is_valid(): + form.instance.counter = self.object + form.instance.operator = operator + form.instance.customer = self.customer + form.instance.save() + else: + self.refill_form = form def get_context_data(self, **kwargs): """ Add customer to the context """ kwargs = super(CounterClick, self).get_context_data(**kwargs) kwargs['customer'] = self.customer kwargs['basket_total'] = self.sum_basket(self.request) + kwargs['refill_form'] = self.refill_form or RefillForm() return kwargs class CounterLogin(RedirectView): diff --git a/sith/settings_sample.py b/sith/settings_sample.py index 66ce8e52..ef15da46 100644 --- a/sith/settings_sample.py +++ b/sith/settings_sample.py @@ -222,6 +222,13 @@ SITH_COUNTER_PAYMENT_METHOD = [ ('cash', 'Espèce'), ] +SITH_COUNTER_BANK = [ + ('other', 'Autre'), + ('la-poste', 'La Poste'), + ('credit-agricole', 'Credit Agricole'), + ('credit-mutuel', 'Credit Mutuel'), + ] + # Subscription durations are in semestres (should be settingized) SITH_SUBSCRIPTIONS = { 'un-semestre': { -- GitLab