From 07d9d163dee57c1473e271ddae55cd8a5314cdc3 Mon Sep 17 00:00:00 2001 From: Andrew Williams Date: Wed, 10 Apr 2013 22:31:34 +0100 Subject: [PATCH] Add a set country function on Counties admin. --- app/stores/admin.py | 23 +++++++++++++++++++++ app/stores/templates/admin/set_country.html | 19 +++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 app/stores/templates/admin/set_country.html diff --git a/app/stores/admin.py b/app/stores/admin.py index ac4a102..0a5c512 100644 --- a/app/stores/admin.py +++ b/app/stores/admin.py @@ -112,6 +112,7 @@ class ClaimAdmin(admin.ModelAdmin): class CountyAdmin(admin.ModelAdmin): list_display = ['pk', 'name', 'country', 'address_count'] + actions = ['set_country'] def address_count(self, obj): return obj.address_count @@ -121,6 +122,28 @@ class CountyAdmin(admin.ModelAdmin): qs = super(CountyAdmin, self).queryset(request) return qs.annotate(address_count=Count('addresses')) + class SetCountryForm(forms.Form): + _selected_action = forms.CharField(widget=forms.MultipleHiddenInput) + country = forms.ModelChoiceField(Country.objects) + + def set_country(self, request, queryset): + form = None + if 'apply' in request.POST: + form = self.SetCountryForm(request.POST) + if form.is_valid(): + country = form.cleaned_data['country'] + with transaction.commit_on_success(): + for county in queryset: + county.country = country + county.save() + self.message_user(request, "Successfully set %d counties to %s." % (queryset.count(), country)) + return HttpResponseRedirect(request.get_full_path()) + if not form: + form = self.SetCountryForm(initial={'_selected_action': queryset.values_list('id', flat=True)}) + return render_to_response('admin/set_country.html', {'stores': queryset, 'country_form': form}, RequestContext(request)) + + set_country.short_description = "Set the selected county's country" + class CountryAdmin(admin.ModelAdmin): list_display = ['pk', 'name', 'address_count', 'county_count'] diff --git a/app/stores/templates/admin/set_country.html b/app/stores/templates/admin/set_country.html new file mode 100644 index 0000000..ed662d0 --- /dev/null +++ b/app/stores/templates/admin/set_country.html @@ -0,0 +1,19 @@ +{% extends "admin/base_site.html" %} + +{% block content %} + +

Set the county's country to:

+ +
+ + {{ country_form }} + +

The following counties will be set:

+ + + + + + {% csrf_token %} +
+{% endblock %} \ No newline at end of file