# Path: clusters/actions.py
# def export_as_csv(modeladmin, request, queryset):
#     """
#     Generic csv export admin action.
#     """
#     if not request.user.is_staff:
#         raise PermissionDenied
#     opts = modeladmin.model._meta
#     response = HttpResponse(mimetype='text/csv')
#     response['Content-Disposition'] = 'attachment; filename=%s.csv' % unicode(opts).replace('.', '_')
#     writer = csv.writer(response)
#     field_names = [field.name for field in opts.fields]
#     # Write a first row with header information
#     writer.writerow(field_names)
#     # Write data rows
#     for obj in queryset:
#         writer.writerow([getattr(obj, field) for field in field_names])
#     return response


from django.contrib import admin
from clusters.models import *
from clusters.actions import export_as_csv

class OutgoingAgentResourceFlowInline(admin.TabularInline):
    model = AgentResourceFlow
    fk_name = 'from_function'
    verbose_name = 'outgoing flow'
    verbose_name_plural = 'outgoing flows'

class EconomicAgentAdmin(admin.ModelAdmin):
    list_display = ('name', 'address')
    search_fields = ['name', 'address']
    inlines = [AgentFunctionInline,]
    
admin.site.register(EconomicAgent, EconomicAgentAdmin)


class AgentFunctionAdmin(admin.ModelAdmin):
    list_display = ('agent', 'function')
    inlines = [AgentFunctionResourceInline,
               IncomingAgentResourceFlowInline,
               OutgoingAgentResourceFlowInline,
               ]
    
admin.site.register(AgentFunction, AgentFunctionAdmin)


class EconomicResourceTypeAdmin(admin.ModelAdmin):
    list_display = ('name', 'parent')
    list_filter = ['parent']
    search_fields = ['name',]
    

# Path: clusters/actions.py
# def export_as_csv(modeladmin, request, queryset):
#     """
#     Generic csv export admin action.
#     """
#     if not request.user.is_staff:
#         raise PermissionDenied
#     opts = modeladmin.model._meta
#     response = HttpResponse(mimetype='text/csv')
#     response['Content-Disposition'] = 'attachment; filename=%s.csv' % unicode(opts).replace('.', '_')
#     writer = csv.writer(response)
#     field_names = [field.name for field in opts.fields]
#     # Write a first row with header information
#     writer.writerow(field_names)
#     # Write data rows
#     for obj in queryset:
#         writer.writerow([getattr(obj, field) for field in field_names])
#     return response


# Path: clusters/admin.py
from django.contrib import admin
from clusters.models import *
from clusters.actions import export_as_csv


class OutgoingAgentResourceFlowInline(admin.TabularInline):
    model = AgentResourceFlow
    fk_name = 'from_function'
    verbose_name = 'outgoing flow'
    verbose_name_plural = 'outgoing flows'

class EconomicAgentAdmin(admin.ModelAdmin):
    list_display = ('name', 'address')
    search_fields = ['name', 'address']
    inlines = [AgentFunctionInline,]
    
admin.site.register(EconomicAgent, EconomicAgentAdmin)


class AgentFunctionAdmin(admin.ModelAdmin):
    list_display = ('agent', 'function')
    inlines = [AgentFunctionResourceInline,
               IncomingAgentResourceFlowInline,
               OutgoingAgentResourceFlowInline,
               ]
    
admin.site.register(AgentFunction, AgentFunctionAdmin)


class EconomicResourceTypeAdmin(admin.ModelAdmin):
    list_display = ('name', 'parent')
    list_filter = ['parent']
    search_fields = ['name',]
    

admin.site.register(EconomicResourceType, EconomicResourceTypeAdmin)