summaryrefslogtreecommitdiff
path: root/website/admin.py
blob: 7a6ba2e04ef788416484463c313bb2fa5cb5c6ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from django.contrib import admin
import csv
from django.http import HttpResponse
from .models import( Profile,
                    AnswerPaper,
                    Question,
                    UploadModel
                   )

try:
    from StringIO import StringIO as string_io
except ImportError:
    from io import BytesIO as string_io


class ProfileAdmin(admin.ModelAdmin):
	list_display = ['title','user', 'institute', 
					'phone_number','position']


class QuestionAdmin(admin.ModelAdmin):
    list_display = ['question', 'correct_answer', 'question_day']



class AnswerPaperAdmin(admin.ModelAdmin):
    list_display = ['participant', 'answered_q', 'date']
    actions = ['download_csv']

    def download_csv(self, request, queryset):
        openfile = string_io() 
        response = HttpResponse(content_type='text/csv')
        response['Content-Disposition'] = 'attachment;\
										filename=AnswerPaper_data.csv'
                                        
        writer = csv.writer(response)
        writer.writerow(['Participant', 'Answer date/time'])
        for q in queryset:
            writer.writerow([q.participant.user.get_full_name(), q.date])
    
        openfile.seek(0)
        response.write(openfile.read())
        return response
	
    download_csv.short_description = "Download CSV file for selected stats."



class UploadModelAdmin(admin.ModelAdmin):
    list_display = ['question']


# Register your models here.
admin.site.register(Profile, ProfileAdmin)
admin.site.register(AnswerPaper, AnswerPaperAdmin)
admin.site.register(Question, QuestionAdmin)
admin.site.register(UploadModel, UploadModelAdmin)