def record(self, request): """Get json object with record data """ obj = self.get() result_dict = {} for column in obj.__table__.columns.keys(): result_dict[column] = to_json_type(getattr(obj, column, None)) result_dict['__url__'] = request.link(self) # add links to related resources result_dict['__links__'] = dict() for foreign_key in obj.__table__.foreign_keys: column_names = foreign_key.constraint.columns column_values = [ getattr(obj, column_name, None) for column_name in column_names ] if [val for val in column_values if val]: table = foreign_key.column.table.name result_dict['__links__'][foreign_key.name or str(foreign_key)] = request.link( Record(table, column_values)) return result_dict
def record_put(self, request): """Replace record data """ r = self.replace(request.json) return request.view(Record.from_object(r))
def record_patch(self, request): """Upgrade record data """ r = self.patch(request.json) return request.view(Record.from_object(r))
def tables_add(self, request): """Add record to table with values form request body """ r = self.add(request.json) return request.view(Record.from_object(r))
def tables(self, request): return { 'data': [request.view(Record.from_object(r)) for r in self.select()], 'add': request.link(self, 'add') }
def get_record(table_name, id): return Record(table_name, id)