import csv

from django.db.models import Count
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from django.http import HttpResponse

from moderator.moderate.models import Event, MozillianProfile, Vote, Question


# Unregister User from admin to attach MozillianProfile
admin.site.unregister(User)


def export_questions_csv(modeladmin, request, queryset):
    """Export questions csv."""
    filename = 'questions.csv'
    response = HttpResponse(mimetype='text/csv')
    response['Content-Disposition'] = 'attachment; filename="%s"' % filename

    writer = csv.writer(response)

    for e in queryset:
        writer.writerow([e.name])
        questions = (e.questions
                     .annotate(vote_count=Count('votes'))
                     .order_by('-vote_count'))
        for q in questions:
            writer.writerow([q.question.encode('utf-8'), q.votes.count()])

    return response


class MozillianProfileInline(admin.StackedInline):
    model = MozillianProfile
    fk_name = 'user'


class UserAdmin(UserAdmin):
    inlines = [MozillianProfileInline]
    list_display = ('username', 'email', 'first_name', 'last_name', 'is_nda_member', 'is_staff',)

    def is_nda_member(self, obj):
        return obj.userprofile.is_nda_member
    is_nda_member.boolean = True


class QuestionInline(admin.StackedInline):
    model = Question
    fields = ('addressed', 'question', 'asked_by',)
    readonly_fields = ('question', 'asked_by',)
    extra = 0


class EventAdmin(admin.ModelAdmin):
    inlines = [QuestionInline]
    list_display = ('name', 'questions_count', 'created_at', 'archived', 'is_nda',)
    actions = [export_questions_csv]
    list_filter = ['archived']


class QuestionAdmin(admin.ModelAdmin):
    list_display = ('id', 'asked_by', 'event')


admin.site.register(User, UserAdmin)
admin.site.register(Event, EventAdmin)
admin.site.register(Question, QuestionAdmin)
admin.site.register(Vote)

@@ -9,43 +9,8 @@
 sv%0A%0A
-from django.db.models import Count%0A
 from
@@ -130,16 +130,51 @@
 rt User%0A
+from django.db.models import Count%0A
 from dja
@@ -269,14 +269,8 @@
 ile,
- Vote,
  Que
@@ -274,17 +274,22 @@
 Question
-%0A
+, Vote
 %0A%0A# Unre
@@ -472,17 +472,17 @@
 ename = 
-'
+%22
 question
@@ -486,17 +486,17 @@
 ions.csv
-'
+%22
 %0A    res
@@ -529,17 +529,17 @@
 ype=
-'
+%22
 text/csv
 ')%0A 
@@ -534,17 +534,17 @@
 text/csv
-'
+%22
 )%0A    re
@@ -550,17 +550,17 @@
 esponse%5B
-'
+%22
 Content-
@@ -570,17 +570,17 @@
 position
-'
+%22
 %5D = 'att
@@ -727,17 +727,16 @@
 tions = 
-(
 e.questi
@@ -738,38 +738,16 @@
 uestions
-%0A                     
 .annotat
@@ -769,17 +769,27 @@
 unt(
-'
+%22
 votes
-'
+%22
 ))
+.order_by(
 %0A   
@@ -801,28 +801,9 @@
     
-         .order_by('
+%22
 -vot
@@ -809,18 +809,26 @@
 te_count
-')
+%22%0A        
 )%0A      
@@ -900,15 +900,15 @@
 ode(
-'
+%22
 utf-8
-'
+%22
 ), q
@@ -1045,14 +1045,14 @@
 e = 
-'
+%22
 user
-'
+%22
 %0A%0A%0Ac
@@ -1141,85 +1141,196 @@
  = (
-'username', 'email', 'first_name', 'last_name', 'is_nda_member', 'is_staff',)
+%0A        %22username%22,%0A        %22email%22,%0A        %22first_name%22,%0A        %22last_name%22,%0A        %22is_nda_member%22,%0A        %22is_staff%22,%0A    )%0A    search_fields = %5B%22email%22, %22first_name%22, %22last_name%22%5D
 %0A%0A  
@@ -1406,16 +1406,17 @@
 _member%0A
+%0A
     is_n
@@ -1524,42 +1524,67 @@
  = (
-'addressed', 'question', '
+%0A        %22addressed%22,%0A        %22question%22,%0A        %22
 asked_by
 ',)%0A
@@ -1575,26 +1575,31 @@
    %22asked_by
-',
+%22,%0A    
 )%0A    readon
@@ -1615,29 +1615,46 @@
  = (
-'question', '
+%0A        %22question%22,%0A        %22
 asked_by
 ',)%0A
@@ -1649,18 +1649,23 @@
 asked_by
-',
+%22,%0A    
 )%0A    ex
@@ -1765,70 +1765,116 @@
  = (
-'name', 'questions_count', 'created_at', 'archived', 'is_nda',
+%0A        %22name%22,%0A        %22questions_count%22,%0A        %22created_at%22,%0A        %22archived%22,%0A        %22is_nda%22,%0A    
 )%0A  
@@ -1931,17 +1931,17 @@
  = %5B
-'
+%22
 archived
 '%5D%0A%0A
@@ -1936,17 +1936,58 @@
 archived
-'
+%22%5D%0A    autocomplete_fields = %5B%22moderators%22
 %5D%0A%0A%0Aclas
@@ -2045,23 +2045,23 @@
  = (
-'id', '
+%22id%22, %22
 asked_by
 ', '
@@ -2060,18 +2060,18 @@
 d_by
-', '
+%22, %22
 event
-'
+%22
 )%0A%0A%0A

Use select2 autocomplete in admin.