Fakultas Ilmu Komputer UI

models.py 6.11 KB
Newer Older
Ryan Karyadiputera's avatar
test  
Ryan Karyadiputera committed
1
2
3
4
5
6
7
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
from django.conf import settings
from django.db import models
from django.dispatch import receiver
from django_cas_ng.signals import cas_user_authenticated
from django.utils import timezone
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, BaseUserManager, UserManager, User
from django.contrib.auth.validators import UnicodeUsernameValidator
from .role import Role
from django.http import HttpResponseRedirect

from lembaga.models import Lembaga

import json
import random

LANG = settings.SSO_UI_ORG_DETAIL_LANG
ORG_CODE = {}
with open(settings.SSO_UI_ORG_DETAIL_FILE_PATH, 'r') as ORG_CODE_FILE:
    ORG_CODE.update(json.load(ORG_CODE_FILE))


class SupervisorSekolah(models.Model):
    """User Profile Supervisor Sekolah model."""

    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='supervisor_sekolah')
    nip = models.CharField('nip', max_length=12, blank=True)

    class Meta:
        verbose_name = 'supervisor_sekolah'
        verbose_name_plural = 'supervisor_sekolah'

    def __str__(self):
        """Return username of the user."""
        return self.user.username


class SupervisorLembaga(models.Model):
    """User Profile Supervisor Lembaga model."""

    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='supervisor_lembaga')
    lembaga = models.ForeignKey(Lembaga, related_name="supervisor_lembaga", \
                                on_delete=models.CASCADE, default="Tidak ada lembaga")
    jabatan = models.CharField('jabatan', max_length=150, blank=False)

    class Meta:
        verbose_name = 'supervisor_lembaga'
        verbose_name_plural = 'supervisor_lembaga'

    def __str__(self):
        """Return username of the user."""
        return self.user.username


class Administrator(models.Model):
    """User Profile Supervisor Sekolah model."""

    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='administrator')
    nip = models.CharField('nip', max_length=12, blank=False)

    class Meta:
        verbose_name = 'administrator'
        verbose_name_plural = 'administrator'

    def __str__(self):
        """Return username of the user."""
        return self.user.username


class Periode(models.Model):
    """Periode model for Mahasiswa model."""

    nama = models.CharField('nama', max_length=50, blank=False)

    class Meta:
        verbose_name = 'periode'
        verbose_name_plural = 'periode'

    def __str__(self):
        """Return periode's name of the periode."""
        return self.nama


class Mahasiswa(models.Model):
    """User Profile Mahasiswa model."""

    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='mahasiswa')
    org_code = models.CharField('organization_code', max_length=11, blank=True)
Ryan Karyadiputera's avatar
test2    
Ryan Karyadiputera committed
88
    npm = models.CharField('npm', max_length=10, blank=True)
Ryan Karyadiputera's avatar
test  
Ryan Karyadiputera committed
89
90
91
92
93
94
95
    faculty = models.CharField('faculty', max_length=128, blank=True)
    major = models.CharField('major', max_length=128, blank=True)
    program = models.CharField('program', max_length=128, blank=True)
    periode = models.ForeignKey(Periode, related_name="periode", on_delete=models.CASCADE, null=True, blank=True)
    supervisor_sekolah = models.ForeignKey(SupervisorSekolah, related_name='mahasiswa', \
                                           to_field='user', on_delete=models.CASCADE, null=True, blank=True)
    supervisor_lembaga = models.ForeignKey(SupervisorLembaga, related_name='mahasiswa',\
Ryan Karyadiputera's avatar
test2    
Ryan Karyadiputera committed
96
                                           to_field='user', on_delete=models.CASCADE, null=True, blank=True)
Ryan Karyadiputera's avatar
test  
Ryan Karyadiputera committed
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
142
143
144
145
146
147
148
149
150
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

    class Meta:
        verbose_name = 'mahasiswa'
        verbose_name_plural = 'mahasiswa'

    def __str__(self):
        """Return username of the user."""
        return self.user.username


class KoordinatorKuliah(models.Model):
    """User Profile Kordinator Kuliah model."""

    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='koordinator_kuliah')
    nip = models.CharField('nip', max_length=12, blank=False)

    class Meta:
        verbose_name = 'koordinator_kuliah'
        verbose_name_plural = 'koordinator_kuliah'

    def __str__(self):
        """Return username of the user."""
        return self.user.username


class Config(models.Model):
    """User Profile Kordinator Kuliah model."""

    key = models.CharField('key', max_length=200, blank=False)
    value = models.CharField('key', max_length=200)

    class Meta:
        verbose_name = 'config'
        verbose_name_plural = 'configs'

    def __str__(self):
        """Return username of the user."""
        return self.key


# @receiver(cas_user_authenticated)
# def save_user_attributes(username, attributes, **kwargs):
#     """Save user attributes from CAS into user and profile objects."""

#     try:
#         user = User.objects.get(email=f'{username}@ui.ac.id')

#         user.email = f'{username}@ui.ac.id'
#         user.username = username
#         user.full_name = attributes['nama']
#         user.save()

#         if user.role == Role.MHS.value:
#             org_code = attributes['kd_org']
#             record = ORG_CODE[LANG][org_code]

#             mahasiswa = Mahasiswa.objects.get(user=user)
#             mahasiswa.npm = attributes['npm']
#             mahasiswa.org_code = org_code
#             mahasiswa.faculty = record['faculty']
#             mahasiswa.major = record['study_program']
#             mahasiswa.program = record['educational_program']
#             mahasiswa.save()

#         elif user.role == Role.SSK.value:
#             supervisor_sekolah = SupervisorSekolah.objects.get(user=user)
#             supervisor_sekolah.nip = attributes['nip']
#             supervisor_sekolah.save()

#         elif user.role == Role.KKL.value:
#             koordinator_kuliah = KoordinatorKuliah.objects.get(user=user)
#             koordinator_kuliah.nip = attributes['nip']
#             koordinator_kuliah.save()

#         elif user.role == Role.ADM.value:
#             administrator = Administrator.objects.get(user=user)
#             administrator.nip = attributes['nip']
#             administrator.save()

#     except User.DoesNotExist:
#         return HttpResponseRedirect(settings.SUCCESS_SSO_AUTH_REDIRECT + 'not-login/')