Skip to content

Commit

Permalink
Use AccountLog and SpecialProfileUsedRecord to replace UstcSnos and U…
Browse files Browse the repository at this point in the history
…stcEligible

Please note that this migration is DESTRUCTIVE:
original UstcSnos and UstcEligible will be deleted!
  • Loading branch information
taoky committed Oct 12, 2023
1 parent fe612aa commit a883560
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 21 deletions.
4 changes: 2 additions & 2 deletions frontend/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
14 changes: 7 additions & 7 deletions frontend/auth_providers/ustc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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


Expand Down
Original file line number Diff line number Diff line change
@@ -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",
),
]
19 changes: 14 additions & 5 deletions frontend/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
14 changes: 7 additions & 7 deletions frontend/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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

Expand All @@ -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')


Expand Down

0 comments on commit a883560

Please sign in to comment.