def create(self, **kwargs): req = cherrypy.request orm_session = req.orm_session event = from_collection(req.json, Event()) orm_session.add(event) orm_session.commit() return to_collection(event, sort_keys=True)
def update(self, id, **kwargs): id = int(id) req = cherrypy.request orm_session = req.orm_session user = api.find_user_by_id(orm_session, id) if user: user = from_collection(req.json, user) orm_session.merge(user) orm_session.commit() return to_collection(user, excludes=("password", "salt"), sort_keys=True) raise HTTPError(404)
def update(self, id, **kwargs): id = int(id) req = cherrypy.request orm_session = req.orm_session user = api.find_user_by_id(orm_session, id) if user: user = from_collection(req.json, user) orm_session.merge(user) orm_session.commit() return to_collection(user, excludes=('password', 'salt'), sort_keys=True) raise HTTPError(404)
def create(self, **kwargs): ''' `user_register` [POST] /user/ ''' req = cherrypy.request orm_session = req.orm_session user = from_collection(req.json, User()) orm_session.add(user) orm_session.commit() cherrypy.session['user_id'] = user.id return to_collection(user, excludes=("password", "salt"), sort_keys=True)
def update(self, id, **kwargs): id = int(id) req = cherrypy.request orm_session = req.orm_session event = api.find_event_by_id(orm_session, id) logger.debug(event) if event: # Caution! crunches ahead event = from_collection(req.json, event, excludes=['fields']) # skip jsonencoded # since 'hidden' is not implemented in the model, skip it for now event.fields = req.json['fields'] # and set them manually orm_session.merge(event) orm_session.commit() return to_collection(event, sort_keys=True) raise HTTPError(404)
def post_message(self, id, **kwargs): ''' `room_post_message` [POST] /room/{id} ''' req = cherrypy.request req.json['user_id'] = cherrypy.session['user_id'] req.json['room_id'] = int(id) if 'meta' not in req.json: req.json['meta'] = {} orm_session = req.orm_session msg = from_collection(req.json, Message()) orm_session.add(msg) orm_session.commit() resp = to_collection(msg, sort_keys=True) #resp['author'] = to_collection(msg.author, sort_keys=True) return resp
def create(self, **kwargs): ''' `room_create` [POST] /room/ ''' req = cherrypy.request orm_session = req.orm_session room = from_collection(req.json, Room()) room.creator_id = cherrypy.session['user_id'] user = api.find_user_by_id(orm_session, cherrypy.session['user_id']) room.members.append(user) try: orm_session.add(room) orm_session.commit() return to_collection(room, sort_keys=True) except sqlalchemy.exc.IntegrityError: raise HTTPError(403)
def test_from_collection(self): self.assertEqual(1, from_collection(1, None)) self.assertEqual(1.1, from_collection(1.1, None)) self.assertEqual("str", from_collection("str", None)) self.assertEqual([1, 2, 3], from_collection([1, 2, 3], [4, 5, 6])) self.assertEqual([1, 2, 3], from_collection((1, 2, 3), (4, 5, 6))) self.assertEqual([1, 2, 3], from_collection(set([1, 2, 3]), set([4, 5, 6]))) self.assertEqual([1, 2, 3], from_collection(frozenset([1, 2, 3]), frozenset([4, 5, 6]))) doc = {'date': {'date': '2012-01-01'}, 'time': {'time': '00:00:00'}, 'interval': {'interval': 3600}, 'id': 1, 'derivedprop': 2, 'datetime': {'datetime': '2012-01-01T00:00:00'}, 'geo': {'type': 'Point', 'coordinates': (45, 45)}, 'related': [{'key': u'key1', 'parent_id': 1, 'discriminator': 'related'}, {'key': u'key2', 'parent_id': 1, 'discriminator': 'relatedsubclass', 'subclass_prop': 'sub'}]} te = DerivedTestEntity() te = from_collection(doc, te) self.assertEqual(te.date, date(2012, 1, 1)) self.assertEqual(te.time, time(0, 0, 0)) self.assertEqual(te.interval, timedelta(seconds=3600)) self.assertEqual(te.datetime, datetime(2012, 1, 1, 0, 0, 0)) self.assertEqual(te.id, 1) self.assertEqual(te.derivedprop, 2) self.assertEqual(to_shape(te.geo).wkt, Point(45, 45).wkt) self.assertIsNone(te.related[0].id) self.assertEqual(te.related[0].parent_id, 1) self.assertEqual(te.related[0].key, "key1") self.assertEqual(te.related[0].discriminator, "related") self.assertIsNone(te.related[1].id) self.assertEqual(te.related[1].parent_id, 1) self.assertEqual(te.related[1].key, "key2") self.assertEqual(te.related[1].discriminator, "relatedsubclass") self.assertEqual(te.related[1].subclass_prop, "sub") # TODO: testing loading of persisted entity, json format, excludes doc = {'date': {'date': '2012-01-01'}, 'time': {'time': '00:00:00'}, 'interval': {'interval': 3600}, 'id': 1, 'datetime': {'datetime': '2012-01-01T00:00:00'}, 'geo': {'type': 'Point', 'coordinates': (45, 45)}, 'related': [{'key': u'key1', 'parent_id': 1, 'discriminator': u'related', "id": 3}]} session = Session() te = session.query(TestEntity).get(2) te = from_collection(doc, te, excludes=["interval"]) self.assertEqual(te.date, date(2012, 1, 1)) self.assertEqual(te.time, time(0, 0, 0)) self.assertEqual(te.interval, timedelta(seconds=3601)) self.assertEqual(te.datetime, datetime(2012, 1, 1, 0, 0, 0)) self.assertEqual(te.id, 1) self.assertEqual(to_shape(te.geo).wkt, Point(45, 45).wkt) self.assertEqual(te.related[0].parent_id, 1) self.assertEqual(te.related[0].key, u"key1") self.assertEqual(te.related[0].id, 3) self.assertEqual(te.related[0].discriminator, u"related") self.assertEqual(len(te.related), 1) self.assertIsNotNone(Session.object_session(te.related[0])) doc = {'related': [{'key': u'hello', 'parent_id': 1, 'discriminator': u'related'}]} te = from_collection(doc, te, collection_handling="append") self.assertEqual(len(te.related), 2) self.assertEqual(te.related[-1].key, u"hello") self.assertEqual(te.related[-1].parent_id, 1) self.assertEqual(te.related[-1].discriminator, "related") te = DerivedTestEntity() json_doc = '{"time": {"time": "00:00:00"}, "date": {"date": "2012-01-01"}, "geo": {"type": "Point", "coordinates": [45, 45]}, "interval": {"interval": 3600}, "datetime": {"datetime": "2012-01-01T00:00:00"}, "id": 1, "related": [{"parent_id": 1, "key": "key1", "discriminator": "related"}, {"parent_id": 1, "subclass_prop": "sub", "key": "key2", "discriminator": "relatedsubclass"}], "derivedprop": 2}' te = from_collection(json_doc, te, format="json") self.assertEqual(te.date, date(2012, 1, 1)) self.assertEqual(te.time, time(0, 0, 0)) self.assertEqual(te.interval, timedelta(seconds=3600)) self.assertEqual(te.datetime, datetime(2012, 1, 1, 0, 0, 0)) self.assertEqual(te.id, 1) self.assertEqual(te.derivedprop, 2) self.assertEqual(to_shape(te.geo).wkt, Point(45, 45).wkt) self.assertIsNone(te.related[0].id) self.assertEqual(te.related[0].parent_id, 1) self.assertEqual(te.related[0].key, "key1") self.assertEqual(te.related[0].discriminator, "related") self.assertIsNone(te.related[1].id) self.assertEqual(te.related[1].parent_id, 1) self.assertEqual(te.related[1].key, "key2") self.assertEqual(te.related[1].discriminator, "relatedsubclass") self.assertEqual(te.related[1].subclass_prop, "sub")