From 4b315c3ecf3d92458472b1f00b59388e06893a2a Mon Sep 17 00:00:00 2001 From: Maxime Bombar Date: Thu, 25 Oct 2018 14:20:52 +0200 Subject: [PATCH] [Printer] Digicode are stored in bdd --- printer/migrations/0010_auto_20181025_1257.py | 38 +++++++++++++++++++ printer/models.py | 25 +++++++++++- printer/utils.py | 15 ++------ 3 files changed, 64 insertions(+), 14 deletions(-) create mode 100644 printer/migrations/0010_auto_20181025_1257.py diff --git a/printer/migrations/0010_auto_20181025_1257.py b/printer/migrations/0010_auto_20181025_1257.py new file mode 100644 index 00000000..f9dea3f3 --- /dev/null +++ b/printer/migrations/0010_auto_20181025_1257.py @@ -0,0 +1,38 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.7 on 2018-10-25 10:57 +from __future__ import unicode_literals + +from django.conf import settings +import django.core.files.storage +from django.db import migrations, models +import django.db.models.deletion +import printer.utils +import printer.validators +import re2o.mixins + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('printer', '0009_jobwithoptions_paid'), + ] + + operations = [ + migrations.CreateModel( + name='Digicode', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('code', models.BigIntegerField(default=0, unique=True)), + ('created', models.DateTimeField(auto_now_add=True)), + ('used_time', models.DateTimeField(null=True)), + ('user', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL)), + ], + bases=(re2o.mixins.RevMixin, models.Model), + ), + migrations.AlterField( + model_name='jobwithoptions', + name='file', + field=models.FileField(storage=django.core.files.storage.FileSystemStorage(location='/var/impressions'), upload_to=printer.models.user_printing_path, validators=[printer.validators.FileValidator(allowed_types=['application/pdf'], max_size=41943040)]), + ), + ] diff --git a/printer/models.py b/printer/models.py index 349d2301..d8dc8c11 100644 --- a/printer/models.py +++ b/printer/models.py @@ -11,6 +11,7 @@ from numpy.random import randint import unidecode from django.core.files.storage import FileSystemStorage +from django.core.exceptions import ObjectDoesNotExist from django.db import models from django.forms import ValidationError @@ -21,8 +22,6 @@ from re2o.mixins import RevMixin import users.models -import unidecode - from .validators import ( FileValidator, ) @@ -54,6 +53,28 @@ def user_printing_path(instance, filename): return 'printings/user_{0}/{1}'.format(instance.user.id, unidecode.unidecode(filename)) +class Digicode(RevMixin, models.Model): + """ + This is a model to represent a digicode, maybe should be an external app. + """ + code = models.BigIntegerField(default=0, unique=True) + user = models.ForeignKey('users.User', on_delete=models.PROTECT) + created = models.DateTimeField(auto_now_add=True) + used_time = models.DateTimeField(null=True) + + def _gen_code(user): + try_again = True + while try_again: + try: + code = randint(695895, 6958942)*1437+38 + Digicode.objects.get(code=code) + except ObjectDoesNotExist: + try_again = False + digicode = Digicode.objects.create(code=code, user=user) + digicode.save() + return (str(code) + '#') + + class JobWithOptions(RevMixin, models.Model): """ This is the main model of printer application : diff --git a/printer/utils.py b/printer/utils.py index 1c67897a..541bc1db 100644 --- a/printer/utils.py +++ b/printer/utils.py @@ -5,9 +5,9 @@ import os from django.template.loader import get_template from django.core.mail import EmailMessage -from preferences.models import OptionalPrinter, GeneralOption, AssoOption +from preferences.models import GeneralOption, AssoOption -from numpy.random import randint +from .models import Digicode import datetime @@ -54,20 +54,11 @@ def pdfbook(file_path): ]) return newfile - -def gen_code(): - code = randint(10**9, 10**10-1) - while code % 1437 != 38: - code = randint(10**9, 10**10-1) - return (str(code) + '#') - - def send_mail_printer(client): """Sends an email to the client explaning how to get the printings""" template = get_template('printer/email_printer') - code = gen_code() - + code = Digicode._gen_code(client) printer_access_fr = "au quatrième (4) étage du bâtiment J (code B7806)" printer_access_en = "on fourth (4th) floor of building J (code B7806)"