models.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. from django.contrib.auth.base_user import BaseUserManager, AbstractBaseUser
  2. from django.db import models
  3. from apps.loon_base_model import BaseModel
  4. from apps.workflow.models import Workflow
  5. class LoonDept(BaseModel):
  6. """
  7. 部门
  8. """
  9. name = models.CharField('名称', max_length=50, help_text='部门名称')
  10. parent_dept_id = models.IntegerField('上级部门id', blank=True, default=0)
  11. leader = models.CharField('部门leader', max_length=50, blank=True, default='', help_text='部门的leader, loonuser表中的用户名')
  12. approver = models.CharField('审批人', max_length=100, blank=True, default='', help_text='loonuser表中的用户名, 逗号隔开多个user。当工作流设置为leader审批时, 优先以审批人为准,如果审批人为空,则取leader')
  13. label = models.CharField('标签', max_length=50, blank=True, default='', help_text='因为部门信息一般是从别处同步过来, 为保证对应关系,同步时可以在此字段设置其他系统中相应的唯一标识')
  14. creator = models.CharField('创建人', max_length=50, help_text='loonuser表中的用户名')
  15. gmt_created = models.DateTimeField('创建时间', auto_now_add=True)
  16. gmt_modified = models.DateTimeField('更新时间', auto_now=True)
  17. is_deleted = models.BooleanField('已删除', default=False)
  18. def get_dict(self):
  19. dept_dict_info = super().get_dict()
  20. creator_obj = LoonUser.objects.filter(username=getattr(self, 'creator')).first()
  21. if creator_obj:
  22. dept_dict_info['creator_info'] = dict(creator_id=creator_obj.id, creator_alias=creator_obj.alias)
  23. else:
  24. dept_dict_info['creator_info'] = dict(creator_id=0, creator_alias='', creator_username=getattr(self, 'creator'))
  25. if self.parent_dept_id:
  26. parent_dept_obj = LoonDept.objects.filter(id=self.parent_dept_id, is_deleted=0).first()
  27. if parent_dept_obj:
  28. parent_dept_info = dict(parent_dept_id=self.parent_dept_id, parent_dept_name=parent_dept_obj.name)
  29. else:
  30. parent_dept_info = dict(parent_dept_id=self.parent_dept_id, parent_dept_name='未知')
  31. else:
  32. parent_dept_info = dict(parent_dept_id=self.parent_dept_id, parent_dept_name='')
  33. dept_dict_info['parent_dept_info'] = parent_dept_info
  34. if self.leader:
  35. leader_obj = LoonUser.objects.filter(username=self.leader).first()
  36. if leader_obj:
  37. dept_dict_info['leader_info'] = {
  38. 'leader_username': leader_obj.username,
  39. 'leader_alias': leader_obj.alias,
  40. 'leader_id': leader_obj.id,
  41. }
  42. else:
  43. dept_dict_info['leader_info'] = {
  44. 'leader_username': self.leader,
  45. 'leader_alias': self.leader,
  46. 'leader_id': 0,
  47. }
  48. else:
  49. dept_dict_info['leader_info'] = {
  50. 'leader_username': '',
  51. 'leader_alias': '',
  52. 'leader_id': 0,
  53. }
  54. if self.approver:
  55. approver_list = self.approver.split(',')
  56. approver_info_list = []
  57. for approver in approver_list:
  58. approver_obj = LoonUser.objects.filter(username=approver).first()
  59. if approver_obj:
  60. approver_info_list.append({
  61. 'approver_name': approver_obj.username,
  62. 'approver_alias': approver_obj.alias,
  63. 'approver_id': approver_obj.id,
  64. })
  65. else:
  66. approver_info_list.append({
  67. 'approver_name': approver,
  68. 'approver_alias': approver,
  69. 'approver_id': 0,
  70. })
  71. dept_dict_info['approver_info'] = approver_info_list
  72. else:
  73. dept_dict_info['approver_info'] = []
  74. return dept_dict_info
  75. class LoonRole(BaseModel):
  76. """
  77. 角色
  78. """
  79. name = models.CharField('名称', max_length=50)
  80. description = models.CharField('描述', max_length=50, default='')
  81. label = models.CharField('标签', max_length=50, blank=True, default='{}', help_text='因为角色信息也可能是从别处同步过来, 为保证对应关系,同步时可以在此字段设置其他系统中相应的唯一标识,字典的json格式')
  82. creator = models.CharField('创建人', max_length=50)
  83. gmt_created = models.DateTimeField('创建时间', auto_now_add=True)
  84. gmt_modified = models.DateTimeField('更新时间', auto_now=True)
  85. is_deleted = models.BooleanField('已删除', default=False)
  86. def get_dict(self):
  87. role_dict_info = super().get_dict()
  88. creator_obj = LoonUser.objects.filter(username=getattr(self, 'creator')).first()
  89. if creator_obj:
  90. role_dict_info['creator_info'] = dict(creator_id=creator_obj.id, creator_alias=creator_obj.alias,
  91. creator_username=creator_obj.username)
  92. else:
  93. role_dict_info['creator_info'] = dict(creator_id=0, creator_alias='', creator_username=getattr(self, 'creator'))
  94. return role_dict_info
  95. class LoonUserManager(BaseUserManager):
  96. def create_user(self, email, username, password=None, dep=0):
  97. if not email:
  98. raise ValueError('Users must have an email address')
  99. user = self.model(username=username, email=self.normalize_email(email))
  100. user.set_password(password)
  101. user.save(using=self._db)
  102. return user
  103. def create_superuser(self, email, username, password):
  104. user = self.create_user(email=self.normalize_email(email), username=username, password=password)
  105. user.type_id = 2
  106. user.save(using=self._db)
  107. return user
  108. class LoonUser(AbstractBaseUser):
  109. """
  110. 用户
  111. """
  112. username = models.CharField('用户名', max_length=50, unique=True)
  113. alias = models.CharField('姓名', max_length=50, default='')
  114. email = models.EmailField('邮箱', max_length=255)
  115. phone = models.CharField('电话', max_length=13, default='')
  116. is_active = models.BooleanField('已激活', default=True)
  117. type_id = models.IntegerField('用户类型', default=0) # 见service.common.constant_service中定义
  118. creator = models.CharField('创建人', max_length=50)
  119. gmt_created = models.DateTimeField('创建时间', auto_now_add=True)
  120. gmt_modified = models.DateTimeField('更新时间', auto_now=True)
  121. is_deleted = models.BooleanField('已删除', default=False)
  122. objects = LoonUserManager()
  123. USERNAME_FIELD = 'username'
  124. REQUIRED_FIELDS = ['email']
  125. @property
  126. def is_staff(self):
  127. return self.is_active
  128. def get_short_name(self):
  129. return self.username
  130. def get_alias_name(self):
  131. return self.alias
  132. def has_perm(self, perm, obj=None):
  133. "Does the user have a specific permission?"
  134. # Simplest possible answer: Yes, always
  135. return True
  136. def has_perms(self, perm, obj=None):
  137. return True
  138. def has_module_perms(self, app_label):
  139. return True
  140. @property
  141. def dept_name(self):
  142. user_dept_queryset = LoonUserDept.objects.filter(user_id=self.id, is_deleted=0).all()
  143. user_dept_name_list = []
  144. for user_dept in user_dept_queryset:
  145. user_dept_name_list.append(user_dept.dept.name)
  146. return ','.join(user_dept_name_list)
  147. def get_dict(self):
  148. fields = []
  149. for field in self._meta.fields:
  150. fields.append(field.name)
  151. dict_result = {}
  152. import datetime
  153. for attr in fields:
  154. if isinstance(getattr(self, attr), datetime.datetime):
  155. dict_result[attr] = getattr(self, attr).strftime('%Y-%m-%d %H:%M:%S')
  156. elif isinstance(getattr(self, attr), datetime.date):
  157. dict_result[attr] = getattr(self, attr).strftime('%Y-%m-%d')
  158. elif attr == 'dept_id':
  159. dept_obj = LoonDept.objects.filter(id=getattr(self, attr), is_deleted=0).first()
  160. dept_name = dept_obj.name if dept_obj else ''
  161. dict_result['dept_info'] = dict(dept_id=getattr(self, attr), dept_name=dept_name)
  162. elif attr == 'password':
  163. pass
  164. elif attr == 'creator':
  165. creator_obj = LoonUser.objects.filter(username=getattr(self, attr)).first()
  166. if creator_obj:
  167. dict_result['creator_info'] = dict(creator_id= creator_obj.id, creator_alias=creator_obj.alias, creator_username=creator_obj.username)
  168. else:
  169. dict_result['creator_info'] = dict(creator_id=0, creator_alias='', creator_username=getattr(self, attr))
  170. else:
  171. dict_result[attr] = getattr(self, attr)
  172. return dict_result
  173. def get_json(self):
  174. import json
  175. dict_result = self.get_dict()
  176. return json.dumps(dict_result)
  177. class LoonUserDept(BaseModel):
  178. """
  179. 用户部门
  180. """
  181. user = models.ForeignKey(LoonUser, to_field='id', db_constraint=False, on_delete=models.DO_NOTHING)
  182. dept = models.ForeignKey(LoonDept, to_field='id', db_constraint=False, on_delete=models.DO_NOTHING)
  183. class LoonUserRole(BaseModel):
  184. """
  185. 用户角色
  186. """
  187. user_id = models.IntegerField('用户id')
  188. role_id = models.IntegerField('角色id')
  189. class AppToken(BaseModel):
  190. """
  191. App token,用于api调用方授权
  192. """
  193. app_name = models.CharField('应用名称', max_length=50)
  194. token = models.CharField('签名令牌', max_length=50, help_text='后端自动生成')
  195. ticket_sn_prefix = models.CharField('工单流水号前缀', default='loonflow', max_length=20, help_text='工单流水号前缀,如设置为loonflow,则创建的工单的流水号为loonflow_201805130013')
  196. def get_dict(self):
  197. role_dict_info = super().get_dict()
  198. creator_obj = LoonUser.objects.filter(username=getattr(self, 'creator')).first()
  199. if creator_obj:
  200. role_dict_info['creator_info'] = dict(creator_id=creator_obj.id, creator_alias=creator_obj.alias,
  201. creator_username=creator_obj.username)
  202. else:
  203. role_dict_info['creator_info'] = dict(creator_id=0, creator_alias='', creator_username=getattr(self, 'creator'))
  204. return role_dict_info