2014-10-20 15:51:24 +00:00
|
|
|
import copy
|
|
|
|
import re
|
2015-04-05 10:46:24 +00:00
|
|
|
from urllib.parse import parse_qsl
|
2014-10-20 15:51:24 +00:00
|
|
|
|
2015-10-05 14:50:37 +00:00
|
|
|
from django import forms
|
|
|
|
from django.apps import apps
|
2023-11-17 12:25:13 +00:00
|
|
|
from django.urls import re_path as url
|
2014-05-08 16:59:35 +00:00
|
|
|
from django.contrib import admin, messages
|
2015-05-01 17:23:22 +00:00
|
|
|
from django.contrib.admin.utils import unquote
|
2014-09-29 13:34:38 +00:00
|
|
|
from django.contrib.auth import admin as auth
|
2021-04-21 12:27:18 +00:00
|
|
|
from django.urls import reverse
|
2014-05-08 16:59:35 +00:00
|
|
|
from django.http import HttpResponseRedirect
|
2015-05-21 13:34:12 +00:00
|
|
|
from django.templatetags.static import static
|
2014-05-08 16:59:35 +00:00
|
|
|
from django.utils.safestring import mark_safe
|
2023-11-17 12:25:13 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2014-05-08 16:59:35 +00:00
|
|
|
|
2014-10-06 14:57:02 +00:00
|
|
|
from orchestra.admin import ExtendedModelAdmin, ChangePasswordAdminMixin
|
2015-09-18 11:29:52 +00:00
|
|
|
from orchestra.admin.actions import SendEmail
|
2016-02-19 10:11:28 +00:00
|
|
|
from orchestra.admin.utils import wrap_admin_view, admin_link, set_url_query
|
2016-05-13 10:07:04 +00:00
|
|
|
from orchestra.contrib.services.settings import SERVICES_IGNORE_ACCOUNT_TYPE
|
2014-07-28 17:28:00 +00:00
|
|
|
from orchestra.core import services, accounts
|
2014-10-06 14:57:02 +00:00
|
|
|
from orchestra.forms import UserChangeForm
|
2016-05-13 10:07:04 +00:00
|
|
|
from orchestra.utils.apps import isinstalled
|
2014-05-08 16:59:35 +00:00
|
|
|
|
2016-03-31 16:02:50 +00:00
|
|
|
from .actions import (list_contacts, service_report, delete_related_services, disable_selected,
|
|
|
|
enable_selected)
|
2014-10-06 14:57:02 +00:00
|
|
|
from .forms import AccountCreationForm
|
2014-05-08 16:59:35 +00:00
|
|
|
from .models import Account
|
|
|
|
|
|
|
|
|
2014-10-06 14:57:02 +00:00
|
|
|
class AccountAdmin(ChangePasswordAdminMixin, auth.UserAdmin, ExtendedModelAdmin):
|
2014-11-09 10:16:07 +00:00
|
|
|
list_display = ('username', 'full_name', 'type', 'is_active')
|
2014-05-08 16:59:35 +00:00
|
|
|
list_filter = (
|
2016-10-03 08:59:47 +00:00
|
|
|
'type', 'is_active',
|
2014-05-08 16:59:35 +00:00
|
|
|
)
|
|
|
|
add_fieldsets = (
|
|
|
|
(_("User"), {
|
|
|
|
'fields': ('username', 'password1', 'password2',),
|
|
|
|
}),
|
2014-09-30 09:49:07 +00:00
|
|
|
(_("Personal info"), {
|
2014-10-27 14:31:04 +00:00
|
|
|
'fields': ('short_name', 'full_name', 'email', ('type', 'language'), 'comments'),
|
2014-09-30 09:49:07 +00:00
|
|
|
}),
|
|
|
|
(_("Permissions"), {
|
2014-09-30 14:46:29 +00:00
|
|
|
'fields': ('is_superuser',)
|
2014-05-08 16:59:35 +00:00
|
|
|
}),
|
|
|
|
)
|
|
|
|
fieldsets = (
|
|
|
|
(_("User"), {
|
2014-10-24 10:16:46 +00:00
|
|
|
'fields': ('username', 'password', 'main_systemuser_link')
|
2014-09-30 09:49:07 +00:00
|
|
|
}),
|
|
|
|
(_("Personal info"), {
|
2014-10-27 14:31:04 +00:00
|
|
|
'fields': ('short_name', 'full_name', 'email', ('type', 'language'), 'comments'),
|
2014-05-08 16:59:35 +00:00
|
|
|
}),
|
2014-09-30 09:49:07 +00:00
|
|
|
(_("Permissions"), {
|
|
|
|
'fields': ('is_superuser', 'is_active')
|
|
|
|
}),
|
|
|
|
(_("Important dates"), {
|
2014-09-30 14:46:29 +00:00
|
|
|
'classes': ('collapse',),
|
2014-09-30 09:49:07 +00:00
|
|
|
'fields': ('last_login', 'date_joined')
|
2014-05-08 16:59:35 +00:00
|
|
|
}),
|
|
|
|
)
|
2014-11-09 10:16:07 +00:00
|
|
|
search_fields = ('username', 'short_name', 'full_name')
|
2014-05-08 16:59:35 +00:00
|
|
|
add_form = AccountCreationForm
|
2014-10-06 14:57:02 +00:00
|
|
|
form = UserChangeForm
|
2014-09-29 13:34:38 +00:00
|
|
|
filter_horizontal = ()
|
2015-05-12 12:38:40 +00:00
|
|
|
change_readonly_fields = ('username', 'main_systemuser_link', 'is_active')
|
2014-08-22 11:28:46 +00:00
|
|
|
change_form_template = 'admin/accounts/account/change_form.html'
|
2015-09-18 11:29:52 +00:00
|
|
|
actions = (
|
2016-03-31 16:02:50 +00:00
|
|
|
disable_selected, enable_selected, delete_related_services, list_contacts, service_report,
|
|
|
|
SendEmail()
|
2015-09-18 11:29:52 +00:00
|
|
|
)
|
2016-03-31 16:02:50 +00:00
|
|
|
change_view_actions = (disable_selected, service_report, enable_selected)
|
2014-11-09 10:16:07 +00:00
|
|
|
ordering = ()
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2014-10-24 10:16:46 +00:00
|
|
|
main_systemuser_link = admin_link('main_systemuser')
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
def formfield_for_dbfield(self, db_field, **kwargs):
|
|
|
|
""" Make value input widget bigger """
|
|
|
|
if db_field.name == 'comments':
|
|
|
|
kwargs['widget'] = forms.Textarea(attrs={'cols': 70, 'rows': 4})
|
|
|
|
return super(AccountAdmin, self).formfield_for_dbfield(db_field, **kwargs)
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2015-10-07 11:44:30 +00:00
|
|
|
def render_change_form(self, request, context, add=False, change=False, form_url='', obj=None):
|
|
|
|
if not add:
|
|
|
|
if request.method == 'GET' and not obj.is_active:
|
2014-05-08 16:59:35 +00:00
|
|
|
messages.warning(request, 'This account is disabled.')
|
2015-10-07 11:44:30 +00:00
|
|
|
context.update({
|
|
|
|
'services': sorted(
|
|
|
|
[model._meta for model in services.get() if model is not Account],
|
|
|
|
key=lambda i: i.verbose_name_plural.lower()
|
|
|
|
),
|
|
|
|
'accounts': sorted(
|
|
|
|
[model._meta for model in accounts.get() if model is not Account],
|
|
|
|
key=lambda i: i.verbose_name_plural.lower()
|
|
|
|
)
|
|
|
|
})
|
|
|
|
return super(AccountAdmin, self).render_change_form(
|
|
|
|
request, context, add, change, form_url, obj)
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2014-10-20 15:51:24 +00:00
|
|
|
def get_fieldsets(self, request, obj=None):
|
2015-03-26 16:00:30 +00:00
|
|
|
fieldsets = super(AccountAdmin, self).get_fieldsets(request, obj)
|
2014-10-20 15:51:24 +00:00
|
|
|
if not obj:
|
|
|
|
fields = AccountCreationForm.create_related_fields
|
|
|
|
if fields:
|
|
|
|
fieldsets = copy.deepcopy(fieldsets)
|
|
|
|
fieldsets = list(fieldsets)
|
|
|
|
fieldsets.insert(1, (_("Related services"), {'fields': fields}))
|
|
|
|
return fieldsets
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2014-10-20 15:51:24 +00:00
|
|
|
def save_model(self, request, obj, form, change):
|
|
|
|
if not change:
|
2014-10-24 10:16:46 +00:00
|
|
|
form.save_model(obj)
|
2014-10-20 15:51:24 +00:00
|
|
|
form.save_related(obj)
|
2014-10-24 10:16:46 +00:00
|
|
|
else:
|
2016-05-13 10:07:04 +00:00
|
|
|
if isinstalled('orchestra.contrib.orders') and isinstalled('orchestra.contrib.services'):
|
|
|
|
if 'type' in form.changed_data:
|
|
|
|
old_type = Account.objects.get(pk=obj.pk).type
|
|
|
|
new_type = form.cleaned_data['type']
|
|
|
|
context = {
|
|
|
|
'from': old_type.lower(),
|
|
|
|
'to': new_type.lower(),
|
|
|
|
'url': reverse('admin:orders_order_changelist'),
|
|
|
|
}
|
|
|
|
msg = ''
|
|
|
|
if old_type in SERVICES_IGNORE_ACCOUNT_TYPE and new_type not in SERVICES_IGNORE_ACCOUNT_TYPE:
|
|
|
|
context['url'] += '?account=%i&ignore=1' % obj.pk
|
|
|
|
msg = _("Account type has been changed from <i>%(from)s</i> to <i>%(to)s</i>. "
|
|
|
|
"You may want to mark <a href='%(url)s'>existing ignored orders</a> as not ignored.")
|
|
|
|
elif old_type not in SERVICES_IGNORE_ACCOUNT_TYPE and new_type in SERVICES_IGNORE_ACCOUNT_TYPE:
|
|
|
|
context['url'] += '?account=%i&ignore=0' % obj.pk
|
|
|
|
msg = _("Account type has been changed from <i>%(from)s</i> to <i>%(to)s</i>. "
|
|
|
|
"You may want to ignore <a href='%(url)s'>existing not ignored orders</a>.")
|
|
|
|
if msg:
|
|
|
|
messages.warning(request, mark_safe(msg % context))
|
2014-10-24 10:16:46 +00:00
|
|
|
super(AccountAdmin, self).save_model(request, obj, form, change)
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2016-03-31 16:02:50 +00:00
|
|
|
def get_change_view_actions(self, obj=None):
|
|
|
|
views = super().get_change_view_actions(obj=obj)
|
|
|
|
if obj is not None:
|
|
|
|
if obj.is_active:
|
|
|
|
return [view for view in views if view.url_name != 'enable']
|
|
|
|
return [view for view in views if view.url_name != 'disable']
|
|
|
|
return views
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2015-09-18 11:29:52 +00:00
|
|
|
def get_actions(self, request):
|
2016-05-09 12:02:10 +00:00
|
|
|
actions = super().get_actions(request)
|
2015-09-18 11:29:52 +00:00
|
|
|
if 'delete_selected' in actions:
|
|
|
|
del actions['delete_selected']
|
|
|
|
return actions
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
admin.site.register(Account, AccountAdmin)
|
|
|
|
|
|
|
|
|
|
|
|
class AccountListAdmin(AccountAdmin):
|
|
|
|
""" Account list to allow account selection when creating new services """
|
2014-11-09 10:16:07 +00:00
|
|
|
list_display = ('select_account', 'username', 'type', 'username')
|
2014-05-08 16:59:35 +00:00
|
|
|
actions = None
|
2015-10-05 14:49:15 +00:00
|
|
|
change_list_template = 'admin/accounts/account/select_account_list.html'
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2021-05-24 10:36:49 +00:00
|
|
|
@mark_safe
|
2014-05-08 16:59:35 +00:00
|
|
|
def select_account(self, instance):
|
2014-09-05 14:27:30 +00:00
|
|
|
# TODO get query string from request.META['QUERY_STRING'] to preserve filters
|
2014-05-08 16:59:35 +00:00
|
|
|
context = {
|
|
|
|
'url': '../?account=' + str(instance.pk),
|
2015-04-24 11:39:20 +00:00
|
|
|
'name': instance.username,
|
|
|
|
'plus': '<strong style="color:green; font-size:12px">+</strong>',
|
2014-05-08 16:59:35 +00:00
|
|
|
}
|
2015-04-24 11:39:20 +00:00
|
|
|
return _('<a href="%(url)s">%(plus)s Add to %(name)s</a>') % context
|
2014-05-08 16:59:35 +00:00
|
|
|
select_account.short_description = _("account")
|
2015-04-24 11:39:20 +00:00
|
|
|
select_account.admin_order_field = 'username'
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
def changelist_view(self, request, extra_context=None):
|
2015-10-05 14:49:15 +00:00
|
|
|
app_label = request.META['PATH_INFO'].split('/')[-5]
|
|
|
|
model = request.META['PATH_INFO'].split('/')[-4]
|
|
|
|
model = apps.get_model(app_label, model)
|
|
|
|
opts = model._meta
|
2014-05-08 16:59:35 +00:00
|
|
|
context = {
|
2015-10-05 14:49:15 +00:00
|
|
|
'title': _("Select account for adding a new %s") % (opts.verbose_name),
|
|
|
|
'original_opts': opts,
|
2014-05-08 16:59:35 +00:00
|
|
|
}
|
|
|
|
context.update(extra_context or {})
|
2015-05-12 12:38:40 +00:00
|
|
|
response = super(AccountListAdmin, self).changelist_view(request, extra_context=context)
|
|
|
|
if hasattr(response, 'context_data'):
|
|
|
|
# user has submitted a change list change, we redirect directly to the add view
|
|
|
|
# if there is only one result
|
2015-10-08 09:00:22 +00:00
|
|
|
query = request.GET.get('q', '')
|
|
|
|
if query:
|
|
|
|
try:
|
|
|
|
account = Account.objects.get(username=query)
|
|
|
|
except Account.DoesNotExist:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
return HttpResponseRedirect('../?account=%i' % account.pk)
|
2015-05-12 12:38:40 +00:00
|
|
|
queryset = response.context_data['cl'].queryset
|
|
|
|
if len(queryset) == 1:
|
|
|
|
return HttpResponseRedirect('../?account=%i' % queryset[0].pk)
|
|
|
|
return response
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AccountAdminMixin(object):
|
|
|
|
""" Provide basic account support to ModelAdmin and AdminInline classes """
|
|
|
|
readonly_fields = ('account_link',)
|
|
|
|
filter_by_account_fields = []
|
2014-07-29 14:29:59 +00:00
|
|
|
change_list_template = 'admin/accounts/account/change_list.html'
|
|
|
|
change_form_template = 'admin/accounts/account/change_form.html'
|
2014-10-03 14:02:11 +00:00
|
|
|
account = None
|
2014-10-23 15:38:46 +00:00
|
|
|
list_select_related = ('account',)
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2021-05-12 12:16:28 +00:00
|
|
|
@mark_safe
|
2015-03-26 16:00:30 +00:00
|
|
|
def display_active(self, instance):
|
|
|
|
if not instance.is_active:
|
2016-05-03 12:14:59 +00:00
|
|
|
return '<img src="%s" alt="False">' % static('admin/img/icon-no.svg')
|
2015-03-26 16:00:30 +00:00
|
|
|
elif not instance.account.is_active:
|
|
|
|
msg = _("Account disabled")
|
2016-05-03 12:14:59 +00:00
|
|
|
return '<img style="width:13px" src="%s" alt="False" title="%s">' % (static('admin/img/inline-delete.svg'), msg)
|
|
|
|
return '<img src="%s" alt="False">' % static('admin/img/icon-yes.svg')
|
2015-03-26 16:00:30 +00:00
|
|
|
display_active.short_description = _("active")
|
|
|
|
display_active.admin_order_field = 'is_active'
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
def account_link(self, instance):
|
|
|
|
account = instance.account if instance.pk else self.account
|
2016-02-19 10:11:28 +00:00
|
|
|
return admin_link()(account)
|
2014-05-08 16:59:35 +00:00
|
|
|
account_link.short_description = _("account")
|
2014-09-29 13:34:38 +00:00
|
|
|
account_link.admin_order_field = 'account__username'
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2015-03-27 19:50:54 +00:00
|
|
|
def get_form(self, request, obj=None, **kwargs):
|
2015-03-26 16:00:30 +00:00
|
|
|
""" Warns user when object's account is disabled """
|
2015-03-27 19:50:54 +00:00
|
|
|
form = super(AccountAdminMixin, self).get_form(request, obj, **kwargs)
|
2015-03-26 16:00:30 +00:00
|
|
|
try:
|
2015-03-27 19:50:54 +00:00
|
|
|
field = form.base_fields['is_active']
|
2015-03-26 16:00:30 +00:00
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
else:
|
2016-03-18 10:13:25 +00:00
|
|
|
opts = self.model._meta
|
|
|
|
help_text = _(
|
|
|
|
"Designates whether this %(name)s should be treated as active. "
|
|
|
|
"Unselect this instead of deleting %(plural_name)s."
|
|
|
|
) % {
|
|
|
|
'name': opts.verbose_name,
|
|
|
|
'plural_name': opts.verbose_name_plural,
|
|
|
|
}
|
2015-03-26 16:00:30 +00:00
|
|
|
if obj and not obj.account.is_active:
|
|
|
|
help_text += "<br><b style='color:red;'>This user's account is dissabled</b>"
|
|
|
|
field.help_text = _(help_text)
|
2015-06-04 14:15:17 +00:00
|
|
|
# Not available in POST
|
|
|
|
form.initial_account = self.get_changeform_initial_data(request).get('account')
|
2015-03-27 19:50:54 +00:00
|
|
|
return form
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2015-03-04 21:06:16 +00:00
|
|
|
def get_fields(self, request, obj=None):
|
|
|
|
""" remove account or account_link depending on the case """
|
|
|
|
fields = super(AccountAdminMixin, self).get_fields(request, obj)
|
|
|
|
fields = list(fields)
|
|
|
|
if obj is not None or getattr(self, 'account_id', None):
|
|
|
|
try:
|
|
|
|
fields.remove('account')
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
fields.remove('account_link')
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
return fields
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2014-09-04 15:55:43 +00:00
|
|
|
def get_readonly_fields(self, request, obj=None):
|
|
|
|
""" provide account for filter_by_account_fields """
|
|
|
|
if obj:
|
|
|
|
self.account = obj.account
|
2015-03-26 16:00:30 +00:00
|
|
|
return super(AccountAdminMixin, self).get_readonly_fields(request, obj)
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
def formfield_for_dbfield(self, db_field, **kwargs):
|
2014-09-29 13:34:38 +00:00
|
|
|
""" Filter by account """
|
2014-05-08 16:59:35 +00:00
|
|
|
formfield = super(AccountAdminMixin, self).formfield_for_dbfield(db_field, **kwargs)
|
|
|
|
if db_field.name in self.filter_by_account_fields:
|
2014-10-03 14:02:11 +00:00
|
|
|
if self.account:
|
2014-05-08 16:59:35 +00:00
|
|
|
# Hack widget render in order to append ?account=id to the add url
|
|
|
|
old_render = formfield.widget.render
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
def render(*args, **kwargs):
|
|
|
|
output = old_render(*args, **kwargs)
|
|
|
|
output = output.replace('/add/"', '/add/?account=%s"' % self.account.pk)
|
2015-10-05 13:31:08 +00:00
|
|
|
with_qargs = r'/add/?\1&account=%s"' % self.account.pk
|
|
|
|
output = re.sub(r'/add/\?([^".]*)"', with_qargs, output)
|
2014-05-08 16:59:35 +00:00
|
|
|
return mark_safe(output)
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
formfield.widget.render = render
|
|
|
|
# Filter related object by account
|
|
|
|
formfield.queryset = formfield.queryset.filter(account=self.account)
|
2015-06-17 10:34:14 +00:00
|
|
|
# Apply heuristic order by
|
|
|
|
if not formfield.queryset.query.order_by:
|
2016-10-11 10:21:48 +00:00
|
|
|
related_fields = [f.name for f in db_field.related_model._meta.get_fields()]
|
2015-06-17 10:34:14 +00:00
|
|
|
if 'name' in related_fields:
|
|
|
|
formfield.queryset = formfield.queryset.order_by('name')
|
|
|
|
elif 'username' in related_fields:
|
|
|
|
formfield.queryset = formfield.queryset.order_by('username')
|
2014-10-03 14:02:11 +00:00
|
|
|
elif db_field.name == 'account':
|
|
|
|
if self.account:
|
|
|
|
formfield.initial = self.account.pk
|
|
|
|
elif Account.objects.count() == 1:
|
|
|
|
formfield.initial = 1
|
2015-06-17 10:34:14 +00:00
|
|
|
formfield.queryset = formfield.queryset.order_by('username')
|
2014-05-08 16:59:35 +00:00
|
|
|
return formfield
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2015-03-23 15:36:51 +00:00
|
|
|
def get_formset(self, request, obj=None, **kwargs):
|
|
|
|
""" provides form.account for convinience """
|
2015-03-26 16:00:30 +00:00
|
|
|
formset = super(AccountAdminMixin, self).get_formset(request, obj, **kwargs)
|
2015-03-23 15:36:51 +00:00
|
|
|
formset.form.account = self.account
|
|
|
|
formset.account = self.account
|
|
|
|
return formset
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2014-07-29 14:29:59 +00:00
|
|
|
def get_account_from_preserve_filters(self, request):
|
|
|
|
preserved_filters = self.get_preserved_filters(request)
|
|
|
|
preserved_filters = dict(parse_qsl(preserved_filters))
|
|
|
|
cl_filters = preserved_filters.get('_changelist_filters')
|
|
|
|
if cl_filters:
|
|
|
|
return dict(parse_qsl(cl_filters)).get('account')
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2014-07-29 14:29:59 +00:00
|
|
|
def changeform_view(self, request, object_id=None, form_url='', extra_context=None):
|
|
|
|
account_id = self.get_account_from_preserve_filters(request)
|
|
|
|
if not object_id:
|
|
|
|
if account_id:
|
|
|
|
# Preselect account
|
|
|
|
set_url_query(request, 'account', account_id)
|
|
|
|
context = {
|
|
|
|
'from_account': bool(account_id),
|
|
|
|
'account': not account_id or Account.objects.get(pk=account_id),
|
|
|
|
'account_opts': Account._meta,
|
|
|
|
}
|
|
|
|
context.update(extra_context or {})
|
2015-10-05 13:31:08 +00:00
|
|
|
return super(AccountAdminMixin, self).changeform_view(
|
|
|
|
request, object_id, form_url=form_url, extra_context=context)
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2014-07-29 14:29:59 +00:00
|
|
|
def changelist_view(self, request, extra_context=None):
|
|
|
|
account_id = request.GET.get('account')
|
2014-09-08 10:03:42 +00:00
|
|
|
context = {}
|
2014-07-29 14:29:59 +00:00
|
|
|
if account_id:
|
|
|
|
opts = self.model._meta
|
|
|
|
account = Account.objects.get(pk=account_id)
|
|
|
|
context = {
|
|
|
|
'account': not account_id or Account.objects.get(pk=account_id),
|
|
|
|
'account_opts': Account._meta,
|
2014-09-08 10:03:42 +00:00
|
|
|
'all_selected': True,
|
2014-07-29 14:29:59 +00:00
|
|
|
}
|
2014-09-08 10:03:42 +00:00
|
|
|
if not request.GET.get('all'):
|
|
|
|
context.update({
|
|
|
|
'all_selected': False,
|
|
|
|
'title': _("Select %s to change for %s") % (
|
2014-09-30 09:49:07 +00:00
|
|
|
opts.verbose_name, account.username),
|
2014-09-08 10:03:42 +00:00
|
|
|
})
|
|
|
|
else:
|
|
|
|
request_copy = request.GET.copy()
|
|
|
|
request_copy.pop('account')
|
|
|
|
request.GET = request_copy
|
2014-07-29 14:29:59 +00:00
|
|
|
context.update(extra_context or {})
|
2015-05-12 12:38:40 +00:00
|
|
|
return super(AccountAdminMixin, self).changelist_view(request, extra_context=context)
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SelectAccountAdminMixin(AccountAdminMixin):
|
|
|
|
""" Provides support for accounts on ModelAdmin """
|
|
|
|
def get_inline_instances(self, request, obj=None):
|
2015-03-26 16:00:30 +00:00
|
|
|
inlines = super(AccountAdminMixin, self).get_inline_instances(request, obj)
|
2014-10-03 14:02:11 +00:00
|
|
|
if self.account:
|
2014-05-08 16:59:35 +00:00
|
|
|
account = self.account
|
|
|
|
else:
|
|
|
|
account = Account.objects.get(pk=request.GET['account'])
|
2015-10-05 13:31:08 +00:00
|
|
|
[setattr(inline, 'account', account) for inline in inlines]
|
2014-05-08 16:59:35 +00:00
|
|
|
return inlines
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
def get_urls(self):
|
|
|
|
""" Hooks select account url """
|
|
|
|
urls = super(AccountAdminMixin, self).get_urls()
|
|
|
|
admin_site = self.admin_site
|
|
|
|
opts = self.model._meta
|
2014-07-08 15:19:15 +00:00
|
|
|
info = opts.app_label, opts.model_name
|
2014-05-08 16:59:35 +00:00
|
|
|
account_list = AccountListAdmin(Account, admin_site).changelist_view
|
2015-05-19 13:27:04 +00:00
|
|
|
select_urls = [
|
2015-10-07 12:34:50 +00:00
|
|
|
url("add/select-account/$",
|
2014-05-08 16:59:35 +00:00
|
|
|
wrap_admin_view(self, account_list),
|
|
|
|
name='%s_%s_select_account' % info),
|
2015-05-19 13:27:04 +00:00
|
|
|
]
|
2015-10-05 13:31:08 +00:00
|
|
|
return select_urls + urls
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
def add_view(self, request, form_url='', extra_context=None):
|
|
|
|
""" Redirects to select account view if required """
|
|
|
|
if request.user.is_superuser:
|
2014-07-29 14:29:59 +00:00
|
|
|
from_account_id = self.get_account_from_preserve_filters(request)
|
|
|
|
if from_account_id:
|
|
|
|
set_url_query(request, 'account', from_account_id)
|
|
|
|
account_id = request.GET.get('account')
|
|
|
|
if account_id or Account.objects.count() == 1:
|
2014-05-08 16:59:35 +00:00
|
|
|
kwargs = {}
|
2014-07-29 14:29:59 +00:00
|
|
|
if account_id:
|
|
|
|
kwargs = dict(pk=account_id)
|
2014-05-08 16:59:35 +00:00
|
|
|
self.account = Account.objects.get(**kwargs)
|
|
|
|
opts = self.model._meta
|
|
|
|
context = {
|
2014-09-30 09:49:07 +00:00
|
|
|
'title': _("Add %s for %s") % (opts.verbose_name, self.account.username),
|
2014-07-29 14:29:59 +00:00
|
|
|
'from_account': bool(from_account_id),
|
2015-10-07 12:34:50 +00:00
|
|
|
'from_select': True,
|
2014-07-29 14:29:59 +00:00
|
|
|
'account': self.account,
|
|
|
|
'account_opts': Account._meta,
|
2014-05-08 16:59:35 +00:00
|
|
|
}
|
|
|
|
context.update(extra_context or {})
|
2015-10-05 13:31:08 +00:00
|
|
|
return super(AccountAdminMixin, self).add_view(
|
|
|
|
request, form_url=form_url, extra_context=context)
|
2014-09-05 14:27:30 +00:00
|
|
|
return HttpResponseRedirect('./select-account/?%s' % request.META['QUERY_STRING'])
|
2021-04-21 12:27:18 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
def save_model(self, request, obj, form, change):
|
|
|
|
"""
|
|
|
|
Given a model instance save it to the database.
|
|
|
|
"""
|
|
|
|
if not change:
|
|
|
|
obj.account_id = self.account.pk
|
|
|
|
obj.save()
|