mirror of
https://github.com/ae-utbm/sith.git
synced 2025-07-11 04:19:25 +00:00
Merge pull request #499 from ae-utbm/unify-account-creation
Unify account id creation
This commit is contained in:
@ -21,6 +21,9 @@
|
||||
# Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
#
|
||||
#
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Tuple
|
||||
|
||||
from django.db import models
|
||||
from django.db.models.functions import Length
|
||||
@ -57,7 +60,7 @@ class Customer(models.Model):
|
||||
|
||||
user = models.OneToOneField(User, primary_key=True, on_delete=models.CASCADE)
|
||||
account_id = models.CharField(_("account id"), max_length=10, unique=True)
|
||||
amount = CurrencyField(_("amount"))
|
||||
amount = CurrencyField(_("amount"), default=0)
|
||||
recorded_products = models.IntegerField(_("recorded product"), default=0)
|
||||
|
||||
class Meta:
|
||||
@ -94,12 +97,27 @@ class Customer(models.Model):
|
||||
) < timedelta(days=90)
|
||||
|
||||
@classmethod
|
||||
def new_for_user(cls, user: User):
|
||||
def get_or_create(cls, user: User) -> Tuple[Customer, bool]:
|
||||
"""
|
||||
Create a new Customer instance for the user given in parameter without saving it
|
||||
The account if is automatically generated and the amount set at 0
|
||||
Work in pretty much the same way as the usual get_or_create method,
|
||||
but with the default field replaced by some under the hood.
|
||||
|
||||
If the user has an account, return it as is.
|
||||
Else create a new account with no money on it and a new unique account id
|
||||
|
||||
Example : ::
|
||||
|
||||
user = User.objects.get(pk=1)
|
||||
account, created = Customer.get_or_create(user)
|
||||
if created:
|
||||
print(f"created a new account with id {account.id}")
|
||||
else:
|
||||
print(f"user has already an account, with {account.id} € on it"
|
||||
"""
|
||||
# account_id are number with a letter appended
|
||||
if hasattr(user, "customer"):
|
||||
return user.customer, False
|
||||
|
||||
# account_id are always a number with a letter appended
|
||||
account_id = (
|
||||
Customer.objects.order_by(Length("account_id"), "account_id")
|
||||
.values("account_id")
|
||||
@ -107,14 +125,19 @@ class Customer(models.Model):
|
||||
)
|
||||
if account_id is None:
|
||||
# legacy from the old site
|
||||
return cls(user=user, account_id="1504a", amount=0)
|
||||
account_id = account_id["account_id"]
|
||||
num = int(account_id[:-1])
|
||||
while Customer.objects.filter(account_id=account_id).exists():
|
||||
num += 1
|
||||
account_id = str(num) + random.choice(string.ascii_lowercase)
|
||||
account = cls.objects.create(user=user, account_id="1504a")
|
||||
return account, True
|
||||
|
||||
return cls(user=user, account_id=account_id, amount=0)
|
||||
account_id = account_id["account_id"]
|
||||
account_num = int(account_id[:-1])
|
||||
while Customer.objects.filter(account_id=account_id).exists():
|
||||
# when entering the first iteration, we are using an already existing account id
|
||||
# so the loop should always execute at least one time
|
||||
account_num += 1
|
||||
account_id = f"{account_num}{random.choice(string.ascii_lowercase)}"
|
||||
|
||||
account = cls.objects.create(user=user, account_id=account_id)
|
||||
return account, True
|
||||
|
||||
def save(self, allow_negative=False, is_selling=False, *args, **kwargs):
|
||||
"""
|
||||
|
Reference in New Issue
Block a user