Fakultas Ilmu Komputer UI

Commit 6123747a authored by addffa's avatar addffa
Browse files

[GREEN] implementasi upload photo

parent 6d9d0753
from django import forms
from .models import Mustahik, DataSource, DataSourceWarga, DataSourceInstitusi, DataSourcePekerja
from .models import Mustahik, DataSource, DataSourceWarga, DataSourceInstitusi, DataSourcePekerja
class MustahikForm(forms.ModelForm):
......@@ -15,8 +15,10 @@ class MustahikForm(forms.ModelForm):
'status',
'gender',
'data_source',
'photo',
]
class DataSourceForm(forms.ModelForm):
class Meta:
model = DataSource
......@@ -24,6 +26,7 @@ class DataSourceForm(forms.ModelForm):
'category',
]
class DataSourceWargaForm(forms.ModelForm):
class Meta:
model = DataSourceWarga
......@@ -41,6 +44,7 @@ class DataSourceWargaForm(forms.ModelForm):
'data_source',
]
class DataSourceInstitusiForm(forms.ModelForm):
class Meta:
model = DataSourceInstitusi
......@@ -59,6 +63,7 @@ class DataSourceInstitusiForm(forms.ModelForm):
'data_source',
]
class DataSourcePekerjaForm(forms.ModelForm):
class Meta:
model = DataSourcePekerja
......@@ -70,4 +75,4 @@ class DataSourcePekerjaForm(forms.ModelForm):
'profession',
'location',
'data_source',
]
\ No newline at end of file
]
import graphene
from graphene_django.forms.mutation import DjangoModelFormMutation
from graphene_django.types import ErrorType
from sizakat.validators import validate_photo
from .forms import (
MustahikForm, DataSourceForm, DataSourceWargaForm,
......@@ -19,6 +21,23 @@ class MustahikMutation(DjangoModelFormMutation):
class Meta:
form_class = MustahikForm
@classmethod
def mutate_and_get_payload(cls, root, info, **input):
form = cls.get_form(root, info, **input)
photo = info.context.FILES['photo']
if not validate_photo(photo):
form.add_error('photo', 'invalid photo format')
if form.is_valid():
mustahik = form.save(commit=False)
mustahik.photo = photo
mustahik.save()
kwargs = {cls._meta.return_field_name: mustahik}
return cls(errors=[], **kwargs)
else:
errors = ErrorType.from_errors(form.errors)
return cls(errors=errors)
class DeleteMustahik(graphene.Mutation):
class Arguments:
......
......@@ -3,3 +3,9 @@ from django.core.validators import RegexValidator
validate_numeric_character = RegexValidator(
r'^[0-9]*$', 'Numeric character only.'
)
def validate_photo(photo):
valid_extensions = ['jpg', 'png']
photo_extension = photo.name.split('.')[-1]
return photo_extension in valid_extensions
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