为User problems_status 添加默认值,并精简了相关代码

This commit is contained in:
sxw@401 2015-09-15 20:28:58 +08:00
parent 21b18b8e97
commit 18e661ad68
5 changed files with 23 additions and 11 deletions

View File

@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('account', '0002_user_problems_status'),
]
operations = [
migrations.AlterField(
model_name='user',
name='problems_status',
field=models.TextField(default=b'{}'),
),
]

View File

@ -31,8 +31,7 @@ class User(AbstractBaseUser):
# 0代表不是管理员 1是普通管理员 2是超级管理员
admin_type = models.IntegerField(default=0)
# JSON字典用来表示该用户的问题的解决状态 1为ac2为正在进行
problems_status = models.TextField(blank=True)
problems_status = models.TextField(default="{}")
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = []

View File

@ -43,10 +43,7 @@ class MessageQueue(object):
except User.DoesNotExist:
logger.warning("Submission user does not exist, submission_id: " + submission_id)
continue
if user.problems_status:
problems_status = json.loads(user.problems_status)
else:
problems_status = {}
problems_status = json.loads(user.problems_status)
problems_status[str(problem.id)] = 1
user.problems_status = json.dumps(problems_status)
user.save()

View File

@ -282,7 +282,7 @@ def problem_list_page(request, page=1):
except Exception:
pass
if request.user.is_authenticated() and request.user.problems_status:
if request.user.is_authenticated():
problems_status = json.loads(request.user.problems_status)
else:
problems_status = {}

View File

@ -47,10 +47,7 @@ class SubmissionAPIView(APIView):
except Exception:
return error_response(u"提交判题任务失败")
# 修改用户解题状态
if request.user.problems_status:
problems_status = json.loads(request.user.problems_status)
else:
problems_status = {}
problems_status = json.loads(request.user.problems_status)
problems_status[str(data["problem_id"])] = 2
request.user.problems_status = json.dumps(problems_status)
request.user.save()