Gui for studentcards in counters

This commit is contained in:
Antoine Bartuccio 2018-10-19 01:21:57 +02:00 committed by Bartuccio Antoine
parent 14d9fc04d1
commit 4669e5a4e9
Signed by: klmp200
GPG Key ID: E7245548C53F904B
3 changed files with 55 additions and 1 deletions

View File

@ -27,8 +27,10 @@ from django.utils.translation import ugettext_lazy as _
from django.utils import timezone from django.utils import timezone
from django.conf import settings from django.conf import settings
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.core.validators import MinLengthValidator
from django.forms import ValidationError from django.forms import ValidationError
from django.utils.functional import cached_property from django.utils.functional import cached_property
from django.core.exceptions import PermissionDenied
from datetime import timedelta, date from datetime import timedelta, date
import random import random
@ -85,6 +87,29 @@ class Customer(models.Model):
letter = random.choice(string.ascii_lowercase) letter = random.choice(string.ascii_lowercase)
return number + letter return number + letter
def add_student_card(self, uid, request, counter=None):
"""
Add a new student card on the customer account
"""
# If you are comming from a counter, only your connection to the counter is checked, not your right on the user to avoid wierd conflicts
if counter != None and (
counter.type != "BAR"
or not (
"counter_token" in request.session.keys()
and request.session["counter_token"] == counter.token
)
or len(counter.get_barmen_list()) < 1
):
raise PermissionDenied
# If you are not comming from a counter, your permissions are checked
if not (
request.user.id == self.user.id
or request.user.is_board_member
or request.user.is_root
):
raise PermissionDenied
StudentCard(customer=self, uid=uid).save()
def save(self, allow_negative=False, is_selling=False, *args, **kwargs): def save(self, allow_negative=False, is_selling=False, *args, **kwargs):
""" """
is_selling : tell if the current action is a selling is_selling : tell if the current action is a selling
@ -744,7 +769,9 @@ class StudentCard(models.Model):
UID_SIZE = 14 UID_SIZE = 14
uid = models.CharField(_("uid"), max_length=14, unique=True) uid = models.CharField(
_("uid"), max_length=14, unique=True, validators=[MinLengthValidator(4)]
)
customer = models.ForeignKey( customer = models.ForeignKey(
Customer, Customer,
related_name="student_cards", related_name="student_cards",

View File

@ -30,6 +30,16 @@
{{ user_mini_profile(customer.user) }} {{ user_mini_profile(customer.user) }}
{{ user_subscription(customer.user) }} {{ user_subscription(customer.user) }}
<p>{% trans %}Amount: {% endtrans %}{{ customer.amount }} €</p> <p>{% trans %}Amount: {% endtrans %}{{ customer.amount }} €</p>
<form method="post" action="{{ url('counter:click', counter_id=counter.id, user_id=customer.user.id) }}">
{% csrf_token %}
<input type="hidden" name="action" value="add_student_card">
{% trans %}Add a student card{% endtrans %}
<input type="input" name="student_card_uid" />
{% if request.session['not_valid_student_card_uid'] %}
<p><strong>{% trans %}This is not a valid student card UID{% endtrans %}</strong></p>
{% endif %}
<input type="submit" value="{% trans %}Go{% endtrans %}" />
</form>
</div> </div>
<div id="bar_ui"> <div id="bar_ui">
<h5>{% trans %}Selling{% endtrans %}</h5> <h5>{% trans %}Selling{% endtrans %}</h5>

View File

@ -380,6 +380,7 @@ class CounterClick(CounterTabsMixin, CanViewMixin, DetailView):
request.session["too_young"] = False request.session["too_young"] = False
request.session["not_allowed"] = False request.session["not_allowed"] = False
request.session["no_age"] = False request.session["no_age"] = False
request.session["not_valid_student_card_uid"] = False
if self.object.type != "BAR": if self.object.type != "BAR":
self.operator = request.user self.operator = request.user
elif self.is_barman_price(): elif self.is_barman_price():
@ -389,6 +390,8 @@ class CounterClick(CounterTabsMixin, CanViewMixin, DetailView):
if "add_product" in request.POST["action"]: if "add_product" in request.POST["action"]:
self.add_product(request) self.add_product(request)
elif "add_student_card" in request.POST["action"]:
self.add_student_card(request)
elif "del_product" in request.POST["action"]: elif "del_product" in request.POST["action"]:
self.del_product(request) self.del_product(request)
elif "refill" in request.POST["action"]: elif "refill" in request.POST["action"]:
@ -525,6 +528,19 @@ class CounterClick(CounterTabsMixin, CanViewMixin, DetailView):
request.session.modified = True request.session.modified = True
return True return True
def add_student_card(self, request):
"""
Add a new student card on the customer account
"""
uid = request.POST["student_card_uid"]
uid = str(uid)
if len(uid) != StudentCard.UID_SIZE:
request.session["not_valid_student_card_uid"] = True
return False
self.customer.add_student_card(uid, request, self.object)
return True
def del_product(self, request): def del_product(self, request):
""" Delete a product from the basket """ """ Delete a product from the basket """
pid = str(request.POST["product_id"]) pid = str(request.POST["product_id"])
@ -648,6 +664,7 @@ class CounterClick(CounterTabsMixin, CanViewMixin, DetailView):
kwargs["basket_total"] = self.sum_basket(self.request) kwargs["basket_total"] = self.sum_basket(self.request)
kwargs["refill_form"] = self.refill_form or RefillForm() kwargs["refill_form"] = self.refill_form or RefillForm()
kwargs["categories"] = ProductType.objects.all() kwargs["categories"] = ProductType.objects.all()
kwargs["student_card_max_uid_size"] = StudentCard.UID_SIZE
return kwargs return kwargs