def test_null_issues(self): """ Make sure nullable values are set properly when null #42 introduced an issue where values may not have been properly set if the value is supposed to be set to null """ Base = declarative_base() class Person(Base): __tablename__ = 'person' id = Column(Integer, primary_key=True) fullname = Column(String, nullable=True) age = Column(Integer, nullable=True) schema = SQLAlchemySchemaNode(Person) person = Person( id=7, fullname="Joe Smith", age=35, ) # dict coming from a form submission update_cstruct = { 'id': '7', 'fullname': '', 'age': '', } update_appstruct = schema.deserialize(update_cstruct) schema.objectify(update_appstruct, context=person) self.assertEqual(person.id, 7) self.assertEqual(person.fullname, None) self.assertEqual(person.age, None)
def statistic_sheet_add_edit_view(context, request): """ View for adding editing statistics sheets """ post_datas = request.POST or request.json_body if 'title' in post_datas: schema = SQLAlchemySchemaNode(StatisticSheet, includes=('title',)) try: appstruct = schema.deserialize(request.POST) except colander.Invalid: logger.exception(u"Erreur à la création de la feuille de \ statistiques") else: if context.__name__ == 'statistic_sheet': sheet = schema.objectify(appstruct, context) sheet = request.dbsession.merge(sheet) else: sheet = schema.objectify(appstruct) request.dbsession.add(sheet) request.dbsession.flush() url = request.route_path('statistic', id=sheet.id) return dict(redirect=url) logger.debug(u"Invalid datas have been passed") raise HTTPClientError() logger.debug(u"Missing datas in the request") raise HTTPClientError()
def statistic_sheet_add_edit_view(context, request): """ View for adding editing statistics sheets """ logger.info("Here we are") logger.info(request.POST) if 'title' in request.POST: schema = SQLAlchemySchemaNode(StatisticSheet, includes=('title',)) try: appstruct = schema.deserialize(request.POST) except colander.Invalid: logger.exception(u"Erreur à la création de la feuille de \ statistiques") else: if context.__name__ == 'statistic_sheet': sheet = schema.objectify(appstruct, context) sheet = request.dbsession.merge(sheet) else: sheet = schema.objectify(appstruct) request.dbsession.add(sheet) request.dbsession.flush() url = request.route_path('statistic', id=sheet.id) return HTTPFound(url) logger.debug(u"Invalid datas have been passed") raise HTTPClientError() logger.debug(u"Missing datas in the request") raise HTTPClientError()
def test_dictify_with_null(self): """ Test SQLAlchemySchemaNode.dictify(obj) with null values and show that result is a valid appstruct for the given schema """ Base = declarative_base() class Sensor(Base): __tablename__ = 'sensor' sensor_id = Column(Integer, primary_key=True) institution_id = Column(Integer, nullable=True) sensor_label = Column(String, nullable=True) sensor = Sensor( sensor_id=3, institution_id=None, sensor_label=None, ) schema = SQLAlchemySchemaNode(Sensor) appstruct = schema.dictify(sensor) cstruct = schema.serialize(appstruct=appstruct) newappstruct = schema.deserialize(cstruct) newobj = schema.objectify(appstruct) self.assertEqual(appstruct, newappstruct) self.assertEqual(sensor.sensor_id, newobj.sensor_id) self.assertEqual(sensor.institution_id, newobj.institution_id) self.assertEqual(sensor.sensor_label, newobj.sensor_label)
def userdata_doctype_view(userdata_model, request): """ View used to register doctypes status userdata_model The UserDatas model retrieved through traversal """ if 'submit' in request.params: schema = get_doctypes_form_schema(userdata_model)[0] appstruct = request.POST.items() appstruct = peppercorn.parse(appstruct) try: appstruct = schema.deserialize(appstruct) except colander.Invalid: logger.exception( "Error while validating doctype registration" ) else: node_schema = SQLAlchemySchemaNode(UserDatasSocialDocTypes) for data in appstruct.values(): model = node_schema.objectify(data) request.dbsession.merge(model) request.session.flash( u"Les informations saisies ont bien été enregistrées" ) return HTTPFound( request.route_path('userdata', id=userdata_model.id) )
def userdata_doctype_view(userdata_model, request): """ View used to register doctypes status userdata_model The UserDatas model retrieved through traversal """ if 'submit' in request.params: schema = get_doctypes_form_schema(userdata_model)[0] appstruct = request.POST.items() appstruct = peppercorn.parse(appstruct) try: appstruct = schema.deserialize(appstruct) except colander.Invalid: logger.exception("Error while validating doctype registration") else: node_schema = SQLAlchemySchemaNode(UserDatasSocialDocTypes) for data in appstruct.values(): model = node_schema.objectify(data) request.dbsession.merge(model) request.session.flash( u"Les informations saisies ont bien été enregistrées") return HTTPFound(request.route_path('userdata', id=userdata_model.id))