Fakultas Ilmu Komputer UI
Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
ppl-fasilkom-ui
2020
PPL-C
Diskominfo-D'Blood
Mantan Aab-D Blood
Commits
9c277e49
Verified
Commit
9c277e49
authored
Jun 21, 2020
by
Giovan Isa Musthofa
Browse files
[REFACTOR] Move download_jadwaldonor to donor/services.py
parent
70a284be
Changes
3
Hide whitespace changes
Inline
Side-by-side
backend/donor/admin.py
View file @
9c277e49
from
main.admin
import
InputFilter
from
donor.models
import
DaftarDonor
,
JadwalDonor
from
django.utils.translation
import
gettext_lazy
as
_
from
django.http
import
HttpResponse
from
django.conf
import
settings
from
django.db.models
import
Q
from
django.contrib
import
admin
from
django.db.models
import
Q
from
django.http
import
HttpResponse
from
django.utils.translation
import
gettext_lazy
as
_
from
.models
import
JadwalDonor
,
DaftarDonor
from
.services
import
jadwal_donor_spreadsheet_service
from
main.admin
import
InputFilter
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'
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
)
writer
=
pd
.
ExcelWriter
(
response
,
engine
=
'xlsxwriter'
)
df
.
to_excel
(
writer
,
sheet_name
=
'Sheet1'
)
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
)):
worksheet
.
set_column
(
i
,
i
,
width
,
text_format
)
workbook
.
close
()
return
response
return
jadwal_donor_spreadsheet_service
.
download_jadwaldonor
(
modeladmin
,
queryset
)
download_jadwaldonor
.
short_description
=
'Download Jadwal Donor as Excel Workbook'
...
...
backend/donor/services.py
View file @
9c277e49
import
os
from
django.conf
import
settings
from
django.http
import
HttpResponse
from
django_pandas.io
import
read_frame
import
pandas
as
pd
from
PIL
import
Image
,
ImageDraw
,
ImageFont
from
main.utils.numbers
import
last_digit
,
second_last_digit
from
main.utils.python
import
default_kwargs_with_override
from
.models
import
DaftarDonor
resource_path
=
os
.
path
.
join
(
settings
.
BASE_DIR
,
"donor"
,
"resources"
)
...
...
@@ -228,3 +233,61 @@ class FormulirDaftarDonorService:
formulir_daftar_donor_service
=
FormulirDaftarDonorService
()
class
JadwalDonorSpreadsheetService
:
def
download_jadwaldonor
(
self
,
modeladmin
,
queryset
):
response
=
HttpResponse
(
content_type
=
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
)
response
[
'Content-Disposition'
]
=
f
'attachment; filename=
{
modeladmin
.
model
.
_meta
}
.xlsx'
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
)
writer
=
pd
.
ExcelWriter
(
response
,
engine
=
'xlsxwriter'
)
df
.
to_excel
(
writer
,
sheet_name
=
'Sheet1'
)
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
)):
worksheet
.
set_column
(
i
,
i
,
width
,
text_format
)
workbook
.
close
()
return
response
jadwal_donor_spreadsheet_service
=
JadwalDonorSpreadsheetService
()
backend/media/formulir-daftar-donor/formulir-1.pdf
View file @
9c277e49
No preview for this file type
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment