Fakultas Ilmu Komputer UI

services.py 8.94 KB
Newer Older
1
2
3
4
5
import os

from django.conf import settings
from PIL import Image, ImageDraw, ImageFont

6
7
8
9
10
from main.utils.numbers import last_digit, second_last_digit
from main.utils.python import default_kwargs_with_override

resource_path = os.path.join(settings.BASE_DIR, "donor", "resources")

11
12
13

class FormulirDaftarDonorService:

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
    def _draw_formulir_data_diri(self, daftar_donor, image):
        font_path = os.path.join(resource_path, "DejaVuSans.ttf")
        regular_font = ImageFont.truetype(font_path, 24)
        unicode_font = ImageFont.truetype(font_path, 40)

        user = daftar_donor.user
        profile = user.profile
        draw = ImageDraw.Draw(image)
        kwargs = {
            "fill": (0, 0, 0),
            "font": regular_font
        }

        first_column_x = 135
        second_column_x = 720
        first_row_y = 265
        delta_y = 42
        draw.text(xy=(first_column_x, first_row_y), text=profile.id_card_no, **kwargs)
        draw.text(xy=(first_column_x, first_row_y + delta_y), text=user.first_name, **kwargs)
        draw.text(xy=(first_column_x, first_row_y + 2*delta_y), text=profile.address, **kwargs)
        draw.text(xy=(first_column_x, first_row_y + 4*delta_y), text=profile.phone_no, **kwargs)
        draw.text(xy=(first_column_x, first_row_y + 6*delta_y), text=profile.work_address, **kwargs)
        draw.text(xy=(first_column_x, first_row_y + 7*delta_y),
                  text=profile.work_phone_no if len(profile.work_phone_no) > 6 else "", **kwargs)
        draw.text(xy=(first_column_x, first_row_y + int(9.8*delta_y)), text=profile.birthplace, **kwargs)
        draw.text(xy=(first_column_x + 10, first_row_y + 12*delta_y),
                  text=str(second_last_digit(profile.birthdate.day)), **kwargs)
        draw.text(xy=(first_column_x + 10 + 50, first_row_y + 12*delta_y),
                  text=str(last_digit(profile.birthdate.day)), **kwargs)
        draw.text(xy=(first_column_x + 160, first_row_y + 12*delta_y),
                  text=str(second_last_digit(profile.birthdate.month)), **kwargs)
        draw.text(xy=(first_column_x + 160 + 50, first_row_y + 12*delta_y),
                  text=str(last_digit(profile.birthdate.month)), **kwargs)
        draw.text(xy=(first_column_x + 315, first_row_y + 12*delta_y),
                  text=str(second_last_digit(profile.birthdate.year)), **kwargs)
        draw.text(xy=(first_column_x + 315 + 50, first_row_y + 12*delta_y),
                  text=str(last_digit(profile.birthdate.year)), **kwargs)

        draw.text(xy=(second_column_x + (125 if profile.sex == 'F' else 0), first_row_y + int(1.1*delta_y)),
                  text="✔", **default_kwargs_with_override(kwargs, {"font": unicode_font}))
        draw.text(xy=(second_column_x, first_row_y + 2*delta_y), text=profile.village, **kwargs)
        draw.text(xy=(second_column_x, first_row_y + 3*delta_y), text=profile.district, **kwargs)
        draw.text(xy=(second_column_x, first_row_y + 4*delta_y), text=profile.city, **kwargs)
        draw.text(xy=(second_column_x, first_row_y + 5*delta_y), text=profile.married_status, **kwargs)

    def _draw_formulir_kondisi(self, daftar_donor, image):
60
        font_path = os.path.join(resource_path, "DejaVuSans.ttf")
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
        regular_font = ImageFont.truetype(font_path, 24)
        unicode_font = ImageFont.truetype(font_path, 40)

        draw = ImageDraw.Draw(image)
        kwargs = {
            "text": "✔",
            "fill": (0, 0, 0),
            "font": unicode_font
        }
        first_column_yes_x = 1055
        first_column_no_x = 1130
        second_column_yes_x = 2230
        second_column_no_x = 2305
        delta_y = 33

        first_column_fields_with_start_y = [
            {
                "start_y": 300,
                "fields": [
                    "merasa_sehat",
                    "minum_antibiotik",
                    "minum_obat_infeksi",
                ]
            },
            {
                "start_y": 435,
                "fields": [
                    "minum_aspirin",
                ]
            },
            {
                "start_y": 500,
                "fields": [
                    "sakit_kepala_dan_demam",
                ]
            },
            {
                "start_y": 595,
                "fields": [
                    "sedang_hamil",
                ]
            },
            {
                "start_y": 656,
                "fields": [
                    "donor_darah_trombosit_plasma",
                    "menerima_vaksinasi",
                    "kontak_vaksinasi",
                ]
            },
            {
                "start_y": 785,
                "fields": [
                    "donor_aferesis",
                ]
            },
            {
                "start_y": 850,
                "fields": [
                    "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",
                ]
            }
        ]
        for column_with_start_y in first_column_fields_with_start_y:
            start_y = column_with_start_y["start_y"]
            fields = column_with_start_y["fields"]
            for i, field in enumerate(fields):
142
                x = first_column_yes_x if getattr(daftar_donor, field) is True else first_column_no_x
143
144
                draw.text(xy=(x, start_y + i*delta_y), **kwargs)

145
146
147
148
149
150
            if daftar_donor.sedang_hamil:
                draw.text(xy=(950, 595), **default_kwargs_with_override(kwargs, {
                    "text": str(daftar_donor.kehamilan_berapa),
                    "font": regular_font
                }))

151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
        second_column_fields_with_start_y = [
            {
                "start_y": 333,
                "fields": [
                    "pernah_di_luar_indonesia",
                ]
            },
            {
                "start_y": 430,
                "fields": [
                    "pernah_jualan_seks",
                ]
            },
            {
                "start_y": 500,
                "fields": [
                    "pernah_homoseks",
                ]
            },
            {
                "start_y": 590,
                "fields": [
                    "pernah_tinggal_di_eropa",
                    "pernah_terima_transfusi_di_inggris",
                ]
            },
            {
                "start_y": 730,
                "fields": [
                    "pernah_tinggal_di_inggris",
                ]
            },
            {
                "start_y": 820,
                "fields": [
                    "positif_aids",
                ]
            },
            {
                "start_y": 885,
                "fields": [
                    "pakai_jarum_suntik",
                    "pakai_konsentrat",
                    "menderita_hepatitis",
                    "menderita_malaria",
                    "menderita_kanker",
                    "bermasalah_jantung_paru_paru",
                    "menderita_pendarahan",
                    "seks_dengan_orang_afrika",
                    "tinggal_di_afrika",
                ]
            },
        ]

        for column_with_start_y in second_column_fields_with_start_y:
            start_y = column_with_start_y["start_y"]
            fields = column_with_start_y["fields"]
            for i, field in enumerate(fields):
209
                x = second_column_yes_x if getattr(daftar_donor, field) is True else second_column_no_x
210
211
212
213
214
215
                draw.text(xy=(x, start_y + i*delta_y), **kwargs)

    def generate_formulir(self, daftar_donor):
        template_data_diri_path = os.path.join(resource_path, "form-a4-data_diri.jpg")
        formulir_data_diri = Image.open(template_data_diri_path).convert("RGB")
        self._draw_formulir_data_diri(daftar_donor, formulir_data_diri)
216

217
218
219
        template_kondisi_path = os.path.join(resource_path, "form-a4-kondisi.jpg")
        formulir_kondisi = Image.open(template_kondisi_path).convert("RGB")
        self._draw_formulir_kondisi(daftar_donor, formulir_kondisi)
220
221
222
223
224
225

        formulir_folder = 'formulir-daftar-donor'
        formulir_name = os.path.join(formulir_folder, 'formulir-' + str(daftar_donor.pk) + '.pdf')
        formulir_folder_path = os.path.join(settings.MEDIA_ROOT, formulir_folder)
        os.makedirs(formulir_folder_path, exist_ok=True)
        formulir_path = os.path.join(settings.MEDIA_ROOT, formulir_name)
226
        formulir_data_diri.save(formulir_path, save_all=True, append_images=[formulir_kondisi])
227
228
229
230
        return settings.MEDIA_URL + formulir_name


formulir_daftar_donor_service = FormulirDaftarDonorService()