from django.contrib import admin
from django.db.models import F, Sum, IntegerField
from django.utils.translation import ugettext_lazy as _

import nested_admin

from .models import District, Location
from campaigns.models import CampaignLocationShift
from coordinators.models import filter_by_district

from campaigns.admin import (
    VolunteerParticipantInline,
    CampaignLocationShiftForm
)

from .admin_actions import list_volunteers_by_shift_and_location

class CampaignShiftInline(nested_admin.NestedTabularInline):
    form = CampaignLocationShiftForm
    model = CampaignLocationShift
    fields = ['day', 'start', 'end', 'total_places', 'shift_leader']
    readonly_fields = ['day', 'start', 'end', 'total_places']
    exclude = ['volunteers']
    inlines = [VolunteerParticipantInline]
    extra = 0


class LocationAdmin(nested_admin.NestedModelAdmin):
    search_fields = ['name', 'address',
            'campaignlocationshift__volunteers__first_name',
            'campaignlocationshift__volunteers__last_name']
    list_filter = ['district']
    inlines = [CampaignShiftInline]
    list_display = ['name', 'volunteers_count', 'free_places']
    save_on_top = True
    actions = [list_volunteers_by_shift_and_location]

    def volunteers_count(self, obj):
        return obj.campaignlocationshift_set.aggregate(
                count=Sum('volunteers__participant_count'))['count']
    volunteers_count.short_description = _('Volunteers count')

    def free_places(self, obj):
        total_places = obj.campaignlocationshift_set.aggregate(total=Sum(
            'total_places'))['total']
        taken_places = obj.campaignlocationshift_set.aggregate(taken=Sum(
            'volunteers__participant_count'))['taken']
        return int(total_places or 0) - int(taken_places or 0)
    free_places.short_description = _('Free places')

    def get_queryset(self, request):
        qs = super(LocationAdmin, self).get_queryset(request)
        return filter_by_district(qs, request.user, 'district')


admin.site.register(District)
admin.site.register(Location, LocationAdmin)

@@ -2009,16 +2009,181 @@
 rict')%0A%0A
+    def get_actions(self, request):%0A        actions = super(LocationAdmin, self).get_actions(request)%0A        del actions%5B'delete_selected'%5D%0A        return actions%0A%0A
 %0Aadmin.s

Disable Location delete action as it is close to print