Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add non required fields filter and CustomDateTime #207

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 45 additions & 16 deletions graphene_django_extras/base_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@

import binascii
import datetime
from ast import literal_eval

import graphene
from graphene.types.datetime import Date, DateTime, Time
from graphene.types.datetime import Date, Time, DateTime
from graphene.utils.str_converters import to_camel_case
from graphql.language import ast

import logging

def factory_type(operation, _type, *args, **kwargs):
if operation == "output":
Expand All @@ -30,6 +31,7 @@ class Meta:
description = "Auto generated Type for {} model".format(
kwargs.get("model").__name__
)
non_required_fields=kwargs.get("non_required_fields")

return GenericType

Expand All @@ -39,7 +41,8 @@ class GenericInputType(_type):
class Meta:
model = kwargs.get("model")
name = kwargs.get("name") or to_camel_case(
"{}_{}_Generic_Type".format(kwargs.get("model").__name__, args[0])
"{}_{}_Generic_Type".format(
kwargs.get("model").__name__, args[0])
)
only_fields = kwargs.get("only_fields")
exclude_fields = kwargs.get("exclude_fields")
Expand All @@ -50,6 +53,7 @@ class Meta:
description = "Auto generated InputType for {} model".format(
kwargs.get("model").__name__
)
non_required_fields=kwargs.get("non_required_fields")

return GenericInputType

Expand Down Expand Up @@ -154,9 +158,9 @@ def serialize(time):
if isinstance(time, datetime.datetime):
time = time.time()

assert isinstance(time, datetime.time), 'Received not compatible time "{}"'.format(
repr(time)
)
assert isinstance(
time, datetime.time
), 'Received not compatible time "{}"'.format(repr(time))
return time.isoformat()


Expand All @@ -168,19 +172,44 @@ def serialize(date):

if isinstance(date, datetime.datetime):
date = date.date()
assert isinstance(date, datetime.date), 'Received not compatible date "{}"'.format(
repr(date)
)
assert isinstance(
date, datetime.date
), 'Received not compatible date "{}"'.format(repr(date))
return date.isoformat()


class CustomDateTime(DateTime):
class CustomDateTime(graphene.Scalar):
"""
This method is wrote to accept epoch and datetime formats
"""
@staticmethod
def serialize(dt):
if isinstance(dt, CustomDateFormat):
return dt.date_str
return int(dt.timestamp() * 1000.)

assert isinstance(
dt, (datetime.datetime, datetime.date)
), 'Received not compatible datetime "{}"'.format(repr(dt))
return dt.isoformat()
@staticmethod
def parse_value(value):
if isinstance(value, str):
value = int(value)
return datetime.datetime.fromtimestamp(value / 1000.0).strftime('%Y-%m-%d %H:%M:%S.%f')


class CustomDict(graphene.Scalar):

@staticmethod
def serialize(dt):
return dt

@staticmethod
def parse_literal(node):
if node.fields:
eval_node = literal_eval(node.value)
if isinstance(eval_node, dict):
return eval_node
else:
raise ValueError("For instance '{'key':'value'}'")
logging.info("INFO ::: JSON/HStore value passed empty")
return None

@staticmethod
def parse_value(value):
return value
Loading