feat: add Eventify ID (EVT-XXXXXXXX) to User model and all APIs
- Add eventify_id CharField (unique, indexed, editable=False) to User - Auto-generate on save() with charset excluding I/O/0/1 for clarity - Migration 0012: add field nullable, backfill all existing users, make non-null - Sync migration 0011 (allowed_modules) pulled from server - Expose eventify_id in accounts/api.py, partner/api.py serializers - Expose eventify_id in mobile_api login response (populates localStorage)
This commit is contained in:
@@ -1,8 +1,18 @@
|
||||
import secrets
|
||||
|
||||
from django.contrib.auth.models import AbstractUser
|
||||
from django.db import models
|
||||
|
||||
from accounts.manager import UserManager
|
||||
from partner.models import Partner
|
||||
|
||||
EVENTIFY_ID_CHARS = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789' # no I, O, 0, 1
|
||||
|
||||
|
||||
def generate_eventify_id():
|
||||
return 'EVT-' + ''.join(secrets.choice(EVENTIFY_ID_CHARS) for _ in range(8))
|
||||
|
||||
|
||||
ROLE_CHOICES = (
|
||||
('admin', 'Admin'),
|
||||
('manager', 'Manager'),
|
||||
@@ -15,6 +25,15 @@ ROLE_CHOICES = (
|
||||
)
|
||||
|
||||
class User(AbstractUser):
|
||||
eventify_id = models.CharField(
|
||||
max_length=12,
|
||||
unique=True,
|
||||
editable=False,
|
||||
db_index=True,
|
||||
null=True,
|
||||
blank=True,
|
||||
)
|
||||
|
||||
phone_number = models.CharField(max_length=15, blank=True, null=True)
|
||||
role = models.CharField(max_length=20, choices=ROLE_CHOICES, default='Staff')
|
||||
|
||||
@@ -39,7 +58,7 @@ class User(AbstractUser):
|
||||
|
||||
allowed_modules = models.TextField(
|
||||
blank=True, null=True,
|
||||
help_text="Comma-separated module slugs this user can access"
|
||||
help_text='Comma-separated module slugs this user can access',
|
||||
)
|
||||
|
||||
ALL_MODULES = ["dashboard", "partners", "events", "ad-control", "users", "reviews", "contributions", "financials", "settings"]
|
||||
@@ -56,5 +75,14 @@ class User(AbstractUser):
|
||||
|
||||
objects = UserManager()
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if not self.eventify_id:
|
||||
for _ in range(10):
|
||||
candidate = generate_eventify_id()
|
||||
if not User.objects.filter(eventify_id=candidate).exists():
|
||||
self.eventify_id = candidate
|
||||
break
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
def __str__(self):
|
||||
return self.username
|
||||
|
||||
Reference in New Issue
Block a user