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)

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='').values_list('name', flat=True)
                if no_boundaries:
                    messages.warning(request, msg + " %d did not match a boundary (%s)." % (len(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)

fix last commit