import traceback

from django.contrib import admin, messages

from representatives.models import *

class RepresentativeSetAdmin(admin.ModelAdmin):

    actions = ['update_from_data_source']
    list_display = ['name', 'last_import_time', 'last_import_successful', 'enabled']
    list_filter = ['last_import_successful', 'enabled']

    def update_from_data_source(self, request, queryset):
        for rset in queryset:
            try:
                num_updated = rset.update_from_data_source()
            except Exception as e:
                messages.error(request, u"Fatal error updating %s: %s" % (rset, traceback.format_exc()))
                continue
            if num_updated is False:
                messages.error(request, "%s could not be updated." % rset)
            else:
                msg = "Updated %s representatives for %s." % (num_updated, rset)
                no_boundaries = Representative.objects.filter(representative_set=rset, boundary='').count()
                if no_boundaries:
                    messages.warning(request, msg + " %s did not match a boundary (%s)." % (no_boundaries, ', '.join(no_boundaries)))
                else:
                    messages.success(request, msg + " All matched a boundary.")
    update_from_data_source.short_description = "Update from data source"
    
class RepresentativeAdmin(admin.ModelAdmin):
    list_display = ['name', 'representative_set', 'district_name', 'elected_office', 'boundary']
    list_filter = ['representative_set']
    search_fields = ['name', 'district_name', 'elected_office']

class CandidateAdmin(admin.ModelAdmin):
    list_display = ['name', 'election', 'district_name', 'elected_office', 'boundary']
    list_filter = ['election']
    search_fields = ['name', 'district_name', 'elected_office']    
    
admin.site.register(Representative, RepresentativeAdmin)
admin.site.register(RepresentativeSet, RepresentativeSetAdmin)
if app_settings.ENABLE_CANDIDATES:
    admin.site.register(Candidate, CandidateAdmin)
    admin.site.register(Election, RepresentativeSetAdmin)

@@ -972,14 +972,37 @@
 '').
-count(
+values_list('name', flat=True
 )%0A  
@@ -1088,17 +1088,17 @@
 sg + %22 %25
-s
+d
  did not
@@ -1124,16 +1124,20 @@
 %25s).%22 %25 
+(len
 (no_boun
@@ -1142,16 +1142,17 @@
 undaries
+)
 , ', '.j

fix last commit