mirror of
https://github.com/nikdoof/vapemap.git
synced 2025-12-14 06:42:17 +00:00
Add a set country function on Counties admin.
This commit is contained in:
@@ -112,6 +112,7 @@ class ClaimAdmin(admin.ModelAdmin):
|
|||||||
|
|
||||||
class CountyAdmin(admin.ModelAdmin):
|
class CountyAdmin(admin.ModelAdmin):
|
||||||
list_display = ['pk', 'name', 'country', 'address_count']
|
list_display = ['pk', 'name', 'country', 'address_count']
|
||||||
|
actions = ['set_country']
|
||||||
|
|
||||||
def address_count(self, obj):
|
def address_count(self, obj):
|
||||||
return obj.address_count
|
return obj.address_count
|
||||||
@@ -121,6 +122,28 @@ class CountyAdmin(admin.ModelAdmin):
|
|||||||
qs = super(CountyAdmin, self).queryset(request)
|
qs = super(CountyAdmin, self).queryset(request)
|
||||||
return qs.annotate(address_count=Count('addresses'))
|
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):
|
class CountryAdmin(admin.ModelAdmin):
|
||||||
list_display = ['pk', 'name', 'address_count', 'county_count']
|
list_display = ['pk', 'name', 'address_count', 'county_count']
|
||||||
|
|||||||
19
app/stores/templates/admin/set_country.html
Normal file
19
app/stores/templates/admin/set_country.html
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
{% extends "admin/base_site.html" %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
|
||||||
|
<p>Set the county's country to:</p>
|
||||||
|
|
||||||
|
<form action="" method="post">
|
||||||
|
|
||||||
|
{{ country_form }}
|
||||||
|
|
||||||
|
<p>The following counties will be set:</p>
|
||||||
|
|
||||||
|
<ul>{{ stores|unordered_list }}</ul>
|
||||||
|
|
||||||
|
<input type="hidden" name="action" value="set_country" />
|
||||||
|
<input type="submit" name="apply" value="Apply tag" />
|
||||||
|
{% csrf_token %}
|
||||||
|
</form>
|
||||||
|
{% endblock %}
|
||||||
Reference in New Issue
Block a user