models.py 2.22 KB
# 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)