Fakultas Ilmu Komputer UI

Commit 78a52a2e authored by Muhammad Fairuzi Teguh's avatar Muhammad Fairuzi Teguh
Browse files

Merge branch 'staging' of...

Merge branch 'staging' of https://gitlab.cs.ui.ac.id/ppl-fasilkom-ui/2020/ppl-c/diskominfo-d-blood/mantan-aab-d-blood into warmfix-iruz
parents ea02b068 654cbdc6
Pipeline #50007 passed with stages
in 1 minute and 51 seconds
from django.http import HttpResponse
from django.contrib import admin
from django.conf import settings
from django.utils.timezone import localtime
from django.utils.html import mark_safe
from acara_donor.models import AcaraDonor
from donor.models import JadwalDonor
from acara_donor.filters import AcaraDonorFilter
def download_acara_donor(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'
df = read_frame(queryset)
df['waktu_donor'] = df['waktu_donor'].astype(str).str[:-6] # Strip tzinfo
writer = pd.ExcelWriter(response, engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1')
workbook = writer.book
# worksheet = writer.sheets['Sheet1']
workbook.close()
return response
download_acara_donor.short_description = 'Download Acara Donor as Excel Workbook'
@admin.register(AcaraDonor)
class AcaraDonorAdmin(admin.ModelAdmin):
list_display = ('nama_institusi', 'alamat_lokasi_donor',
'display_waktu', 'status')
list_filter = ('status', AcaraDonorFilter, 'kecamatan', 'kategori')
search_fields = ('waktu_mulai',)
search_fields = ('waktu_mulai', 'alamat_lokasi_donor')
readonly_fields = ('id',
'user',
'nama_institusi',
......@@ -43,4 +24,59 @@ class AcaraDonorAdmin(admin.ModelAdmin):
'email_koor',
'no_telp_koor',
'keterangan',
'foto_lokasi')
'foto_lokasi',
'show_image')
fieldsets = (
(None, {
'fields': ('id',
'user',
'status')
}),
('INFORMASI ACARA DONOR', {
'fields': ('kategori',
'alamat_lokasi_donor',
'kecamatan',
('waktu_mulai', 'waktu_berakhir'),
'perkiraan_jumlah_donor',
'keterangan',
'show_image')
}),
('INFORMASI PENYELENGGARA', {
'fields': ('nama_institusi', 'alamat_institusi',
'email_kantor', 'no_telp_kantor')
}),
('KONTAK KOORDINATOR', {
'fields': ('nama_koor', 'email_koor', 'no_telp_koor')
}),
)
def display_waktu(self, obj):
"""Create a string for waktu acara donor."""
waktu_mulai = localtime(obj.waktu_mulai)
waktu_berakhir = localtime(obj.waktu_berakhir)
return f"{waktu_mulai.date().strftime('%d/%m/%Y')} \
({waktu_mulai.hour:02d}:{waktu_mulai.minute:02d} \
- {waktu_berakhir.hour:02d}:{waktu_berakhir.minute:02d})"
def show_image(self, obj):
"""Create an image preview for foto lokasi acara donor."""
return mark_safe('<img src="%s" style="width=auto; height=auto; \
max-height: 1000px; max-width: 1000px;" />'
% (obj.foto_lokasi.url))
display_waktu.short_description = 'Waktu'
show_image.short_description = 'Preview foto lokasi'
class Media:
js = [settings.MEDIA_URL+'/js/acara_donor_custom_admin.js', ]
def save_model(self, request, obj, form, change):
if(obj.status):
JadwalDonor.objects.create(kecamatan=obj.kecamatan,
location=obj.alamat_lokasi_donor,
time_start=obj.waktu_mulai,
time_end=obj.waktu_berakhir,
quota=obj.perkiraan_jumlah_donor,
category=obj.kategori)
super().save_model(request, obj, form, change)
......@@ -26,17 +26,15 @@ class AcaraDonorFactory(factory.DjangoModelFactory):
email_koor = factory.Faker('free_email', locale=LOCALE)
no_telp_koor = factory.Sequence(lambda n: '021-1128-%04d' % n)
kategori = factory.LazyAttribute(lambda _:
choice(JadwalDonor.Category.choices)[0])
kategori = factory.LazyAttribute(
lambda _: choice(JadwalDonor.Category.choices)[0]) # NOSONAR
alamat_lokasi_donor = factory.Faker('address', locale=LOCALE)
kecamatan = factory.LazyAttribute(lambda _:
choice(JadwalDonor.Kecamatan.choices)[0])
waktu_mulai = factory.LazyAttribute(lambda _: timezone.now()
+ timedelta(
days=randint(1, 66),
hours=randint(1, 23))) # NOSONAR
waktu_berakhir = factory.LazyAttribute(lambda t: t.waktu_mulai
+ timedelta(
hours=randint(2, 6))) # NOSONAR
kecamatan = factory.LazyAttribute(
lambda _: choice(JadwalDonor.Kecamatan.choices)[0]) # NOSONAR
waktu_mulai = factory.LazyAttribute(
lambda _: timezone.now() + timedelta(days=randint(1, 66), # NOSONAR
hours=randint(1, 23))) # NOSONAR
waktu_berakhir = factory.LazyAttribute(
lambda t: t.waktu_mulai + timedelta(hours=randint(2, 6))) # NOSONAR
perkiraan_jumlah_donor = factory.fuzzy.FuzzyInteger(low=33, high=666)
keterangan = factory.Faker('text')
# Generated by Django 3.0.5 on 2020-06-02 13:55
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('acara_donor', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='acaradonor',
name='status',
field=models.BooleanField(default=None, null=True),
),
]
......@@ -7,7 +7,7 @@ from donor.models import JadwalDonor
class AcaraDonor(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
status = models.BooleanField(null=True)
status = models.BooleanField(null=True, default=None)
nama_institusi = models.CharField(max_length=95)
alamat_institusi = models.CharField(max_length=140)
......@@ -18,10 +18,10 @@ class AcaraDonor(models.Model):
email_koor = models.EmailField()
no_telp_koor = models.CharField(max_length=20)
kategori = models.CharField(max_length=20,
kategori = models.CharField(max_length=20, blank=False, null=False,
choices=JadwalDonor.Category.choices)
alamat_lokasi_donor = models.CharField(max_length=140)
kecamatan = models.CharField(max_length=20,
kecamatan = models.CharField(max_length=20, blank=False, null=False,
choices=JadwalDonor.Kecamatan.choices)
waktu_mulai = models.DateTimeField()
waktu_berakhir = models.DateTimeField()
......
......@@ -26,4 +26,5 @@ class AcaraDonorSerializer(serializers.ModelSerializer):
class Meta:
model = AcaraDonor
exclude = ["user", ]
extra_kwargs = {'foto_lokasi': {'required': False}}
extra_kwargs = {'foto_lokasi': {'required': False},
'status': {'default': None}}
import datetime
from unittest.mock import patch
from django.contrib.auth import get_user_model
from django.test import TestCase
from django.utils import timezone
from django.utils.timezone import localtime
......@@ -14,9 +13,6 @@ from acara_donor.admin import AcaraDonorAdmin
from acara_donor.filters import AcaraDonorFilter
User = get_user_model()
class AcaraDonorTest(TestCase):
""" Test module for Acara Donor model"""
......
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 donor.models import DaftarDonor, JadwalDonor
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
def download_jadwaldonor(modeladmin, request, queryset):
......@@ -68,8 +68,12 @@ download_jadwaldonor.short_description = 'Download Jadwal Donor as Excel Workboo
@admin.register(JadwalDonor)
class JadwalDonorAdmin(admin.ModelAdmin):
list_filter = ('kecamatan', 'time_start', 'category')
search_fields = ('time_start', 'location')
actions = [download_jadwaldonor]
class Media:
js = [settings.MEDIA_URL+'/js/acara_donor_custom_admin.js', ]
class JadwalDonorFilter(InputFilter):
parameter_name = 'lokasi_donor'
......@@ -85,7 +89,61 @@ class JadwalDonorFilter(InputFilter):
@admin.register(DaftarDonor)
class DaftarDonorAdmin(admin.ModelAdmin):
list_filter = ('has_attended', JadwalDonorFilter, 'jadwal_donor__time_start')
list_display = ('user', 'has_attended', 'get_goldar', 'jadwal_donor')
search_fields = ('user__email',
'jadwal_donor__location', 'jadwal_donor__time_start')
fields = ('has_attended',
'id',
'user',
'jadwal_donor',
'merasa_sehat',
'minum_antibiotik',
'minum_obat_infeksi',
'minum_aspirin',
'sakit_kepala_dan_demam',
'sedang_hamil',
'kehamilan_berapa',
'donor_darah_trombosit_plasma',
'menerima_vaksinasi',
'kontak_vaksinasi',
'donor_aferesis',
'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',
'pernah_di_luar_indonesia',
'pernah_jualan_seks',
'pernah_homoseks',
'pernah_tinggal_di_eropa',
'pernah_terima_transfusi_di_inggris',
'pernah_tinggal_di_inggris',
'positif_aids',
'pakai_jarum_suntik',
'pakai_konsentrat',
'menderita_hepatitis',
'menderita_malaria',
'menderita_kanker',
'bermasalah_jantung_paru_paru',
'menderita_pendarahan',
'seks_dengan_orang_afrika',
'tinggal_di_afrika')
list_filter = ('has_attended', JadwalDonorFilter, 'jadwal_donor__time_start', 'user__profile__blood_type')
def get_goldar(self, obj):
return obj.user.profile.blood_type
get_goldar.short_description = 'Goldar'
get_goldar.admin_order_field = 'user__profile__blood_type'
def get_readonly_fields(self, request, obj=None):
readonly_fields = list(set(
......@@ -95,3 +153,6 @@ class DaftarDonorAdmin(admin.ModelAdmin):
if 'has_attended' in readonly_fields:
readonly_fields.remove('has_attended')
return readonly_fields
class Media:
js = [settings.MEDIA_URL+'/js/daftar_donor_custom_admin.js', ]
# Generated by Django 3.0.5 on 2020-06-04 05:36
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('donor', '0011_auto_20200602_0227'),
]
operations = [
migrations.AlterField(
model_name='jadwaldonor',
name='location',
field=models.CharField(max_length=140),
),
]
......@@ -26,7 +26,7 @@ class JadwalDonor(models.Model):
PRIVATE = "Tertutup"
kecamatan = models.CharField(max_length=20, choices=Kecamatan.choices)
location = models.CharField(max_length=30)
location = models.CharField(max_length=140)
time_start = models.DateTimeField()
time_end = models.DateTimeField()
quota = models.PositiveIntegerField()
......
from django.contrib import admin
from django.conf import settings
from edukasi.models import Artikel
@admin.register(Artikel)
class ArtikelAdmin(admin.ModelAdmin):
pass
list_display = ('title', 'posted_at')
search_fields = ('posted_at', 'title')
fields = ('posted_at', 'title', 'featured_image', 'content')
readonly_fields = ('posted_at',)
class Media:
js = [settings.MEDIA_URL+'/js/artikel_custom_admin.js', ]
......@@ -12,6 +12,6 @@ class ArtikelFactory(factory.DjangoModelFactory):
class Meta:
model = Artikel
title = fake.sentence()[:-1][:30]
title = fake.sentence()[:-1][:58]
featured_image = os.path.join("edukasi", "placeholder.jpg")
content = "\n".join(fake.paragraphs())
......@@ -14,9 +14,9 @@ class Command(BaseCommand):
help = 'Add dummy data artikel edukasi'
def handle(self, *args, **options):
for i in range(20):
for _ in range(20):
content = []
for i in range(5):
for _ in range(5):
content.append(" ".join(fake.paragraphs()))
Artikel.objects.create(
title=fake.sentence()[:-1][:30],
......
# Generated by Django 3.0.5 on 2020-06-03 13:16
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('edukasi', '0002_artikel_posted_at'),
]
operations = [
migrations.AlterField(
model_name='artikel',
name='title',
field=models.CharField(max_length=25),
),
]
# Generated by Django 3.0.5 on 2020-06-03 13:20
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('edukasi', '0003_auto_20200603_2016'),
]
operations = [
migrations.AlterField(
model_name='artikel',
name='title',
field=models.CharField(max_length=30),
),
]
# Generated by Django 3.0.5 on 2020-06-03 14:18
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('edukasi', '0004_auto_20200603_2020'),
]
operations = [
migrations.AlterField(
model_name='artikel',
name='title',
field=models.CharField(max_length=60),
),
]
......@@ -2,7 +2,7 @@ from django.db import models
class Artikel(models.Model):
title = models.CharField(max_length=32)
title = models.CharField(max_length=60)
featured_image = models.ImageField()
content = models.TextField()
posted_at = models.DateTimeField(auto_now_add=True)
......
......@@ -6,4 +6,4 @@ from .models import Artikel
class ArtikelSerializer(serializers.ModelSerializer):
class Meta:
model = Artikel
fields = ['id', 'title', 'featured_image', 'content']
fields = ['id', 'title', 'featured_image', 'content', 'posted_at']
from django.contrib import admin
from django.conf import settings
from django.contrib.auth.admin import UserAdmin as DjangoUserAdmin
from django.utils.translation import ugettext_lazy as _
......@@ -9,10 +10,17 @@ from .models import User, Profile
class UserAdmin(DjangoUserAdmin):
"""Define admin model for custom User model with no username field."""
readonly_fields = ('email',
'first_name',
'last_name',
'last_login',
'date_joined')
fieldsets = (
(None, {'fields': ('email', 'password')}),
(_('Personal info'), {'fields': ('first_name', 'last_name')}),
(_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser', 'is_verified',
(_('Permissions'), {'fields': ('is_active', 'is_staff',
'is_superuser', 'is_verified',
'groups', 'user_permissions')}),
(_('Important dates'), {'fields': ('last_login', 'date_joined')}),
)
......@@ -26,12 +34,8 @@ class UserAdmin(DjangoUserAdmin):
search_fields = ('email', 'first_name', 'last_name')
ordering = ('email',)
@admin.register(Profile)
class ProfileAdmin(admin.ModelAdmin):
pass
# common/admin.py
class Media:
js = [settings.MEDIA_URL+'/js/user_custom_admin.js', ]
class InputFilter(admin.SimpleListFilter):
......@@ -50,3 +54,59 @@ class InputFilter(admin.SimpleListFilter):
if k != self.parameter_name
)
yield all_choice
@admin.register(Profile)
class ProfileAdmin(admin.ModelAdmin):
list_display = ('user', 'district', 'address', 'age', 'sex', 'blood_type')
list_filter = ('blood_type', 'sex')
search_fields = ('address', 'district')
readonly_fields = ('user',
'body_weight',
'id_card_no',
'birthplace',
'birthdate',
'sex',
'profession',
'blood_type',
'married_status',
'address',
'city',
'district',
'village',
'phone_no',
'work_address',
'work_email',
'work_phone_no',
'age')
fieldsets = (
(None, {'fields': ('user',)}),
('INFORMASI PRIBADI', {
'fields': ('id_card_no',
'birthplace',
'birthdate',
'age',
'body_weight',
'sex',
'profession',
'blood_type',
'married_status')
}),
('ALAMAT', {
'fields': ('address',
'village',
'district',
'city',
'work_address')
}),
('KONTAK', {
'fields': ('phone_no',
'work_email',
'work_phone_no')
}),
)
class Media:
js = [settings.MEDIA_URL+'/js/profile_custom_admin.js', ]
......@@ -98,7 +98,7 @@ class Profile(models.Model):
return today.year - born.year - ((today.month, today.day) < (born.month, born.day))
def __str__(self):
return f'({self.sex}, {self.blood_type})'
return f'{self.user} ({self.sex}, {self.blood_type})'
class Meta:
pass
......@@ -105,7 +105,7 @@ class ProfileTests(TestCase):
profile__sex=Sex.FEMALE,
profile__blood_type='O+')
self.assertEqual(str(user.profile), '(F, O+)')
self.assertEqual(str(user.profile), f'{str(user)} (F, O+)')
def test_dont_save_new_profile_on_user_update(self):
user = UserFactory(last_name='Musthofa')
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment