summaryrefslogtreecommitdiff
path: root/workshop_app/admin.py
blob: d236c3978a8baed956f91afb333f9fe5101efc59 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import pandas as pd

from django.contrib import admin
from django.http import HttpResponse

from .models import (
    Profile, WorkshopType,
    Workshop,
    Testimonial, Comment, Banner, AttachmentFile
)

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


# Custom Classes
class ProfileAdmin(admin.ModelAdmin):
    list_display = ['title', 'user', 'institute', 'location', 'department',
                    'phone_number', 'position']
    list_filter = ['position', 'department']
    actions = ['download_csv']

    def download_csv(self, request, queryset):
        data = queryset.values(
            "title", "user__first_name", "user__last_name",
            "user__email", "institute", "location", "department",
            "phone_number"
        )
        df = pd.DataFrame(data)
        response = HttpResponse(content_type='text/csv')
        response['Content-Disposition'] = f'attachment; filename=profile.csv'
        output_file = df.to_csv(response, index=False)
        return response

    download_csv.short_description = "Download CSV file for selected stats."


class WorkshopAdmin(admin.ModelAdmin):
    list_display = ['workshop_type', 'instructor', 'date', 'status', 'coordinator']
    list_filter = ['workshop_type', 'date']
    actions = ['download_csv']

    def download_csv(self, request, queryset):
        data = queryset.values(
            "workshop_type__name", "date", "coordinator__first_name",
            "coordinator__last_name", "instructor__first_name",
            "instructor__last_name", "status"
        )
        df = pd.DataFrame(data)
        df.status.replace(
            [0, 1, 2], ['Pending', 'Success', 'Reject'], inplace=True
        )
        response = HttpResponse(content_type='text/csv')
        response['Content-Disposition'] = f'attachment; filename=workshops.csv'
        output_file = df.to_csv(response, index=False)
        return response

    download_csv.short_description = "Download CSV file for selected stats."


class AttachmentFileInline(admin.TabularInline):
    model = AttachmentFile


class WorkshopTypeAdmin(admin.ModelAdmin):
    list_display = ['name', 'duration']
    list_filter = ['name']
    actions = ['download_csv']
    inlines = [AttachmentFileInline]

    def download_csv(self, request, queryset):
        openfile = string_io()
        response = HttpResponse(content_type='text/csv')
        response['Content-Disposition'] = 'attachment;filename=workshoptype_data.csv'
        writer = csv.writer(response)
        writer.writerow(['name', 'duration'])

        for q in queryset:
            writer.writerow([q.name, q.duration])

        openfile.seek(0)
        response.write(openfile.read())
        return response

    download_csv.short_description = "Download CSV file for selected stats."


class TestimonialAdmin(admin.ModelAdmin):
    list_display = ['name', 'department', 'institute']
    list_filter = ['department']
    actions = ['download_csv']

    def download_csv(self, request, queryset):
        openfile = string_io()
        response = HttpResponse(content_type='text/csv')
        response['Content-Disposition'] = 'attachment;filename=testimonials_data.csv'
        writer = csv.writer(response)
        writer.writerow(['name', 'department', 'institute'])

        for q in queryset:
            writer.writerow([q.name,
                             q.department,
                             q.institute
                             ])

        openfile.seek(0)
        response.write(openfile.read())
        return response

    download_csv.short_description = "Download CSV file for selected stats."


class CommentAdmin(admin.ModelAdmin):
    list_display = ['workshop', 'comment', 'created_date', 'author', 'public']
    list_filter = ['workshop', 'author', 'created_date', 'public']


# Register your models here.
admin.site.register(Profile, ProfileAdmin)
admin.site.register(WorkshopType, WorkshopTypeAdmin)
admin.site.register(Workshop, WorkshopAdmin)
admin.site.register(Testimonial, TestimonialAdmin)
admin.site.register(Comment, CommentAdmin)
admin.site.register(Banner)
admin.site.register(AttachmentFile)