Fakultas Ilmu Komputer UI

models.py 4.1 KB
Newer Older
1
from django.core.exceptions import ValidationError
2
from django.core.validators import MaxValueValidator, MinValueValidator
3
from django.db import models
4
from django.utils.timezone import localtime
5

6
7
from main.models import User

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

class JadwalDonor(models.Model):

    class Kecamatan(models.TextChoices):
        BEJI = "Beji"
        PANCORAN_MAS = "Pancoran Mas"
        CIPAYUNG = "Cipayung"
        SUKMAJAYA = "Sukmajaya"
        CILODONG = "Cilodong"
        LIMO = "Limo"
        CINERE = "Cinere"
        CIMANGGIS = "Cimanggis"
        TAPOS = "Tapos"
        SAWANGAN = "Sawangan"
        BOJONG_SARI = "Bojong Sari"

    class Category(models.TextChoices):
        PUBLIC = "terbuka"
        PRIVATE = "tertutup"

    kecamatan = models.CharField(max_length=20, choices=Kecamatan.choices)
    location = models.CharField(max_length=30)
    time_start = models.DateTimeField()
    time_end = models.DateTimeField()
    quota = models.PositiveIntegerField()
    category = models.CharField(max_length=20, choices=Category.choices)

    def clean(self):
        if self.time_start >= self.time_end:
            raise ValidationError("time_start should be less than time_end")

    def save(self, *args, **kwargs):
        self.full_clean()
        return super().save(*args, **kwargs)

    def __str__(self):
44
45
46
        time_start = localtime(self.time_start)
        time_end = localtime(self.time_end)
        return f"{self.location}, {time_start.date()} ({time_start.time()} - {time_end.time()})"
47

48
49
50
    class Meta:
        pass

51
52

class DaftarDonor(models.Model):
53
    user = models.ForeignKey(User, on_delete=models.CASCADE)
54
    jadwal_donor = models.ForeignKey(JadwalDonor, on_delete=models.CASCADE, related_name='daftar_donors')
55
    has_attended = models.BooleanField(default=False)
56
57
58
59
60
61
    merasa_sehat = models.BooleanField()
    minum_antibiotik = models.BooleanField()
    minum_obat_infeksi = models.BooleanField()
    minum_aspirin = models.BooleanField()
    sakit_kepala_dan_demam = models.BooleanField()
    sedang_hamil = models.BooleanField()
62
63
    kehamilan_berapa = models.IntegerField(null=True, blank=True, validators=[
                                           MinValueValidator(limit_value=1), MaxValueValidator(limit_value=15)])
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
    donor_darah_trombosit_plasma = models.BooleanField()
    menerima_vaksinasi = models.BooleanField()
    kontak_vaksinasi = models.BooleanField()
    donor_aferesis = models.BooleanField()
    pernah_transfusi = models.BooleanField()
    pernah_transplasi = models.BooleanField()
    pernah_cangkok_tulang = models.BooleanField()
    pernah_tusuk_jarum_medis = models.BooleanField()
    pernah_seks_aids = models.BooleanField()
    pernah_seks_psk = models.BooleanField()
    pernah_seks_narkoba = models.BooleanField()
    pernah_seks_konsentrat = models.BooleanField()
    wanita_pernah_seks_laki_biseksual = models.BooleanField()
    pernah_seks_dengan_hepatitis = models.BooleanField()
    tinggal_dengan_hepatitis = models.BooleanField()
    punya_tato = models.BooleanField()
    punya_tindik = models.BooleanField()
    sedang_sifilis_go = models.BooleanField()
    pernah_dipenjara = models.BooleanField()
    pernah_di_luar_indonesia = models.BooleanField()
    pernah_jualan_seks = models.BooleanField()
    pernah_homoseks = models.BooleanField()
    pernah_tinggal_di_eropa = models.BooleanField()
    pernah_terima_transfusi_di_inggris = models.BooleanField()
    pernah_tinggal_di_inggris = models.BooleanField()
    positif_aids = models.BooleanField()
    pakai_jarum_suntik = models.BooleanField()
    pakai_konsentrat = models.BooleanField()
    menderita_hepatitis = models.BooleanField()
    menderita_malaria = models.BooleanField()
    menderita_kanker = models.BooleanField()
    bermasalah_jantung_paru_paru = models.BooleanField()
    menderita_pendarahan = models.BooleanField()
    seks_dengan_orang_afrika = models.BooleanField()
    tinggal_di_afrika = models.BooleanField()

    class Meta:
        constraints = [
            models.UniqueConstraint(fields=['user', 'jadwal_donor'], name='unique__user-jadwal_donor')
        ]
104
105
106

    def __str__(self):
        return str(self.user) + " - " + str(self.jadwal_donor)