Fakultas Ilmu Komputer UI

Commit dd9ceaef authored by Muhammad Fairuzi Teguh's avatar Muhammad Fairuzi Teguh
Browse files

[GREEN] implement formulir daftar donor with dummy data

parent e84c9fba
Pipeline #47719 passed with stages
in 1 minute and 41 seconds
......@@ -28,4 +28,6 @@ ENV DJANGO_SETTINGS_MODULE 'dblood.settings.production'
ENV DEBUG 'False'
ENV STATIC_URL '/api/static/'
RUN ["python", "manage.py", "installtasks"]
CMD ["gunicorn", "dblood.wsgi", "--bind", "0.0.0.0:8000"]
......@@ -15,7 +15,7 @@ import os
import dj_database_url
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# Application definition
......@@ -28,6 +28,7 @@ INSTALLED_APPS = [
'django.contrib.staticfiles',
'rest_framework',
'rest_framework_authlib',
'kronos',
'main',
'stok_darah',
'anymail',
......@@ -47,6 +48,10 @@ MIDDLEWARE = [
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
CRON_CLASSES = [
'donor.crons.ClearFormulirDaftarDonor'
]
ROOT_URLCONF = 'dblood.urls'
TEMPLATES = [
......
......@@ -24,3 +24,7 @@ USE_X_FORWARDED_HOST = os.getenv('USE_X_FORWARDED_HOST', 'False') == 'True'
USE_X_FORWARDED_PORT = os.getenv('USE_X_FORWARDED_PORT', 'False') == 'True'
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
MEDIA_URL = os.getenv('MEDIA_URL', '/media/')
MEDIA_ROOT = os.getenv('MEDIA_ROOT', os.path.join(BASE_DIR, 'media'))
......@@ -13,8 +13,10 @@ Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path, include
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
......@@ -22,4 +24,4 @@ urlpatterns = [
path('donor/', include('donor.urls')),
path('acara-donor/', include('acara_donor.urls')),
path('stok-darah/', include('stok_darah.urls'))
]
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
FROM python:3.8-buster
RUN apt-get update -q && \
apt-get install -y libpq-dev python3-dev
apt-get install -y libpq-dev python3-dev cron
WORKDIR /app
COPY requirements.txt /app/
......@@ -16,5 +16,6 @@ ENV DATABASE_URL 'sqlite:///db.sqlite3'
ENV DJANGO_SETTINGS_MODULE 'dblood.settings.staging'
ENV STATIC_URL '/staging/api/static/'
RUN ["python3", "manage.py", "collectstatic"]
RUN ["python", "manage.py", "installtasks"]
CMD ["gunicorn", "dblood.wsgi", "--bind", "0.0.0.0:8000"]
import glob
import os
from datetime import datetime
import kronos
from django.conf import settings
from django.core.management import BaseCommand
@kronos.register('0 0 * * *')
class Command(BaseCommand):
help = 'Clear unused formulir daftar donor'
def handle(self, *args, **options):
formulir_folder = 'formulir-daftar-donor'
all_files = glob.glob(os.path.join(settings.MEDIA_ROOT, formulir_folder, "*.pdf"))
now = datetime.now()
cnt_removed = 0
for file in all_files:
statbuf = os.stat(file)
modified_time = datetime.fromtimestamp(statbuf.st_mtime)
elapsed_time = now - modified_time
if elapsed_time.days > 1:
os.remove(file)
cnt_removed += 1
self.stdout.write(self.style.SUCCESS(
"Successfully clear {:d} unused formulir daftar donor".format(cnt_removed)))
from django.core.exceptions import ValidationError
from django.core.validators import MinValueValidator, MaxValueValidator
from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models
from django.utils.timezone import localtime
......
import os
from django.conf import settings
from datetime import datetime
from PIL import Image, ImageDraw, ImageFont
class FormulirDaftarDonorService:
def generate_formulir(self, daftar_donor):
resource_path = os.path.join(settings.BASE_DIR, "donor", "resources")
template_path = os.path.join(resource_path, "form-a4.png")
formulir = Image.open(template_path).convert("RGB")
draw = ImageDraw.Draw(formulir)
font_path = os.path.join(resource_path, "DejaVuSans.ttf")
unicode_font = ImageFont.truetype(font_path, 24)
# dummy for now
draw.text(xy=(482, 180), text="✔", fill=(0, 0, 0), font=unicode_font)
draw.text(xy=(516, 195), text="✔", fill=(0, 0, 0), font=unicode_font)
draw.text(xy=(482, 210), text="✔", fill=(0, 0, 0), font=unicode_font)
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)
formulir.save(formulir_path)
return settings.MEDIA_URL + formulir_name
formulir_daftar_donor_service = FormulirDaftarDonorService()
......@@ -284,9 +284,9 @@ class FormulirDaftarDonorTest(APITestCase):
def test_get_formulir_authenticated(self):
self.client.credentials()
response = self.client.get('/donor/formulir-daftar/' + self.daftar_donor.id)
response = self.client.get('/donor/formulir-daftar/' + str(self.daftar_donor.id))
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
def test_get_formulir_return_url(self):
response = self.client.get('/donor/formulir-daftar/' + self.daftar_donor.id)
response = self.client.get('/donor/formulir-daftar/' + str(self.daftar_donor.id))
self.assertIn('http', response.data['url'])
from django.urls import path
from donor.views import JadwalDonorView, DaftarDonorView, AgendaDonorView, RiwayatDonorView
from donor.views import JadwalDonorView, DaftarDonorView, AgendaDonorView, RiwayatDonorView, FormulirDaftarDonorView
urlpatterns = [
path('jadwal/', JadwalDonorView.as_view()),
path('jadwal/<int:pk>/daftar/', DaftarDonorView.as_view()),
path('jadwal/agenda/', AgendaDonorView.as_view()),
path('jadwal/riwayat/', RiwayatDonorView.as_view())
path('jadwal/riwayat/', RiwayatDonorView.as_view()),
path('formulir-daftar/<int:pk>', FormulirDaftarDonorView.as_view())
]
from django.shortcuts import get_object_or_404
from django.utils import timezone
from django.utils.timezone import now
from rest_framework import generics
from rest_framework import generics, views
from rest_framework.exceptions import ValidationError
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from donor.models import DaftarDonor, JadwalDonor
from donor.serializers import DaftarDonorSerializer, JadwalDonorSerializer
from main.paginations import ExtraSmallResultsSetPagination
from donor.services import formulir_daftar_donor_service
class JadwalDonorView(generics.ListAPIView):
......@@ -59,5 +61,12 @@ class RiwayatDonorView(generics.ListAPIView):
return JadwalDonor.objects.filter(daftar_donors__in=daftar_donors).order_by('-time_start')
class FormulirDaftarDonorView(generics.RetrieveAPIView):
pass
class FormulirDaftarDonorView(views.APIView):
permission_classes = [IsAuthenticated]
def get(self, request, pk):
daftar_donor = get_object_or_404(DaftarDonor, user=request.user, jadwal_donor__pk=pk)
file_path = formulir_daftar_donor_service.generate_formulir(daftar_donor)
file_url = request.build_absolute_uri(file_path)
return Response({"url": file_url})
......@@ -7,3 +7,5 @@ django-anymail==7.0.0
djangorestframework==3.11.0
django-cors-headers==3.2.1
gunicorn==20.0.4
Pillow==7.1.2
django-kronos==1.0
\ No newline at end of file
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