Fakultas Ilmu Komputer UI

Commit c0e88397 authored by addffa's avatar addffa
Browse files

[GREEN] implementasi query data source model

parent 550096b9
Pipeline #51195 passed with stage
in 11 minutes and 20 seconds
......@@ -3,8 +3,8 @@ import graphene
from django.db.models import Q
from functools import reduce
from .models import Mustahik
from .types import MustahikType
from .models import Mustahik, DataSource
from .types import MustahikType, DataSourceType
class MustahikQuery(graphene.ObjectType):
......@@ -14,6 +14,8 @@ class MustahikQuery(graphene.ObjectType):
name_contains=graphene.String()
)
mustahik = graphene.Field(MustahikType, id=graphene.ID(required=True))
data_sources = graphene.List(DataSourceType, category=graphene.String())
data_source = graphene.Field(DataSourceType, id=graphene.ID(required=True))
def resolve_mustahiks(self, info, **kwargs):
statuses = kwargs.get('statuses', None)
......@@ -33,3 +35,14 @@ class MustahikQuery(graphene.ObjectType):
def resolve_mustahik(self, info, id):
return Mustahik.objects.get(pk=id)
def resolve_data_sources(self, info, **kwargs):
category = kwargs.get('category')
filter_query = Q()
if category:
filter_query &= Q(category=category)
return DataSource.objects.filter(filter_query)
def resolve_data_source(self, info, id):
return DataSource.objects.get(pk=id)
import graphene
from graphene_django.types import DjangoObjectType
from .models import Mustahik
from .models import (
Mustahik, DataSource, DataSourceInstitusi,
DataSourcePekerja, DataSourceWarga
)
class MustahikType(DjangoObjectType):
......@@ -9,3 +12,45 @@ class MustahikType(DjangoObjectType):
model = Mustahik
age = graphene.Int(source='calculate_age')
class DataSourceInstitusiType(DjangoObjectType):
class Meta:
model = DataSourceInstitusi
class DataSourcePekerjaType(DjangoObjectType):
class Meta:
model = DataSourcePekerja
class DataSourceWargaType(DjangoObjectType):
class Meta:
model = DataSourceWarga
class DataSourceDetailType(graphene.Union):
class Meta:
types = (
DataSourceInstitusiType, DataSourcePekerjaType,
DataSourceWargaType
)
my_attr = True
class DataSourceType(DjangoObjectType):
class Meta:
model = DataSource
exclude = (
'datasourceinstitusi', 'datasourcepekerja', 'datasourcewarga'
)
data_source_detail = graphene.Field(DataSourceDetailType)
def resolve_data_source_detail(self, info):
if self.category == DataSource.Category.INSTITUSI:
return DataSourceInstitusi.objects.get(data_source__pk=self.pk)
if self.category == DataSource.Category.PEKERJA:
return DataSourcePekerja.objects.get(data_source__pk=self.pk)
if self.category == DataSource.Category.WARGA:
return DataSourceWarga.objects.get(data_source__pk=self.pk)
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