2015-06-26 07:59:53 +00:00
|
|
|
from django.contrib.auth.models import AbstractBaseUser
|
2016-09-25 06:07:45 +00:00
|
|
|
from django.db import models
|
2015-09-24 07:47:05 +00:00
|
|
|
from jsonfield import JSONField
|
2015-09-24 06:55:20 +00:00
|
|
|
|
2016-06-23 04:19:16 +00:00
|
|
|
|
|
|
|
class AdminType(object):
|
2016-10-29 18:17:35 +00:00
|
|
|
REGULAR_USER = "regular_user"
|
|
|
|
ADMIN = "admin"
|
|
|
|
SUPER_ADMIN = "super_admin"
|
2016-06-23 04:19:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ProblemSolutionStatus(object):
|
|
|
|
ACCEPTED = 1
|
|
|
|
PENDING = 2
|
|
|
|
|
|
|
|
|
2015-06-29 04:43:17 +00:00
|
|
|
class UserManager(models.Manager):
|
|
|
|
use_in_migrations = True
|
|
|
|
|
|
|
|
def get_by_natural_key(self, username):
|
|
|
|
return self.get(**{self.model.USERNAME_FIELD: username})
|
|
|
|
|
|
|
|
|
2015-06-26 07:59:53 +00:00
|
|
|
class User(AbstractBaseUser):
|
|
|
|
username = models.CharField(max_length=30, unique=True)
|
2016-09-25 09:00:52 +00:00
|
|
|
real_name = models.CharField(max_length=30, null=True)
|
|
|
|
email = models.EmailField(max_length=254, null=True)
|
2015-10-12 10:22:24 +00:00
|
|
|
create_time = models.DateTimeField(auto_now_add=True, null=True)
|
2016-06-23 04:19:16 +00:00
|
|
|
# One of UserType
|
2016-10-29 18:17:35 +00:00
|
|
|
admin_type = models.CharField(max_length=24, default=AdminType.REGULAR_USER)
|
2016-09-25 09:00:52 +00:00
|
|
|
reset_password_token = models.CharField(max_length=40, null=True)
|
2016-10-29 18:17:35 +00:00
|
|
|
reset_password_token_expire_time = models.DateTimeField(null=True)
|
2016-06-23 04:19:16 +00:00
|
|
|
# SSO auth token
|
2016-09-25 09:00:52 +00:00
|
|
|
auth_token = models.CharField(max_length=40, null=True)
|
2015-12-11 14:43:23 +00:00
|
|
|
two_factor_auth = models.BooleanField(default=False)
|
2016-09-25 09:00:52 +00:00
|
|
|
tfa_token = models.CharField(max_length=40, null=True)
|
2016-02-17 01:45:35 +00:00
|
|
|
# open api key
|
2016-06-23 04:19:16 +00:00
|
|
|
open_api = models.BooleanField(default=False)
|
2016-09-25 09:00:52 +00:00
|
|
|
open_api_appkey = models.CharField(max_length=35, null=True)
|
2016-06-23 04:19:16 +00:00
|
|
|
is_disabled = models.BooleanField(default=False)
|
2015-06-26 07:59:53 +00:00
|
|
|
|
2017-01-23 08:01:56 +00:00
|
|
|
USERNAME_FIELD = "username"
|
2015-06-29 04:43:17 +00:00
|
|
|
REQUIRED_FIELDS = []
|
|
|
|
|
|
|
|
objects = UserManager()
|
|
|
|
|
2016-09-25 06:07:45 +00:00
|
|
|
def is_admin(self):
|
2016-10-29 18:17:35 +00:00
|
|
|
return self.admin_type in [AdminType.ADMIN, AdminType.SUPER_ADMIN]
|
2016-09-25 06:07:45 +00:00
|
|
|
|
2015-06-26 07:59:53 +00:00
|
|
|
class Meta:
|
|
|
|
db_table = "user"
|
2015-10-25 07:30:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _random_avatar():
|
|
|
|
import random
|
|
|
|
return "/static/img/avatar/avatar-" + str(random.randint(1, 20)) + ".png"
|
|
|
|
|
|
|
|
|
|
|
|
class UserProfile(models.Model):
|
|
|
|
user = models.OneToOneField(User)
|
2016-10-29 18:17:35 +00:00
|
|
|
# Store user problem solution status with json string format
|
|
|
|
# {"problems": {1: ProblemSolutionStatus.ACCEPTED}, "contest_problems": {20: ProblemSolutionStatus.PENDING)}
|
|
|
|
problems_status = JSONField(default={})
|
2015-10-25 07:30:11 +00:00
|
|
|
avatar = models.CharField(max_length=50, default=_random_avatar)
|
|
|
|
blog = models.URLField(blank=True, null=True)
|
|
|
|
mood = models.CharField(max_length=200, blank=True, null=True)
|
2016-04-24 05:21:22 +00:00
|
|
|
accepted_problem_number = models.IntegerField(default=0)
|
|
|
|
submission_number = models.IntegerField(default=0)
|
2015-11-10 10:46:51 +00:00
|
|
|
phone_number = models.CharField(max_length=15, blank=True, null=True)
|
|
|
|
school = models.CharField(max_length=200, blank=True, null=True)
|
2016-09-25 09:00:52 +00:00
|
|
|
major = models.CharField(max_length=200, blank=True, null=True)
|
2015-12-08 06:31:43 +00:00
|
|
|
student_id = models.CharField(max_length=15, blank=True, null=True)
|
2016-09-25 09:00:52 +00:00
|
|
|
time_zone = models.CharField(max_length=32, blank=True, null=True)
|
|
|
|
language = models.CharField(max_length=32, blank=True, null=True)
|
2015-11-10 10:46:51 +00:00
|
|
|
|
2016-04-24 07:02:51 +00:00
|
|
|
def add_accepted_problem_number(self):
|
2016-06-23 04:19:16 +00:00
|
|
|
self.accepted_problem_number = models.F("accepted_problem_number") + 1
|
|
|
|
self.save()
|
2016-04-24 07:02:51 +00:00
|
|
|
|
|
|
|
def add_submission_number(self):
|
2016-06-23 04:19:16 +00:00
|
|
|
self.submission_number = models.F("submission_number") + 1
|
|
|
|
self.save()
|
2016-04-24 07:02:51 +00:00
|
|
|
|
2016-06-11 15:43:31 +00:00
|
|
|
def minus_accepted_problem_number(self):
|
2016-06-23 04:19:16 +00:00
|
|
|
self.accepted_problem_number = models.F("accepted_problem_number") - 1
|
|
|
|
self.save()
|
2016-06-11 15:43:31 +00:00
|
|
|
|
2015-10-25 07:30:11 +00:00
|
|
|
class Meta:
|
|
|
|
db_table = "user_profile"
|