def make_mailing_list_all(self, request, queryset):
from emencia.django.newsletter.models import Contact
from emencia.django.newsletter.models import MailingList
subscribers = []
for order in OrderRecurring.objects.filter(status='active'):
contact, created = Contact.objects.get_or_create(email=order.customer_email,
defaults={'first_name': order.customer_first_name,
'last_name': order.customer_last_name,
'content_object': order})
subscribers.append(contact)
new_mailing = MailingList(name='All Order recurrings mailing list',
description='New mailing list created from all order recurrings')
new_mailing.save()
new_mailing.subscribers.add(*subscribers)
new_mailing.save()
self.message_user(request, '%s succesfully created.' % new_mailing)
make_mailing_list_all.short_description = _('Create a mailing list for all active recurring orders')
actions = ['make_mailing_list_all',]
Стандартно, если в списке не выбран ни один чекбокс, action не срабатывает. Нужно переписать метод admin.ModelAdmin.response_action:
def response_action(self, request, queryset):
from django.contrib.admin import helpers
try:
action_index = int(request.POST.get('index', 0))
except ValueError:
action_index = 0
data = request.POST.copy()
data.pop(helpers.ACTION_CHECKBOX_NAME, None)
data.pop("index", None)
try:
data.update({'action': data.getlist('action')[action_index]})
except IndexError:
pass
action_form = self.action_form(data, auto_id=None)
action_form.fields['action'].choices = self.get_action_choices(request)
if action_form.is_valid():
action = action_form.cleaned_data['action']
func, name, description = self.get_actions(request)[action]
selected = request.POST.getlist(helpers.ACTION_CHECKBOX_NAME)
### CHANGED ###
if not selected:
if action == u'make_mailing_list_all':
pass
else:
return None
else:
queryset = queryset.filter(pk__in=selected)
response = func(self, request, queryset)
### END CHANGED ###
if isinstance(response, HttpResponse):
return response
else:
return HttpResponseRedirect(".")
No comments:
Post a Comment