admin.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. from django.contrib import admin
  2. from django.contrib.auth.models import Group
  3. from apps.account.models import LoonUser, LoonDept, LoonRole, LoonUserRole, AppToken
  4. # Register your models here.
  5. from apps.loon_model_base_admin import LoonModelBaseAdmin
  6. class LoonUserAdmin(LoonModelBaseAdmin):
  7. list_display = ('id', 'username', 'alias', 'email', 'phone', 'dept_id', 'is_active', 'is_admin') + LoonModelBaseAdmin.list_display
  8. readonly_fields = ['creator', 'last_login']
  9. search_fields = ('username',)
  10. def save_model(self, request, obj, form, change):
  11. if not obj.creator:
  12. obj.creator = request.user.username
  13. # 可用于生成密码,晚点修改下
  14. obj.set_password(form.cleaned_data['password'])
  15. obj.save()
  16. class LoonDeptAdmin(LoonModelBaseAdmin):
  17. search_fields = ('name',)
  18. list_display = ('id', 'name', 'parent_dept_id', 'leader', 'approver') + LoonModelBaseAdmin.list_display
  19. class LoonRoleAdmin(LoonModelBaseAdmin):
  20. search_fields = ('name',)
  21. list_display = ('id', 'name', 'description', 'label') + LoonModelBaseAdmin.list_display
  22. class LoonUserRoleAdmin(LoonModelBaseAdmin):
  23. search_fields = ('user_id',)
  24. list_display = ('id', 'user_id', 'role_id') + LoonModelBaseAdmin.list_display
  25. class AppTokenAdmin(LoonModelBaseAdmin):
  26. search_fields = ('app_name',)
  27. readonly_fields = ['token', 'creator']
  28. list_display = ('id', 'app_name', 'token') + LoonModelBaseAdmin.list_display
  29. def save_model(self, request, obj, form, change):
  30. if not obj.creator:
  31. obj.creator = request.user.username
  32. import uuid
  33. obj.token = uuid.uuid1()
  34. obj.save()
  35. admin.site.register(LoonUser, LoonUserAdmin)
  36. admin.site.register(LoonDept, LoonDeptAdmin)
  37. admin.site.register(LoonRole, LoonRoleAdmin)
  38. admin.site.register(LoonUserRole, LoonUserRoleAdmin)
  39. admin.site.register(AppToken, AppTokenAdmin)
  40. admin.site.unregister(Group)