diff --git a/counter/migrations/0017_studentcard.py b/counter/migrations/0017_studentcard.py new file mode 100644 index 00000000..963f77db --- /dev/null +++ b/counter/migrations/0017_studentcard.py @@ -0,0 +1,41 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.13 on 2018-10-17 23:02 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [("counter", "0016_producttype_comment")] + + operations = [ + migrations.CreateModel( + name="StudentCard", + fields=[ + ( + "id", + models.AutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ( + "uid", + models.CharField(max_length=14, unique=True, verbose_name="uid"), + ), + ( + "customer", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + related_name="student_cards", + to="counter.Customer", + verbose_name="student cards", + ), + ), + ], + ) + ] diff --git a/counter/models.py b/counter/models.py index 9767cfd3..196b474d 100644 --- a/counter/models.py +++ b/counter/models.py @@ -732,3 +732,23 @@ class Eticket(models.Model): return hmac.new( bytes(self.secret, "utf-8"), bytes(string, "utf-8"), hashlib.sha1 ).hexdigest() + + +class StudentCard(models.Model): + """ + Alternative way to connect a customer into a counter + We are using Mifare DESFire EV1 specs since it's used for izly cards + https://www.nxp.com/docs/en/application-note/AN10927.pdf + UID is 7 byte long that means 14 hexa characters + """ + + UID_SIZE = 14 + + uid = models.CharField(_("uid"), max_length=14, unique=True) + customer = models.ForeignKey( + Customer, + related_name="student_cards", + verbose_name=_("student cards"), + null=False, + blank=False, + ) diff --git a/counter/views.py b/counter/views.py index 941cbc80..fd51b20f 100644 --- a/counter/views.py +++ b/counter/views.py @@ -57,6 +57,7 @@ from subscription.models import Subscription from counter.models import ( Counter, Customer, + StudentCard, Product, Selling, Refilling, @@ -121,9 +122,14 @@ class GetUserForm(forms.Form): cleaned_data = super(GetUserForm, self).clean() cus = None if cleaned_data["code"] != "": - cus = Customer.objects.filter( - account_id__iexact=cleaned_data["code"] - ).first() + if len(cleaned_data["code"]) == StudentCard.UID_SIZE: + card = StudentCard.objects.filter(uid=cleaned_data["code"]) + if card.exists(): + cus = card.first().customer + if cus is None: + cus = Customer.objects.filter( + account_id__iexact=cleaned_data["code"] + ).first() elif cleaned_data["id"] is not None: cus = Customer.objects.filter(user=cleaned_data["id"]).first() if cus is None or not cus.can_buy: