Update project

This commit is contained in:
2026-08-19 17:15:55 +03:30
parent 02d40e1238
commit f945f960c9
25 changed files with 1092 additions and 287 deletions

View File

@@ -20,6 +20,25 @@ class User(models.Model):
class Meta:
ordering = ['-created_at']
constraints = [
# 🔒 اصلاح race condition: قبلاً device_id هیچ قید یکتاییِ سطح
# DB نداشت، پس User.objects.get_or_create(device_id=...) در
# GuestSessionView در برابر دو درخواستِ هم‌زمان با همون
# device_id، می‌تونست دو کاربرِ مهمانِ جدا بسازه (هر دو create
# می‌شدن چون هیچ‌کدوم قبل از دیگری commit نشده بود که get بعدی
# پیداش کنه). این UniqueConstraint (فقط برای کاربرانِ مهمان و
# device_id غیرخالی — کاربرانِ ثبت‌نام‌شده با phone می‌تونن
# device_id مشترک/خالی داشته باشن) کافیه؛ چون Django's
# get_or_create از قبل خودش IntegrityError روی همین constraint
# رو می‌گیره و رکورد موجود رو دوباره get می‌کنه (نگاه کنید به
# پیاده‌سازی _create_object_from_params در Django)، پس نیازی به
# تغییر GuestSessionView نبود.
models.UniqueConstraint(
fields=['device_id'],
condition=models.Q(is_guest=True) & ~models.Q(device_id=None) & ~models.Q(device_id=''),
name='uniq_guest_device_id',
),
]
def __str__(self):
return self.name or self.phone or f"guest-{self.pk}"