Tutor (prompt tutor-v3-stepwise-plain): plain text instead of LaTeX/markdown, with rag/services/mathtext.py converting whatever still slips through; full step-by-step solutions with a check before the final answer; three tones by grade; the last 6 messages of the conversation go to the model and follow-ups are no longer served from another user's cache; book solutions the checker disputes are kept out of the context; a questionId gives the tutor the practice question and the student's wrong answer. /api/health reports when the AI credit runs out. rag_model_benchmark compares chat models on verified book problems (run on the server; credit ran out mid-run). Map: only lessons that have questions count toward finishing an island, lessonIds[0] / nextLessonId is always the next lesson to play, and a finished island stays finished. The Seyar characters (sigma/delta/pi) are seeded. Audit fixes, each reproduced before fixing: - the plan is stored on the order, so paying a monthly order after starting a yearly checkout no longer grants a year - opening checkout no longer demotes an active subscription; renewals extend from the current expiry; expired subscriptions are not reported active - one discount code per order and only before the gateway; a 100% code completes the purchase; lost Zarinpal confirmations are re-verified - the unused /api/payment/webhook "mark paid" endpoint is removed - XP counts only answers recorded on the server - 6-digit OTP, each code dies after 5 wrong guesses; POST /api/auth/logout - a guest who signs up keeps their account and progress (test phones excluded) - affiliate commission on the first real payment; guests excluded - parents with several children no longer get a 500 - SQLite runs transactions IMMEDIATE, since it ignores select_for_update - admin login is rate-limited; display names drop phone numbers and links; userQuestion is capped at 2000 characters - server-side heart enforcement exists behind HEARTS_ENFORCED (off until the frontend handles 403 no_hearts) Not done on purpose: rotating the server's secret key and moving to Postgres. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
87 lines
4.3 KiB
Python
87 lines
4.3 KiB
Python
from django.db import models
|
||
|
||
|
||
class User(models.Model):
|
||
"""
|
||
کاربر اصلی سیستم (مهمان یا ثبتنامشده با شماره).
|
||
توجه: این مدل، AUTH_USER_MODEL جنگو نیست؛ یک مدل بیزنسی مستقله که
|
||
با TadrisinoTokenAuthentication سفارشی احراز هویت میشه.
|
||
|
||
grade_id فقط یک ارجاع سبک (بدون FK واقعی) به content.Grade هست؛
|
||
این اپ عمداً هیچوقت FK واقعی به مدل اپ دیگه نمیزنه.
|
||
"""
|
||
phone = models.CharField(max_length=15, unique=True, null=True, blank=True, db_index=True)
|
||
device_id = models.CharField(max_length=64, null=True, blank=True, db_index=True)
|
||
name = models.CharField(max_length=100, blank=True)
|
||
avatar = models.CharField(max_length=100, blank=True)
|
||
is_guest = models.BooleanField(default=True)
|
||
grade_id = models.IntegerField(null=True, blank=True) # ارجاع سبک به content.Grade
|
||
created_at = models.DateTimeField(auto_now_add=True)
|
||
# آخرین زمانِ فعالیتِ کاربر (لاگین موفق، رفرش توکن موفق، یا سشنِ
|
||
# مهمانِ واقعی). فقط برای هدفگیریِ آیندهی کمپینهای پیامی نگهداری
|
||
# میشه؛ در حال حاضر هیچ منطقِ دیگهای به این فیلد وابسته نیست.
|
||
last_activity_at = models.DateTimeField(null=True, blank=True)
|
||
# access tokenهای صادرشده پیش از این لحظه رد میشوند (خروج از حساب). توکنِ
|
||
# access فقط user_id امضاشده است و به Session وصل نیست؛ بدونِ این، خروج
|
||
# فقط refresh را باطل میکرد و توکنِ فعلی تا یک ساعت کار میکرد.
|
||
tokens_valid_after = models.DateTimeField(null=True, blank=True)
|
||
|
||
class Meta:
|
||
ordering = ['-created_at']
|
||
constraints = [
|
||
# جلوگیری از ساخت دو کاربر مهمان همزمان با یک device_id یکسان.
|
||
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}"
|
||
|
||
@property
|
||
def is_authenticated(self):
|
||
# این مدل AUTH_USER_MODEL نیست؛ این property فقط برای سازگاری با
|
||
# rest_framework.permissions.IsAuthenticated لازمه.
|
||
return True
|
||
|
||
|
||
class Profile(models.Model):
|
||
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile')
|
||
birth_year = models.PositiveSmallIntegerField(null=True, blank=True)
|
||
parent_phone = models.CharField(max_length=15, blank=True)
|
||
bio = models.CharField(max_length=200, blank=True)
|
||
|
||
def __str__(self):
|
||
return f"profile-{self.user_id}"
|
||
|
||
|
||
class Session(models.Model):
|
||
"""
|
||
نگهداری refresh_token هر ورود. توکن دسترسی (access token) در دیتابیس
|
||
ذخیره نمیشه؛ بهصورت stateless امضا (signed) میشه — دقیقاً طبق فیلدهای
|
||
همین مدل که فقط refresh_token داره، نه access token.
|
||
"""
|
||
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='sessions')
|
||
refresh_token = models.CharField(max_length=200, unique=True)
|
||
device_id = models.CharField(max_length=64, blank=True)
|
||
expires_at = models.DateTimeField()
|
||
revoked = models.BooleanField(default=False)
|
||
|
||
def __str__(self):
|
||
return f"session-{self.pk}-user{self.user_id}"
|
||
|
||
|
||
class OTPCode(models.Model):
|
||
phone = models.CharField(max_length=15, db_index=True)
|
||
code = models.CharField(max_length=6)
|
||
expires_at = models.DateTimeField()
|
||
is_used = models.BooleanField(default=False)
|
||
# تلاشهای غلط روی همین کد. در دیتابیس شمرده میشود، نه در کشِ LocMem که
|
||
# برای هر worker جداست؛ بعد از OTP_MAX_FAILED_ATTEMPTS کد باطل میشود.
|
||
failed_attempts = models.PositiveSmallIntegerField(default=0)
|
||
|
||
def __str__(self):
|
||
return f"otp-{self.phone}"
|