django-orchestra/orchestra/contrib/lists/admin.py

80 lines
2.9 KiB
Python
Raw Normal View History

2014-05-08 16:59:35 +00:00
from django.contrib import admin
from django.urls import re_path as url
2014-05-08 16:59:35 +00:00
from django.contrib.auth.admin import UserAdmin
from django.utils.translation import gettext_lazy as _
2014-05-08 16:59:35 +00:00
2014-10-15 12:47:28 +00:00
from orchestra.admin import ExtendedModelAdmin, ChangePasswordAdminMixin
2016-03-31 16:02:50 +00:00
from orchestra.admin.actions import disable, enable
2014-07-21 15:43:36 +00:00
from orchestra.admin.utils import admin_link
from orchestra.contrib.accounts.actions import list_accounts
2015-04-05 10:46:24 +00:00
from orchestra.contrib.accounts.admin import SelectAccountAdminMixin
2015-04-09 14:32:10 +00:00
from orchestra.contrib.accounts.filters import IsActiveListFilter
from orchestra.forms import UserCreationForm, NonStoredUserChangeForm
2014-05-08 16:59:35 +00:00
2016-02-23 11:49:10 +00:00
from . import settings
2016-03-14 10:40:11 +00:00
from .filters import HasCustomAddressListFilter
2014-05-08 16:59:35 +00:00
from .models import List
2014-10-15 12:47:28 +00:00
class ListAdmin(ChangePasswordAdminMixin, SelectAccountAdminMixin, ExtendedModelAdmin):
2015-04-09 14:32:10 +00:00
list_display = (
'name', 'address_name', 'address_domain_link', 'account_link', 'display_active'
)
2014-05-08 16:59:35 +00:00
add_fieldsets = (
(None, {
'classes': ('wide',),
2015-04-09 14:32:10 +00:00
'fields': ('account_link', 'name', 'is_active')
2014-05-08 16:59:35 +00:00
}),
(_("Address"), {
'classes': ('wide',),
'fields': (('address_name', 'address_domain'),)
}),
(_("Admin"), {
'classes': ('wide',),
'fields': ('admin_email', 'password1', 'password2'),
}),
)
fieldsets = (
(None, {
'classes': ('wide',),
2015-04-09 14:32:10 +00:00
'fields': ('account_link', 'name', 'is_active')
2014-05-08 16:59:35 +00:00
}),
(_("Address"), {
'classes': ('wide',),
2016-02-23 11:49:10 +00:00
'description': _("Additional address besides the default <name>@%s"
) % settings.LISTS_DEFAULT_DOMAIN,
2014-05-08 16:59:35 +00:00
'fields': (('address_name', 'address_domain'),)
}),
(_("Admin"), {
'classes': ('wide',),
2014-10-15 12:47:28 +00:00
'fields': ('password',),
2014-05-08 16:59:35 +00:00
}),
)
2014-11-18 13:59:21 +00:00
search_fields = ('name', 'address_name', 'address_domain__name', 'account__username')
2016-03-14 10:40:11 +00:00
list_filter = (IsActiveListFilter, HasCustomAddressListFilter)
2014-05-08 16:59:35 +00:00
readonly_fields = ('account_link',)
change_readonly_fields = ('name',)
form = NonStoredUserChangeForm
add_form = UserCreationForm
2014-11-20 15:34:59 +00:00
list_select_related = ('account', 'address_domain',)
2014-05-08 16:59:35 +00:00
filter_by_account_fields = ['address_domain']
2016-03-31 16:02:50 +00:00
actions = (disable, enable, list_accounts)
2014-05-08 16:59:35 +00:00
2014-07-21 15:43:36 +00:00
address_domain_link = admin_link('address_domain', order='address_domain__name')
2014-05-08 16:59:35 +00:00
def get_urls(self):
useradmin = UserAdmin(List, self.admin_site)
2015-05-19 13:27:04 +00:00
return [
url(r'^(\d+)/password/$',
self.admin_site.admin_view(useradmin.user_change_password))
] + super(ListAdmin, self).get_urls()
2015-07-27 12:55:35 +00:00
def save_model(self, request, obj, form, change):
""" set password """
if not change:
obj.set_password(form.cleaned_data["password1"])
super(ListAdmin, self).save_model(request, obj, form, change)
2014-05-08 16:59:35 +00:00
admin.site.register(List, ListAdmin)