Fakultas Ilmu Komputer UI

admin.py 4.97 KB
Newer Older
1
from django.contrib import admin
2
from django.conf import settings
3
from django.http import HttpResponse
4

5
from donor.models import JadwalDonor, DaftarDonor
6
7


8
9
10
11
12
13
14
def download_jadwaldonor(modeladmin, request, queryset):
    import pandas as pd
    from django_pandas.io import read_frame

    response = HttpResponse(content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
    response['Content-Disposition'] = f'attachment; filename={modeladmin.model._meta}.xlsx'

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
    daftar_donor = DaftarDonor.download_query(queryset)
    fieldnames = [
        'id',
        'jadwal_donor__time_start',
        'user__profile__id_card_no',
        'user__first_name',
        'user__profile__address',
        'user__profile__phone_no',
        'user__profile__blood_type',
        'user__profile__sex',
    ]
    df = read_frame(daftar_donor, fieldnames=fieldnames)
    df = df.rename(columns={
        fieldnames[0]: "NoTrans",
        fieldnames[1]: "Tanggal",
        fieldnames[2]: "ID",
        fieldnames[3]: "Nama Lengkap",
        fieldnames[4]: "Alamat",
        fieldnames[5]: "HP",
        fieldnames[6]: "Gol (RH)",
        fieldnames[7]: "JK",
    })

    if len(df):
        df['Tanggal'] = df['Tanggal'].dt.tz_localize(None)
40
41
42
43
44

    writer = pd.ExcelWriter(response, engine='xlsxwriter')
    df.to_excel(writer, sheet_name='Sheet1')

    workbook = writer.book
45
46
47
48
49
50
51
52
    worksheet = writer.sheets['Sheet1']

    def get_col_widths(dataframe):
        # First we find the maximum length of the index column
        idx_max = max([len(str(s)) for s in dataframe.index.values] + [len(str(dataframe.index.name))])
        # Then, we concatenate this to the max of the lengths of column name
        # and its values for each column, left to right
        return [idx_max] + [max([len(str(s)) for s in dataframe[col].values] + [len(col)]) for col in dataframe.columns]
53

54
55
56
57
58
59
    text_format = workbook.add_format({'text_wrap': True})

    for i, width in enumerate(get_col_widths(df)):
        worksheet.set_column(i, i, width, text_format)

    workbook.close()
60
61
62
63
64
65
    return response


download_jadwaldonor.short_description = 'Download Jadwal Donor as Excel Workbook'


66
67
@admin.register(JadwalDonor)
class JadwalDonorAdmin(admin.ModelAdmin):
68
    list_filter = ('kecamatan', 'time_start', 'category')
69
    search_fields = ('time_start', 'location')
70
    actions = [download_jadwaldonor]
71

72
73
74
    class Media:
        js = [settings.MEDIA_URL+'/js/acara_donor_custom_admin.js', ]

75
76
77

@admin.register(DaftarDonor)
class DaftarDonorAdmin(admin.ModelAdmin):
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
128
129
130
131
132
    list_display = ('user', 'has_attended', 'get_goldar', 'jadwal_donor')
    search_fields = ('user__email',
                     'jadwal_donor__location', 'jadwal_donor__time_start')
    fields = ('has_attended',
              'id',
              'user',
              'jadwal_donor',
              'merasa_sehat',
              'minum_antibiotik',
              'minum_obat_infeksi',
              'minum_aspirin',
              'sakit_kepala_dan_demam',
              'sedang_hamil',
              'kehamilan_berapa',
              'donor_darah_trombosit_plasma',
              'menerima_vaksinasi',
              'kontak_vaksinasi',
              'donor_aferesis',
              'pernah_transfusi',
              'pernah_transplasi',
              'pernah_cangkok_tulang',
              'pernah_tusuk_jarum_medis',
              'pernah_seks_aids',
              'pernah_seks_psk',
              'pernah_seks_narkoba',
              'pernah_seks_konsentrat',
              'wanita_pernah_seks_laki_biseksual',
              'pernah_seks_dengan_hepatitis',
              'tinggal_dengan_hepatitis',
              'punya_tato',
              'punya_tindik',
              'sedang_sifilis_go',
              'pernah_dipenjara',
              'pernah_di_luar_indonesia',
              'pernah_jualan_seks',
              'pernah_homoseks',
              'pernah_tinggal_di_eropa',
              'pernah_terima_transfusi_di_inggris',
              'pernah_tinggal_di_inggris',
              'positif_aids',
              'pakai_jarum_suntik',
              'pakai_konsentrat',
              'menderita_hepatitis',
              'menderita_malaria',
              'menderita_kanker',
              'bermasalah_jantung_paru_paru',
              'menderita_pendarahan',
              'seks_dengan_orang_afrika',
              'tinggal_di_afrika')
    list_filter = ('has_attended', 'user__profile__blood_type')

    def get_goldar(self, obj):
        return obj.user.profile.blood_type
    get_goldar.short_description = 'Goldar'
    get_goldar.admin_order_field = 'user__profile__blood_type'
133
134
135
136
137
138
139
140
141

    def get_readonly_fields(self, request, obj=None):
        readonly_fields = list(set(
            [field.name for field in self.opts.local_fields] +
            [field.name for field in self.opts.local_many_to_many]
        ))
        if 'has_attended' in readonly_fields:
            readonly_fields.remove('has_attended')
        return readonly_fields
142
143
144

    class Media:
        js = [settings.MEDIA_URL+'/js/daftar_donor_custom_admin.js', ]