mirror of
https://github.com/ae-utbm/sith.git
synced 2024-10-31 19:38:04 +00:00
Improve generation of account id
This commit is contained in:
parent
44a6621aac
commit
260a17ae4f
18
counter/migrations/0014_auto_20160804_1603.py
Normal file
18
counter/migrations/0014_auto_20160804_1603.py
Normal file
@ -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'},
|
||||
),
|
||||
]
|
@ -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:
|
||||
|
@ -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()
|
||||
|
@ -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})
|
||||
|
Loading…
Reference in New Issue
Block a user