models.py
2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# coding=utf-8
import logging
import traceback
from django.db import models
from django.contrib.auth.models import BaseUserManager, AbstractBaseUser
class MyUser(AbstractBaseUser):
"""
用户模型
"""
username = models.CharField(u'用户名', max_length=40, unique=True)
chinese_name = models.CharField(u'中文名', max_length=40)
email = models.EmailField(verbose_name='邮箱', max_length=255, unique=True)
phone = models.CharField(u"电话", max_length=13, blank=True, null=True)
is_leader = models.BooleanField(default=False, verbose_name=u"是否部门主管")
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email']
class Meta:
verbose_name = '用户'
verbose_name_plural='用户'
def get_chinese_name(self):
# The user is identified by their email address
return self.chinese_name
def get_short_name(self):
# The user is identified by their email address
return self.chinese_name
def get_username(self):
# The user is identified by their email address
return self.username
def get_full_name(self):
# The user is identified by their email address
return self.chinese_name
# On Python 3: def __str__(self):
def __unicode__(self):
if self.dept:
return u'{}-{}'.format(self.chinese_name, self.dept)
else:
return self.chinese_name
def has_perm(self, perm, obj=None):
"Does the user have a specific permission?"
# Simplest possible answer: Yes, always
return True
def has_module_perms(self, app_label):
"Does the user have permissions to view the app `app_label`?"
# Simplest possible answer: Yes, always
return True
@property
def is_staff(self):
"Is the user a member of staff?"
# Simplest possible answer: All admins are staff
return self.is_admin
def is_in_dept(self, dept_id):
"""
判断当前用户self是否是这个部门下的员工
:param dept_id:
:return:
"""
return self.dept.is_in_dept(dept_id=dept_id)