Fakultas Ilmu Komputer UI

Commit 0785106f authored by addffa's avatar addffa
Browse files

[REFACTOR] refactor query dan validator

parent 9a667dff
from django.db import models
from django.core.validators import RegexValidator
numeric_only = RegexValidator(r'^[0-9]*$', 'Numeric only.')
from sizakat.validators import validate_numeric_character
class Mustahik(models.Model):
......@@ -14,17 +12,21 @@ class Mustahik(models.Model):
name = models.CharField(max_length=150)
no_ktp = models.CharField(
max_length=32, unique=True,
validators=[numeric_only]
validators=[validate_numeric_character]
)
phone = models.CharField(
max_length=32, blank=True, null=True,
validators=[numeric_only]
validators=[validate_numeric_character]
)
address = models.TextField()
province = models.CharField(max_length=32)
regency = models.CharField(max_length=50)
rt = models.CharField(max_length=4, validators=[numeric_only])
rw = models.CharField(max_length=4, validators=[numeric_only])
rt = models.CharField(
max_length=4, validators=[validate_numeric_character]
)
rw = models.CharField(
max_length=4, validators=[validate_numeric_character]
)
birthdate = models.DateField()
status = models.CharField(
max_length=32,
......
......@@ -18,18 +18,18 @@ class MustahikQuery(graphene.ObjectType):
def resolve_mustahiks(self, info, **kwargs):
statuses = kwargs.get('statuses', None)
name_contains = kwargs.get('name_contains', None)
mustahiks = Mustahik.objects.all()
filter_query = Q()
if statuses and len(statuses) > 0:
mustahiks = mustahiks.filter(reduce(
filter_query |= reduce(
lambda a, b: a | b,
list(map(lambda s: Q(status=s), statuses))
))
[Q(status=status) for status in statuses]
)
if name_contains:
mustahiks = mustahiks.filter(name__icontains=name_contains)
filter_query &= Q(name__icontains=name_contains)
return mustahiks
return Mustahik.objects.filter(filter_query)
def resolve_mustahik(self, info, id):
return Mustahik.objects.get(pk=id)
......@@ -57,7 +57,8 @@ MIDDLEWARE = [
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
CORS_ORIGIN_WHITELIST = os.environ.get('CORS_ORIGIN_WHITELIST').split()
CORS_ORIGIN_WHITELIST = os.environ.get(
'CORS_ORIGIN_WHITELIST', 'http://localhost:3000').split()
ROOT_URLCONF = 'sizakat.urls'
......
from django.core.validators import RegexValidator
validate_numeric_character = RegexValidator(
r'^[0-9]*$', 'Numeric character only.'
)
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