diff --git a/frontend/admin.py b/frontend/admin.py index cee93ba..3b5bc7f 100644 --- a/frontend/admin.py +++ b/frontend/admin.py @@ -9,9 +9,9 @@ from server.terms.models import Terms from server.trigger.models import Trigger from server.user.models import User -from .models import Page, Account, Code, UstcSnos, UstcEligible, Qa, Credits +from .models import Page, Account, Code, AccountLog, SpecialProfileUsedRecord, Qa, Credits -admin.site.register([Page, Account, Code, UstcSnos, UstcEligible, Qa, Credits]) +admin.site.register([Page, Account, Code, AccountLog, SpecialProfileUsedRecord, Qa, Credits]) class PermissionListFilter(admin.SimpleListFilter): diff --git a/frontend/auth_providers/ustc.py b/frontend/auth_providers/ustc.py index 211cb8c..1005026 100644 --- a/frontend/auth_providers/ustc.py +++ b/frontend/auth_providers/ustc.py @@ -7,7 +7,7 @@ from django.shortcuts import redirect from django.urls import path -from ..models import UstcSnos +from ..models import AccountLog from .base import BaseLoginView @@ -53,13 +53,13 @@ def to_set(s): def from_set(vs): return ','.join(sorted(vs)) try: - o = account.ustcsnos - new_snos = from_set(to_set(o.snos) | {self.sno}) - if new_snos != o.snos: - o.snos = new_snos + o = AccountLog.objects.get(account=account, content_type='学号') + new_snos = from_set(to_set(o.contents) | {self.sno}) + if new_snos != o.contents: + o.contents = new_snos o.save() - except UstcSnos.DoesNotExist: - UstcSnos.objects.create(account=account, snos=self.sno) + except AccountLog.DoesNotExist: + AccountLog.objects.create(account=account, contents=f"{self.sno}", content_type='学号') return account diff --git a/frontend/migrations/0007_accountlog_specialprofileusedrecord_and_more.py b/frontend/migrations/0007_accountlog_specialprofileusedrecord_and_more.py new file mode 100644 index 0000000..bca3a30 --- /dev/null +++ b/frontend/migrations/0007_accountlog_specialprofileusedrecord_and_more.py @@ -0,0 +1,62 @@ +# Generated by Django 4.2.5 on 2023-10-12 17:46 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + dependencies = [ + ("auth", "0012_alter_user_first_name_max_length"), + ("frontend", "0006_credits"), + ] + + operations = [ + migrations.CreateModel( + name="AccountLog", + fields=[ + ( + "id", + models.AutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("contents", models.TextField()), + ("content_type", models.CharField(default="学号", max_length=32)), + ( + "account", + models.OneToOneField( + on_delete=django.db.models.deletion.CASCADE, + to="frontend.account", + ), + ), + ], + ), + migrations.CreateModel( + name="SpecialProfileUsedRecord", + fields=[ + ( + "user", + models.OneToOneField( + on_delete=django.db.models.deletion.CASCADE, + primary_key=True, + serialize=False, + to=settings.AUTH_USER_MODEL, + ), + ), + ], + ), + migrations.RemoveField( + model_name="ustcsnos", + name="account", + ), + migrations.DeleteModel( + name="UstcEligible", + ), + migrations.DeleteModel( + name="UstcSnos", + ), + ] diff --git a/frontend/models.py b/frontend/models.py index 0ea3ed1..cf14cdd 100644 --- a/frontend/models.py +++ b/frontend/models.py @@ -71,14 +71,23 @@ def authenticate(cls, provider, identity, code): return False -class UstcSnos(models.Model): - account = models.OneToOneField(Account, models.CASCADE, primary_key=True) - snos = models.TextField() +# 记录特殊登录方式(例如 USTC CAS)的用户,其登录方式返回的「可靠」信息 +class AccountLog(models.Model): + account = models.OneToOneField(Account, models.CASCADE, db_index=True) + contents = models.TextField() + content_type = models.CharField(max_length=32, default='学号') + + def __str__(self): + return f"{self.account.pk} ({self.contents})" -class UstcEligible(models.Model): +# 记录需要在首次登录后显示换组页面并且已经换组的用户 +# 目前只有 USTC 有这个需求 +class SpecialProfileUsedRecord(models.Model): user = models.OneToOneField(get_user_model(), models.CASCADE, primary_key=True) - eligible = models.BooleanField() + + def __str__(self) -> str: + return f"{self.user.pk}" class Qa(models.Model): diff --git a/frontend/views.py b/frontend/views.py index 5de5567..20dc46f 100644 --- a/frontend/views.py +++ b/frontend/views.py @@ -18,7 +18,7 @@ from server.context import Context from server.exceptions import Error, NotFound, WrongFormat -from frontend.models import Account, Credits, Qa, UstcEligible +from frontend.models import Account, Credits, Qa, SpecialProfileUsedRecord # noinspection PyMethodMayBeStatic @@ -27,8 +27,8 @@ def get(self, request): if request.user.is_authenticated: if Account.objects.filter(provider='ustc', user=request.user).exists(): try: - request.user.ustceligible - except UstcEligible.DoesNotExist: + request.user.specialprofileusedrecord + except SpecialProfileUsedRecord.DoesNotExist: return redirect('ustcprofile') context = Context.from_request(request) try: @@ -228,9 +228,9 @@ def check(self): if request.user.is_authenticated: if Account.objects.filter(provider='ustc', user=request.user).exists(): try: - request.user.ustceligible + request.user.specialprofileusedrecord return False - except UstcEligible.DoesNotExist: + except SpecialProfileUsedRecord.DoesNotExist: return True return False @@ -244,11 +244,11 @@ def post(self, request): return redirect('hub') eligible = request.POST['eligible'] if eligible == 'yes': - UstcEligible.objects.create(user=request.user, eligible=True) + SpecialProfileUsedRecord.objects.create(user=request.user, eligible=True) user = User.get(Context.from_request(request).copy(elevated=True), request.user.pk) user.update(group='ustc') elif eligible == 'no': - UstcEligible.objects.create(user=request.user, eligible=False) + SpecialProfileUsedRecord.objects.create(user=request.user, eligible=False) return redirect('hub')