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

Support core model field subclasses #38

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
17 changes: 13 additions & 4 deletions business_logic/models/types_.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,17 @@
'datetime': (models.DateTimeField,),
}

TYPES_FOR_DJANGO_FIELDS = {}

for _type, fields in DJANGO_FIELDS_FOR_TYPES.items():
for field in fields:
TYPES_FOR_DJANGO_FIELDS[field] = _type
def is_model_field(cls):
field_types in DJANGO_FIELDS_FOR_TYPES['model']
for field_type in field_types:
if issubclass(cls, field_type):
return True
return False


def get_data_type(cls):
for data_type, field_types in DJANGO_FIELDS_FOR_TYPES.items():
for field_type in field_types:
if issubclass(cls, field_type):
return data_type
18 changes: 15 additions & 3 deletions business_logic/rest/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
FunctionLibrary, LogEntry, Program, ProgramArgument, ProgramArgumentField, ProgramInterface,
ProgramVersion, ReferenceDescriptor, FunctionArgument, FunctionArgumentChoice)

from ..models.types_ import TYPES_FOR_DJANGO_FIELDS, DJANGO_FIELDS_FOR_TYPES
from ..models.types_ import get_data_type, is_model_field

from ..blockly.build import BlocklyXmlBuilder
from ..blockly.create import NodeTreeCreator
Expand Down Expand Up @@ -206,24 +206,36 @@ def to_representation(self, instance):
representation['name'] = instance.name

argument = instance.program_argument
print('argument', argument)
print('argument.content_type', argument.content_type)
model = argument.content_type.model_class()
print('model', model)

field_names = instance.name.split('.')
print('field_names', field_names)
for i, field_name in enumerate(field_names):
field = model._meta.get_field(field_name)
is_last_field = i == len(field_names) - 1
is_django_model = field.__class__ in DJANGO_FIELDS_FOR_TYPES['model']
if not is_last_field:
assert is_model_field(field.__class__)
model = field.related_model
continue

data_type = get_data_type(field.__class__)
is_django_model = data_type == 'model'

if is_django_model:
model = field.related_model

if is_last_field:
representation['data_type'] = TYPES_FOR_DJANGO_FIELDS[field.__class__]
representation['data_type'] = data_type
representation['content_type'] = (ContentTypeSerializer().to_representation(
ContentType.objects.get_for_model(model)) if is_django_model else None)

representation['verbose_name'] = instance.get_title()

print('representation', representation)

return representation


Expand Down