Skip to content

Commit

Permalink
Add relations
Browse files Browse the repository at this point in the history
  • Loading branch information
f1nality committed Dec 25, 2018
1 parent 9e7cdd9 commit e6f5754
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
21 changes: 20 additions & 1 deletion jet_bridge/serializers/model_description.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

class ModelDescriptionFieldSerializer(Serializer):
name = fields.CharField()
field = fields.CharField()
db_column = fields.CharField()
field = fields.CharField()
filterable = fields.BooleanField()
editable = fields.BooleanField()
params = fields.JSONField()
Expand All @@ -21,16 +21,35 @@ class Meta:
)


class ModelDescriptionRelationSerializer(Serializer):
name = fields.CharField()
related_model = fields.JSONField()
field = fields.CharField()
related_model_field = fields.CharField()
through = fields.JSONField()

class Meta:
fields = (
'name',
'related_model',
'field',
'related_model_field',
'through',
)


class ModelDescriptionSerializer(Serializer):
model = fields.CharField()
db_table = fields.CharField()
hidden = fields.BooleanField()
fields = ModelDescriptionFieldSerializer(many=True)
relations = ModelDescriptionRelationSerializer(many=True)

class Meta:
fields = (
'model',
'db_table',
'hidden',
'fields',
'relations',
)
21 changes: 20 additions & 1 deletion jet_bridge/views/model_description.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from sqlalchemy import inspect
from sqlalchemy.orm.base import ONETOMANY

from jet_bridge.db import Session, MappedBase
from jet_bridge.models import data_types
Expand Down Expand Up @@ -36,14 +37,32 @@ def map_column(column):
'params': params
}

def map_relation(relation):
field = None
through = None

if relation.direction == ONETOMANY:
field = 'ManyToOneRel'

return {
'name': relation.key,
'related_model': {
'model': relation.table.name
},
'field': field,
'related_model_field': relation.primaryjoin.right.name,
'through': through
}

def map_table(cls):
mapper = inspect(cls)
name = mapper.selectable.name
return {
'model': name,
'db_table': name,
'fields': list(map(map_column, mapper.columns)),
'hidden': name in hidden
'hidden': name in hidden,
'relations': list(map(map_relation, filter(lambda x: x.direction == ONETOMANY, mapper.relationships)))
}

return list(map(map_table, MappedBase.classes))
Expand Down

0 comments on commit e6f5754

Please sign in to comment.