diff --git a/counter/models.py b/counter/models.py index 196b474d..0d373d36 100644 --- a/counter/models.py +++ b/counter/models.py @@ -27,8 +27,10 @@ from django.utils.translation import ugettext_lazy as _ from django.utils import timezone from django.conf import settings from django.core.urlresolvers import reverse +from django.core.validators import MinLengthValidator from django.forms import ValidationError from django.utils.functional import cached_property +from django.core.exceptions import PermissionDenied from datetime import timedelta, date import random @@ -85,6 +87,29 @@ class Customer(models.Model): letter = random.choice(string.ascii_lowercase) 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): """ is_selling : tell if the current action is a selling @@ -744,7 +769,9 @@ class StudentCard(models.Model): 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, related_name="student_cards", diff --git a/counter/templates/counter/counter_click.jinja b/counter/templates/counter/counter_click.jinja index aee9c309..521ec4c9 100644 --- a/counter/templates/counter/counter_click.jinja +++ b/counter/templates/counter/counter_click.jinja @@ -30,6 +30,16 @@ {{ user_mini_profile(customer.user) }} {{ user_subscription(customer.user) }}
{% trans %}Amount: {% endtrans %}{{ customer.amount }} €
+