import csv
from datetime import datetime

from django.contrib import admin
from django.contrib.admin import util
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from django.utils.encoding import force_unicode
from django.utils.translation import ugettext_lazy as _

from . import app_settings
from .models import Project, ProjectAdmin, Pledge
from .utils import get_backer_model, use_default_backer_model

from .paypal.models import Preapproval, Payment

def export_as_csv(modeladmin, request, queryset):
    model = modeladmin.model
    response = HttpResponse(mimetype='text/csv')
    model_name = force_unicode(model._meta.verbose_name)
    timestamp = datetime.now().strftime('%d%m%y_%H%M')
    filename = '%s_export_%s.csv' % (model_name, timestamp)
    response['Content-Disposition'] = 'attachment; filename=%s' % filename
    writer = csv.writer(response)
    field_names = modeladmin.list_display

    def get_label(field):
        label = util.label_for_field(field, model, modeladmin)
        return force_unicode(label).title().encode('utf-8')

    def serialize(field, obj):
        f, attr, value = util.lookup_field(field, obj, modeladmin)
        if f is not None:
            value = util.display_for_field(value, f)
        return force_unicode(value).encode('utf-8')

    writer.writerow([get_label(field) for field in field_names])

    for obj in queryset:
        writer.writerow([serialize(field, obj) for field in field_names])

    return response

export_as_csv.short_description = _('Export as csv')


class PledgeInlineAdmin(admin.TabularInline):
    model = Pledge
    extra = 0
    raw_id_fields = ('backer','project')
    feincms_inline = True


class DefaultBackerAdmin(admin.ModelAdmin):
    list_display = ('first_name', 'last_name', 'email')
    search_fields = ('_first_name', '_last_name', '_email', 'user__username', 'user__email')
    raw_id_fields = ['user']
    inlines = [PledgeInlineAdmin]
    actions = [export_as_csv]

if use_default_backer_model():
    BackerModel = get_backer_model()
    admin.site.register(BackerModel, DefaultBackerAdmin)


class RewardListFilter(admin.SimpleListFilter):

    title = _('Reward')
    parameter_name = 'reward'

    def lookups(self, request, model_admin):
        project_id = request.GET.get('project__id__exact', None)
        if  project_id:
            project = get_object_or_404(Project, pk=project_id)
            for reward in project.rewards.all():
                name = '{0} {1}'.format(reward.minimum, project.currency)
                yield (str(reward.pk), name)


    def queryset(self, request, queryset):
        value = self.value()

        if value:
            return queryset.filter(reward__id=value)


class PaypalFilter(admin.SimpleListFilter):

    title = _('Paypal status')
    parameter_name = 'paypal'

    def lookups(self, request, model_admin):
        return (
            ('approved', _('is approved')),
            ('paid', _('is paid'))
        )

    def queryset(self, request, queryset):
        value = self.value()

        if value == 'approved':
            approved = Preapproval.objects.filter(approved=True)
            approved_ids = approved.values_list('pledge__pk')
            return queryset.filter(pk__in=approved_ids)
        if value == 'paid':
            paid = Payment.objects.filter(status=Payment.COMPLETED)
            paid_ids = paid.values_list('preapproval__pledge__pk')
            return queryset.filter(pk__in=paid_ids)


class PledgeAdmin(admin.ModelAdmin):

    def first_name(self, pledge):
        return pledge.backer.first_name
    first_name.short_description = _('first name')

    def last_name(self, pledge):
        return pledge.backer.last_name
    last_name.short_description = _('last name')

    def email(self, pledge):
        return pledge.backer.email
    email.short_description = _('email')

    def amount_display(self, pledge):
        return '%s %s' % (pledge.amount, pledge.currency)
    amount_display.short_description = _('amount')

    list_display = (
        'email',
        'first_name',
        'last_name',
        'amount_display',
        'reward',
        'status'
    )

    search_fields = (
        'backer___first_name',
        'backer___last_name',
        'backer___email',
        'backer__user__username',
        'backer__user__first_name',
        'backer__user__last_name',
        'backer__user__email',
    )

    raw_id_fields = ('backer', 'project')
    radio_fields = {'reward': admin.VERTICAL}
    list_filter = ('project', 'status', PaypalFilter, RewardListFilter)
    actions = [export_as_csv]

admin.site.register(Project, ProjectAdmin)
admin.site.register(Pledge, PledgeAdmin)
@@ -3536,24 +3536,238 @@
 delAdmin):%0A%0A
+    def username(self, pledge):%0A        if pledge.backer and pledge.backer.user:%0A            return pledge.backer.user.username%0A        else:%0A            return None%0A    username.short_description = _('username')%0A%0A
     def firs
@@ -3780,32 +3780,32 @@
 (self, pledge):%0A
-
         return p
@@ -4273,24 +4273,44 @@
 display = (%0A
+        'username',%0A
         'ema
@@ -4421,24 +4421,95 @@
 tus'%0A    )%0A%0A
+    list_display_links = (%0A        'username',%0A        'email',%0A    )%0A%0A
     search_f
@@ -4744,24 +4744,24 @@
 il',%0A    )%0A%0A
+
     raw_id_f
@@ -4794,54 +4794,8 @@
 t')%0A
-    radio_fields = %7B'reward': admin.VERTICAL%7D%0A
     

add username to pledge list