Fakultas Ilmu Komputer UI

models.py 4.09 KB
Newer Older
1
2
import os

addffa's avatar
addffa committed
3
from django.db import models
4
from django.utils import timezone
addffa's avatar
addffa committed
5

6
from sizakat.validators import validate_numeric_character
addffa's avatar
addffa committed
7

addffa's avatar
addffa committed
8

9
class Mustahik(models.Model):
addffa's avatar
addffa committed
10
    class Status(models.TextChoices):
11
        FAKIR = ('FAKIR', 'Fakir')
addffa's avatar
addffa committed
12
        MISKIN = ('MISKIN', 'Miskin')
13
14
15
16
17
        AMIL = ('AMIL', 'Amil')
        MUALAF = ('MUALAF', 'Mualaf')
        GHARIM = ('GHARIM', 'Gharim')
        FISABILILLAH = ('FISABILILLAH', 'Fisabilillah')
        MUSAFIR = ('MUSAFIR', 'Musafir')
addffa's avatar
addffa committed
18

19
20
21
22
    class Gender(models.TextChoices):
        LAKILAKI = ('L', 'Laki-Laki')
        PEREMPUAN = ('P', 'Perempuan')

23
24
25
    name = models.CharField(max_length=150)
    no_ktp = models.CharField(
        max_length=32, unique=True,
26
        validators=[validate_numeric_character]
27
28
29
    )
    phone = models.CharField(
        max_length=32, blank=True, null=True,
30
        validators=[validate_numeric_character]
31
    )
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
    address = models.CharField(max_length=255)
    birthdate = models.DateField()
    status = models.CharField(max_length=32, choices=Status.choices)
    gender = models.CharField(max_length=1, choices=Gender.choices)
    photo = models.FileField(
        upload_to=os.path.join('images', 'mustahik'),
        default=os.path.join('images', 'default_photo.jpg')
    )
    data_source = models.ForeignKey('DataSource', on_delete=models.CASCADE)

    def calculate_age(self):
        return timezone.now().year - self.birthdate.year


class DataSource(models.Model):
    class Category(models.TextChoices):
        WARGA = ('WARGA', 'Warga')
        INSTITUSI = ('INSTITUSI', 'Institusi')
        PEKERJA = ('PEKERJA', 'Pekerja')

52
53
    category = models.CharField(max_length=32, choices=Category.choices)

54
55
56
57
58
59
60
61
    def get_source_detail(self):
        if self.category == DataSource.Category.INSTITUSI:
            return DataSourceInstitusi.objects.get(data_source=self)
        if self.category == DataSource.Category.PEKERJA:
            return DataSourcePekerja.objects.get(data_source=self)
        if self.category == DataSource.Category.WARGA:
            return DataSourceWarga.objects.get(data_source=self)

62
63
64
65
66

class DataSourceDetail(models.Model):
    class Meta:
        abstract = True

67
68
69
70
71
72
73
74
75
76
77
78
    pic_name = models.CharField(max_length=150)
    pic_ktp = models.CharField(
        max_length=32,
        validators=[validate_numeric_character]
    )
    pic_phone = models.CharField(
        max_length=32,
        validators=[validate_numeric_character]
    )
    pic_position = models.CharField(max_length=50)


79
class DataSourceWarga(DataSourceDetail):
80
    province = models.CharField(max_length=50)
81
    regency = models.CharField(max_length=50)
82
83
    sub_district = models.CharField(max_length=50)
    village = models.CharField(max_length=50)
84
    rt = models.CharField(
85
        max_length=3, validators=[validate_numeric_character]
86
87
    )
    rw = models.CharField(
88
        max_length=3, validators=[validate_numeric_character]
89
    )
90
91
92
    data_source = models.OneToOneField(
        'DataSource', on_delete=models.CASCADE,
        limit_choices_to={'category': DataSource.Category.WARGA}
addffa's avatar
addffa committed
93
    )
94

95

96
class DataSourceInstitusi(DataSourceDetail):
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
    name = models.CharField(max_length=150)
    province = models.CharField(max_length=50)
    regency = models.CharField(max_length=50)
    sub_district = models.CharField(max_length=50)
    village = models.CharField(max_length=50)
    rt = models.CharField(
        max_length=3, validators=[validate_numeric_character]
    )
    rw = models.CharField(
        max_length=3, validators=[validate_numeric_character]
    )
    address = models.CharField(max_length=255)
    data_source = models.OneToOneField(
        'DataSource', on_delete=models.CASCADE,
        limit_choices_to={'category': DataSource.Category.INSTITUSI}
    )


115
class DataSourcePekerja(DataSourceDetail):
116
117
118
119
120
121
    profession = models.CharField(max_length=50)
    location = models.CharField(max_length=50)
    data_source = models.OneToOneField(
        'DataSource', on_delete=models.CASCADE,
        limit_choices_to={'category': DataSource.Category.PEKERJA}
    )