Skip to content

Commit

Permalink
fix: fix using random generators for models' field
Browse files Browse the repository at this point in the history
  • Loading branch information
Adibov committed Jan 11, 2024
1 parent 77dcaaa commit d21b327
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 4.2.4 on 2024-01-11 13:15

from django.db import migrations, models
import utils.random


class Migration(migrations.Migration):

dependencies = [
('backend_api', '0058_presentation_recorded_link_workshop_recorded_link_and_more'),
]

operations = [
migrations.AlterField(
model_name='discount',
name='code',
field=models.CharField(default=utils.random.random_discount_code, max_length=32, unique=True),
),
migrations.AlterField(
model_name='presentationparticipation',
name='password',
field=models.CharField(default=utils.random.random_password, max_length=255),
),
migrations.AlterField(
model_name='workshopregistration',
name='password',
field=models.CharField(default=utils.random.random_password, max_length=255),
),
]
12 changes: 5 additions & 7 deletions backend/backend_api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from aaiss_backend.settings import BASE_URL
from backend_api import validators
from backend_api.email import MailerThread
from utils.random import create_random_string
from utils.random import create_random_string, random_password, random_discount_code
from utils.renderers import new_detailed_response

SMALL_MAX_LENGTH = 255
Expand Down Expand Up @@ -288,11 +288,10 @@ class StatusChoices(models.IntegerChoices):
AWAITING_PAYMENT = 1, _('Waiting for payment')
PURCHASED = 2, _('Purchase confirmed')

_PASSWORD_LENGTH = 16
workshop = models.ForeignKey(Workshop, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE)
status = models.IntegerField(choices=StatusChoices.choices, default=StatusChoices.AWAITING_PAYMENT)
password = models.CharField(max_length=SMALL_MAX_LENGTH, default=create_random_string(_PASSWORD_LENGTH))
password = models.CharField(max_length=SMALL_MAX_LENGTH, default=random_password)

class Meta:
unique_together = ('workshop', 'user',)
Expand All @@ -307,11 +306,10 @@ class StatusChoices(models.IntegerChoices):
AWAITING_PAYMENT = 1, _('Waiting for payment')
PURCHASED = 2, _('Purchase confirmed')

_PASSWORD_LENGTH = 16
presentation = models.ForeignKey(Presentation, on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete=models.CASCADE)
status = models.IntegerField(choices=StatusChoices.choices, default=StatusChoices.AWAITING_PAYMENT)
password = models.CharField(max_length=SMALL_MAX_LENGTH, default=create_random_string(_PASSWORD_LENGTH))
password = models.CharField(max_length=SMALL_MAX_LENGTH, default=random_password)

class Meta:
unique_together = ('presentation', 'user',)
Expand Down Expand Up @@ -357,8 +355,8 @@ def __str__(self):


class Discount(models.Model):
_CODE_LENGTH = 32
code = models.CharField(max_length=_CODE_LENGTH, null=False, default=create_random_string(_CODE_LENGTH),
_MAX_CODE_LENGTH = 32
code = models.CharField(max_length=_MAX_CODE_LENGTH, null=False, default=random_discount_code,
unique=True)
discount_percent = models.IntegerField(default=0)
is_active = models.BooleanField(default=False)
Expand Down
15 changes: 14 additions & 1 deletion backend/utils/random.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
import random
import string

_PASSWORD_LENGTH = 16
_DISCOUNT_CODE_LENGTH = 32


def create_random_string(length: int) -> str:
return ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in range(length))
return ''.join(
random.SystemRandom().choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in
range(length))


def random_password() -> str:
return create_random_string(_PASSWORD_LENGTH)


def random_discount_code() -> str:
return create_random_string(_DISCOUNT_CODE_LENGTH)

0 comments on commit d21b327

Please sign in to comment.