def test_update_version_function(self): with app.test_request_context(): update_version(1, True, "tracks_ver") self.assertEqual(Version.query.get(1).id, 1) self.assertEqual(Version.query.get(1).tracks_ver, 0) update_version(1, False, "tracks_ver") self.assertEqual(Version.query.get(2).tracks_ver, 1)
def save_db_model(new_model, model_name, event_id=None): """ Save a new/modified model to database """ save_to_db(new_model, "Model %s saved" % model_name) if not event_id: update_version(event_id, False, "session_ver") return new_model
def create_model(model, data, event_id=None): """ Creates a model. If event_id is set, the model is assumed as a child of event i.e. Service Model """ if event_id is not None: get_object_or_404(EventModel, event_id) data['event_id'] = event_id new_model = model(**data) save_to_db(new_model, "Model %s saved" % model.__name__) if event_id: update_version(event_id, False, "session_ver") return new_model
def update_model(model, item_id, data, event_id=None): """ Updates a model """ if event_id is not None: item = get_object_in_event(model, item_id, event_id) else: item = get_object_or_404(model, item_id) db.session.query(model).filter_by(id=item_id).update(dict(data)) # model.__table__.update().where(model.id==item_id).values(**data) save_to_db(item, "%s updated" % model.__name__) if event_id: update_version(event_id, False, "session_ver") return item
def create(self, data, url): data = self.validate(data) payload = self.fix_payload(data) new_event = self.model(**payload) new_event.creator = g.user save_to_db(new_event, "Event saved") # set organizer role = Role.query.filter_by(name=ORGANIZER).first() uer = UsersEventsRoles(g.user, new_event, role) save_to_db(uer, 'UER saved') update_version(event_id=new_event.id, is_created=True, column_to_increment="event_ver") # Return created resource with a 201 status code and its Location # (url) in the header. resource_location = url + '/' + str(new_event.id) return self.get(new_event.id), 201, {'Location': resource_location}
def create(self, data, url): data = self.validate(data) payload = self.fix_payload(data) new_event = self.model(**payload) new_event.creator = g.user save_to_db(new_event, "Event saved") # set organizer role = Role.query.filter_by(name=ORGANIZER).first() uer = UsersEventsRoles(g.user, new_event, role) save_to_db(uer, 'UER saved') update_version( event_id=new_event.id, is_created=True, column_to_increment="event_ver" ) # Return created resource with a 201 status code and its Location # (url) in the header. resource_location = url + '/' + str(new_event.id) return self.get(new_event.id), 201, {'Location': resource_location}
def update_model(model, item_id, data, event_id=None): """ Updates a model """ if event_id is not None: item = get_object_in_event(model, item_id, event_id) else: item = get_object_or_404(model, item_id) # if no data in payload, happens when only related models were # changed through the API if len(data) == 0: return item # update data db.session.query(model).filter_by(id=item_id).update(dict(data)) # model.__table__.update().where(model.id==item_id).values(**data) save_to_db(item, "%s updated" % model.__name__) if event_id: update_version(event_id, False, "session_ver") return item
def test_increasing_version_after_update_version_called(self): with app.test_request_context(): self.assertEqual(len(Version.query.all()), 0) update_version(1, True, "tracks_ver") self.assertEqual(len(Version.query.all()), 1)