Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
value_types_description = 'floating point numbers'
class StrField(DjangoQLField):
type = 'str'
value_types = [text_type]
value_types_description = 'strings'
class BoolField(DjangoQLField):
type = 'bool'
value_types = [bool]
value_types_description = 'True or False'
class DateField(DjangoQLField):
type = 'date'
value_types = [text_type]
value_types_description = 'dates in "YYYY-MM-DD" format'
def validate(self, value):
super(DateField, self).validate(value)
try:
self.get_lookup_value(value)
except ValueError:
raise DjangoQLSchemaError(
'Field "%s" can be compared to dates in '
'"YYYY-MM-DD" format, but not to %s' % (
self.name,
repr(value),
)
)
def build_filter(expr, schema_instance):
if isinstance(expr.operator, Logical):
left = build_filter(expr.left, schema_instance)
right = build_filter(expr.right, schema_instance)
if expr.operator.operator == 'or':
return left | right
else:
return left & right
field = schema_instance.resolve_name(expr.left)
if not field:
# That must be a reference to a model without specifying a field.
# Let's construct an abstract lookup field for it
field = DjangoQLField(
name=expr.left.parts[-1],
nullable=True,
)
return field.get_lookup(
path=expr.left.parts[:-1],
operator=expr.operator.operator,
value=expr.right.value,
)
)
else:
msg = (
'Field "{field}" has "{field_type}" type. It can '
'be compared to {possible_values}, '
'but not to {value}'
)
raise DjangoQLSchemaError(msg.format(
field=self.name,
field_type=self.type,
possible_values=self.value_types_description,
value=repr(value),
))
class IntField(DjangoQLField):
type = 'int'
value_types = [int]
value_types_description = 'integer numbers'
class FloatField(DjangoQLField):
type = 'float'
value_types = [int, float, Decimal]
value_types_description = 'floating point numbers'
class StrField(DjangoQLField):
type = 'str'
value_types = [text_type]
value_types_description = 'strings'
def get_field_cls(self, field):
str_fields = (models.CharField, models.TextField, models.UUIDField)
if isinstance(field, str_fields):
return StrField
elif isinstance(field, (models.AutoField, models.IntegerField)):
return IntField
elif isinstance(field, (models.BooleanField, models.NullBooleanField)):
return BoolField
elif isinstance(field, (models.DecimalField, models.FloatField)):
return FloatField
elif isinstance(field, models.DateTimeField):
return DateTimeField
elif isinstance(field, models.DateField):
return DateField
return DjangoQLField
)
raise DjangoQLSchemaError(msg.format(
field=self.name,
field_type=self.type,
possible_values=self.value_types_description,
value=repr(value),
))
class IntField(DjangoQLField):
type = 'int'
value_types = [int]
value_types_description = 'integer numbers'
class FloatField(DjangoQLField):
type = 'float'
value_types = [int, float, Decimal]
value_types_description = 'floating point numbers'
class StrField(DjangoQLField):
type = 'str'
value_types = [text_type]
value_types_description = 'strings'
class BoolField(DjangoQLField):
type = 'bool'
value_types = [bool]
value_types_description = 'True or False'
Returns a dict with all model labels and their fields found.
"""
result = {}
open_set = deque([model])
closed_set = list(exclude)
while open_set:
model = open_set.popleft()
model_label = self.model_label(model)
if model_label in closed_set:
continue
model_fields = OrderedDict()
for field in self.get_fields(model):
if not isinstance(field, DjangoQLField):
field = self.get_field_instance(model, field)
if not field:
continue
if isinstance(field, RelationField):
if field.relation not in closed_set:
model_fields[field.name] = field
open_set.append(field.related_model)
else:
model_fields[field.name] = field
result[model_label] = model_fields
closed_set.append(model_label)
return result
value_types_description = 'integer numbers'
class FloatField(DjangoQLField):
type = 'float'
value_types = [int, float, Decimal]
value_types_description = 'floating point numbers'
class StrField(DjangoQLField):
type = 'str'
value_types = [text_type]
value_types_description = 'strings'
class BoolField(DjangoQLField):
type = 'bool'
value_types = [bool]
value_types_description = 'True or False'
class DateField(DjangoQLField):
type = 'date'
value_types = [text_type]
value_types_description = 'dates in "YYYY-MM-DD" format'
def validate(self, value):
super(DateField, self).validate(value)
try:
self.get_lookup_value(value)
except ValueError:
raise DjangoQLSchemaError(
except ValueError:
raise DjangoQLSchemaError(
'Field "%s" can be compared to dates in '
'"YYYY-MM-DD" format, but not to %s' % (
self.name,
repr(value),
)
)
def get_lookup_value(self, value):
if not value:
return None
return datetime.strptime(value, '%Y-%m-%d').date()
class DateTimeField(DjangoQLField):
type = 'datetime'
value_types = [text_type]
value_types_description = 'timestamps in "YYYY-MM-DD HH:MM" format'
def validate(self, value):
super(DateTimeField, self).validate(value)
try:
self.get_lookup_value(value)
except ValueError:
raise DjangoQLSchemaError(
'Field "%s" can be compared to timestamps in '
'"YYYY-MM-DD HH:MM" format, but not to %s' % (
self.name,
repr(value),
)
)