Fakultas Ilmu Komputer UI

Verified Commit fce77f3a authored by Giovan Isa Musthofa's avatar Giovan Isa Musthofa
Browse files

[CHORES] Add 'Donor Ke-' and 'Baru Ulang' column to excel output

parent 9c277e49
from datetime import datetime
import os
from dateutil.relativedelta import relativedelta
from django.conf import settings
from django.http import HttpResponse
from django_pandas.io import read_frame
......@@ -237,6 +239,43 @@ formulir_daftar_donor_service = FormulirDaftarDonorService()
class JadwalDonorSpreadsheetService:
@classmethod
def _birthdates_to_age(cls, birthdates):
today = datetime.today()
return [relativedelta(today, birthdate).years for birthdate in birthdates]
@classmethod
def _get_daftar_donor_ulang_list(cls, daftar_donor_qs):
daftar_donor_ulang_list = []
for daftar_donor in daftar_donor_qs:
daftar_donor_ulang = DaftarDonor.objects.filter(
user=daftar_donor.user,
has_attended=True,
jadwal_donor__time_end__lt=daftar_donor.jadwal_donor.time_start)
daftar_donor_ulang_list.append(daftar_donor_ulang)
return daftar_donor_ulang_list
@classmethod
def _get_baru_ulang(cls, daftar_donor_qs):
baru_ulang_list = []
for daftar_donor in daftar_donor_qs:
daftar_donor_ulang = DaftarDonor.objects.filter(
user=daftar_donor.user,
has_attended=True,
jadwal_donor__time_end__lt=daftar_donor.jadwal_donor.time_start)
baru_ulang_list.append('Ulang' if daftar_donor_ulang.exists() else 'Baru')
return baru_ulang_list
@classmethod
def _get_col_widths(cls, 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]
def download_jadwaldonor(self, modeladmin, queryset):
response = HttpResponse(content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
......@@ -250,19 +289,32 @@ class JadwalDonorSpreadsheetService:
'user__first_name',
'user__profile__address',
'user__profile__phone_no',
'user__profile__birthdate',
'user__profile__blood_type',
'user__profile__sex',
]
df = read_frame(daftar_donor, fieldnames=fieldnames)
df[fieldnames[6]] = self._birthdates_to_age(df[fieldnames[6]])
daftar_donor_ulang_list = self._get_daftar_donor_ulang_list(daftar_donor)
df['Baru/Ulang'] = [
'Ulang' if daftar_donor_ulang.exists() else 'Belum'
for daftar_donor_ulang in daftar_donor_ulang_list
]
df['Daftar Ke-'] = [
len(daftar_donor_ulang) + 1
for daftar_donor_ulang in daftar_donor_ulang_list
]
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",
fieldnames[0]: 'NoTrans',
fieldnames[1]: 'Tanggal',
fieldnames[2]: 'ID',
fieldnames[3]: 'Nama Lengkap',
fieldnames[4]: 'Alamat',
fieldnames[5]: 'HP',
fieldnames[6]: 'Umur',
fieldnames[7]: 'Gol (RH)',
fieldnames[8]: 'JK',
})
if len(df):
......@@ -274,16 +326,9 @@ class JadwalDonorSpreadsheetService:
workbook = writer.book
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]
text_format = workbook.add_format({'text_wrap': True})
for i, width in enumerate(get_col_widths(df)):
for i, width in enumerate(self._get_col_widths(df)):
worksheet.set_column(i, i, width, text_format)
workbook.close()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment