Skip to content

Commit

Permalink
m to n handling added to the rest interface
Browse files Browse the repository at this point in the history
  • Loading branch information
vvmruder committed Oct 13, 2015
1 parent 5d4b397 commit 9c89676
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions lib/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from sqlalchemy.orm.exc import NoResultFound
from pyramid.httpexceptions import HTTPNotFound, HTTPBadRequest, HTTPServerError
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, Session, scoped_session
from sqlalchemy.orm import sessionmaker, Session, scoped_session, class_mapper
from pyramid.config import Configurator
from pyramid.request import Request

Expand Down Expand Up @@ -595,7 +595,7 @@ def description(self, request):
"""
return self.model.description(self.dictionary)

def m_to_n_handling(self, key, value):
def m_to_n_handling(self, key, value, session):
"""
Small helper method. It checks if the passed key is defined as an m_to_n relation ship to other tables. If
it is: The method gets the corresponding objects from database by the passed (comma seperated) id's and return
Expand All @@ -605,19 +605,21 @@ def m_to_n_handling(self, key, value):
:type key: str
:param value: The effective value which is intended to be set to the column
:type value:
:param session: The session instance which should be used by this method
:type session: Session
:return: The found results of corresponding datasets or False if the checked column was not a m_to_n one
:rtype: bool or list
"""
for column in self.model.description(self.dictionary).get('columns'):
# found the column which is m:n
if column.get('is_m_to_n') and key == column.get('column_name'):
value_list = str(value).split(',')
bound_model = getattr(self.model, key).argument
bound_model = class_mapper(self.model).get_property(key).argument
pk_name = bound_model.description(self.dictionary).get('pk_name')
pk_column = getattr(bound_model, pk_name)
relation_list = []
for identifier in value_list:
result = self.session.query(bound_model).filter(pk_column == identifier).one()
result = session.query(bound_model).filter(pk_column == identifier).one()
relation_list.append(result)
return relation_list
return False
Expand Down Expand Up @@ -645,7 +647,7 @@ def create(self, request):
m_to_n = self.m_to_n_handling(key, value)
if m_to_n:
value = m_to_n
setattr(new_record, key, value)
setattr(new_record, key, value, session)
session.add(new_record)
session.flush()
request.response.status_int = 201
Expand Down Expand Up @@ -683,7 +685,7 @@ def update(self, request):
for key, value in data.iteritems():
if key == 'geom':
value = WKBSpatialElement(buffer(wkt.loads(value).wkb), srid=2056)
m_to_n = self.m_to_n_handling(key, value)
m_to_n = self.m_to_n_handling(key, value, session)
if m_to_n:
value = m_to_n
setattr(element, key, value)
Expand Down

0 comments on commit 9c89676

Please sign in to comment.