Improve generation of account id

This commit is contained in:
Skia 2016-08-05 00:50:25 +02:00
parent 44a6621aac
commit 260a17ae4f
4 changed files with 30 additions and 5 deletions

View 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'},
),
]

View File

@ -6,7 +6,8 @@ from django.core.urlresolvers import reverse
from django.forms import ValidationError from django.forms import ValidationError
from datetime import timedelta from datetime import timedelta
from random import randrange import random
import string
from club.models import Club from club.models import Club
from accounting.models import CurrencyField from accounting.models import CurrencyField
@ -26,12 +27,17 @@ class Customer(models.Model):
class Meta: class Meta:
verbose_name = _('customer') verbose_name = _('customer')
verbose_name_plural = _('customers') verbose_name_plural = _('customers')
ordering = ['account_id',]
def __str__(self): def __str__(self):
return self.user.username return self.user.username
def generate_account_id(): def generate_account_id(number):
return randrange(0, 4000) # TODO: improve me! 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): def save(self, *args, **kwargs):
if self.amount < 0: if self.amount < 0:

View File

@ -59,7 +59,8 @@ class Invoice(models.Model):
raise DataError(_("Invoice already validated")) raise DataError(_("Invoice already validated"))
from counter.models import Customer from counter.models import Customer
if not Customer.objects.filter(user=self.user).exists(): 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": if self.payment_method == "SITH_ACCOUNT":
self.user.customer.amount -= self.get_total() self.user.customer.amount -= self.get_total()
self.user.customer.save() self.user.customer.save()

View File

@ -51,7 +51,7 @@ class Subscription(models.Model):
super(Subscription, self).save() super(Subscription, self).save()
from counter.models import Customer from counter.models import Customer
if not Customer.objects.filter(user=self.member).exists(): 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): def get_absolute_url(self):
return reverse('core:user_profile', kwargs={'user_id': self.member.pk}) return reverse('core:user_profile', kwargs={'user_id': self.member.pk})