admin.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # Copyright (C) 2014 Andrey Antukh <niwi@niwi.be>
  2. # Copyright (C) 2014 Jesús Espino <jespinog@gmail.com>
  3. # Copyright (C) 2014 David Barragán <bameda@dbarragan.com>
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as
  6. # published by the Free Software Foundation, either version 3 of the
  7. # License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. from django.utils.translation import ugettext_lazy as _
  17. from django.contrib import admin
  18. from django.contrib.auth.models import Group, Permission
  19. from django.contrib.auth.admin import UserAdmin as DjangoUserAdmin
  20. from .models import Role, User
  21. from .forms import UserChangeForm, UserCreationForm
  22. admin.site.unregister(Group)
  23. class RoleAdmin(admin.ModelAdmin):
  24. list_display = ["name"]
  25. filter_horizontal = ('permissions',)
  26. def formfield_for_manytomany(self, db_field, request=None, **kwargs):
  27. if db_field.name == 'permissions':
  28. qs = kwargs.get('queryset', db_field.rel.to.objects)
  29. # Avoid a major performance hit resolving permission names which
  30. # triggers a content_type load:
  31. kwargs['queryset'] = qs.select_related('content_type')
  32. return super().formfield_for_manytomany(
  33. db_field, request=request, **kwargs)
  34. # admin.site.register(Role, RoleAdmin)
  35. class UserAdmin(DjangoUserAdmin):
  36. fieldsets = (
  37. (None, {'fields': ('username', 'password')}),
  38. (_('Personal info'), {'fields': ('full_name', 'email', 'bio', 'photo')}),
  39. (_('Extra info'), {'fields': ('color', 'lang', 'timezone', 'token', 'colorize_tags', 'email_token', 'new_email')}),
  40. (_('Permissions'), {'fields': ('is_active', 'is_superuser',)}),
  41. (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
  42. )
  43. form = UserChangeForm
  44. add_form = UserCreationForm
  45. list_display = ('username', 'email', 'full_name')
  46. list_filter = ('is_superuser', 'is_active')
  47. search_fields = ('username', 'full_name', 'email')
  48. ordering = ('username',)
  49. filter_horizontal = ()
  50. class RoleInline(admin.TabularInline):
  51. model = Role
  52. extra = 0
  53. admin.site.register(User, UserAdmin)