From 260a17ae4f9d1854d8592d921aff40e2109f6534 Mon Sep 17 00:00:00 2001 From: Skia Date: Fri, 5 Aug 2016 00:50:25 +0200 Subject: [PATCH] Improve generation of account id --- counter/migrations/0014_auto_20160804_1603.py | 18 ++++++++++++++++++ counter/models.py | 12 +++++++++--- eboutic/models.py | 3 ++- subscription/models.py | 2 +- 4 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 counter/migrations/0014_auto_20160804_1603.py diff --git a/counter/migrations/0014_auto_20160804_1603.py b/counter/migrations/0014_auto_20160804_1603.py new file mode 100644 index 00000000..de65ad2a --- /dev/null +++ b/counter/migrations/0014_auto_20160804_1603.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('counter', '0013_auto_20160801_2255'), + ] + + operations = [ + migrations.AlterModelOptions( + name='customer', + options={'verbose_name': 'customer', 'ordering': ['account_id'], 'verbose_name_plural': 'customers'}, + ), + ] diff --git a/counter/models.py b/counter/models.py index 21509732..e705e918 100644 --- a/counter/models.py +++ b/counter/models.py @@ -6,7 +6,8 @@ from django.core.urlresolvers import reverse from django.forms import ValidationError from datetime import timedelta -from random import randrange +import random +import string from club.models import Club from accounting.models import CurrencyField @@ -26,12 +27,17 @@ class Customer(models.Model): class Meta: verbose_name = _('customer') verbose_name_plural = _('customers') + ordering = ['account_id',] def __str__(self): return self.user.username - def generate_account_id(): - return randrange(0, 4000) # TODO: improve me! + def generate_account_id(number): + number = str(number) + letter = random.choice(string.ascii_lowercase) + while Customer.objects.filter(account_id=number+letter).exists(): + letter = random.choice(string.ascii_lowercase) + return number+letter def save(self, *args, **kwargs): if self.amount < 0: diff --git a/eboutic/models.py b/eboutic/models.py index 2b84b589..58ee1063 100644 --- a/eboutic/models.py +++ b/eboutic/models.py @@ -59,7 +59,8 @@ class Invoice(models.Model): raise DataError(_("Invoice already validated")) from counter.models import Customer if not Customer.objects.filter(user=self.user).exists(): - Customer(user=self.user, account_id=Customer.generate_account_id(), amount=0).save() + number = Customer.objects.last().account_id[:-1] + Customer(user=self.user, account_id=Customer.generate_account_id(number), amount=0).save() if self.payment_method == "SITH_ACCOUNT": self.user.customer.amount -= self.get_total() self.user.customer.save() diff --git a/subscription/models.py b/subscription/models.py index 7423cade..333951f4 100644 --- a/subscription/models.py +++ b/subscription/models.py @@ -51,7 +51,7 @@ class Subscription(models.Model): super(Subscription, self).save() from counter.models import Customer if not Customer.objects.filter(user=self.member).exists(): - Customer(user=self.member, account_id=Customer.generate_account_id(), amount=0).save() + Customer(user=self.member, account_id=Customer.generate_account_id(self.id), amount=0).save() def get_absolute_url(self): return reverse('core:user_profile', kwargs={'user_id': self.member.pk})