def create_article(session: helpers.extend.session, user: hug.directives.user, response, warehouse_id: int, reference_id: int, quantity: int, expiry: fields.Date(allow_none=True)=None, location_id: fields.Int(allow_none=True)=None, tags=None): """Creates a article""" db_tags = queries.get_tags_from_ids(session, user, warehouse_id, tags) if db_tags == None: return response.error("invalid_tag_ids", falcon.HTTP_400) try: warehouse = queries.with_editor_role(queries.user_warehouse(session, user, warehouse_id)).one() location = queries.with_editor_role(queries.user_location(session, user, location_id)).one() if location_id else None reference = queries.with_editor_role(queries.user_reference(session, user, reference_id)).one() if warehouse.id != reference.warehouse.id or (location is not None and warehouse.id != location.warehouse.id): return helpers.response.error("bad_referenece_and_or_location", falcon.HTTP_400) article = Article(warehouse=warehouse, location=location, reference=reference, quantity=quantity, expiry=expiry, tags=db_tags) return article except NoResultFound: return helpers.response.error("warehouse_location_or_reference_not_found", falcon.HTTP_401)
def update_tag(session: helpers.extend.session, user: hug.directives.user, response, id: int, name): """Updates a tag""" return helpers.update( "tag", session, queries.with_editor_role(queries.user_tag(session, user, id)), {"name": name})
def create_tag(session: helpers.extend.session, user: hug.directives.user, response, warehouse_id: int, name): """Creates a tag""" return helpers.do_in_warehouse( "tag", queries.with_editor_role( queries.user_warehouse(session, user, warehouse_id)), lambda warehouse: Tag(warehouse=warehouse, name=name))
def update_warehouse_settings(session: helpers.extend.session, user: hug.directives.user, response, id: int, body): """Gets a warehouse""" helpers.update( "warehouse", session, queries.with_editor_role(queries.user_warehouse(session, user, id)), {"settings": json.dumps(body)}) return body
def update_location(session: helpers.extend.session, user: hug.directives.user, response, id: int, name): """Updates a location""" if len(name) == 0: return helpers.response.error("location_name_mandatory", falcon.HTTP_400) return helpers.update( "location", session, queries.with_editor_role(queries.user_location(session, user, id)), {"name": name})
def create_location(session: helpers.extend.session, user: hug.directives.user, response, warehouse_id: int, name): """Creates a location""" if len(name) == 0: return helpers.response.error("location_name_mandatory", falcon.HTTP_400) return helpers.do_in_warehouse( "location", queries.with_editor_role( queries.user_warehouse(session, user, warehouse_id)), lambda warehouse: Location(warehouse=warehouse, name=name))
def update_article(session: helpers.extend.session, user: hug.directives.user, response, id: int, quantity: int, expiry: fields.Date(allow_none=True)=None, location_id: fields.Int(allow_none=True)=None, tags=None): """Updates a article""" def update(article): if location_id: location = queries.user_location(session, user, location_id).one() if location.warehouse_id != article.warehouse_id: raise Exception("wrong_location_id") db_tags = queries.get_tags_from_ids(session, user, article.warehouse_id, tags) if db_tags == None: raise Exception("invalid_tag_ids") return {"location_id": location_id, "quantity": quantity, "expiry": expiry, "tags": db_tags} try: print(id) return helpers.update("article", session, queries.with_editor_role(queries.user_article(session, user, id)), update) except Exception as e: return helpers.response.error(e.__str__(), falcon.HTTP_404)
def update_reference(session: helpers.extend.session, user: hug.directives.user, response, id: int, name, categories=None, target_quantity: fields.Int(allow_none=True) = None): """Updates a reference""" if len(name) == 0: return helpers.response.error("reference_name_mandatory", falcon.HTTP_400) db_categories = queries.get_categories_from_ids( session, user, session.query(Reference).get(id).warehouse_id, categories) if db_categories == None: return response.error("invalid_category_ids", falcon.HTTP_400) return helpers.update( "reference", session, queries.with_editor_role(queries.user_reference(session, user, id)), { "name": name, "categories": db_categories, "target_quantity": target_quantity })
def create_reference(session: helpers.extend.session, user: hug.directives.user, response, warehouse_id: int, name, categories=None, target_quantity: fields.Int(allow_none=True) = None): """Creates a reference""" if len(name) == 0: return helpers.response.error("reference_name_mandatory", falcon.HTTP_400) db_categories = queries.get_categories_from_ids(session, user, warehouse_id, categories) if db_categories == None: return response.error("invalid_category_ids", falcon.HTTP_400) return helpers.do_in_warehouse( "reference", queries.with_editor_role( queries.user_warehouse(session, user, warehouse_id)), lambda warehouse: Reference(warehouse=warehouse, name=name, categories=db_categories, target_quantity=target_quantity))
def delete_article(session: helpers.extend.session, user: hug.directives.user, response, id: int): """Deletes a article""" return helpers.delete("article", session, queries.with_editor_role(queries.user_article(session, user, id)))
def delete_location(session: helpers.extend.session, user: hug.directives.user, response, id: int): """Deletes a location""" return helpers.delete( "location", session, queries.with_editor_role(queries.user_location(session, user, id)))
def delete_category(session: helpers.extend.session, user: hug.directives.user, response, id: int): """Deletes a category""" return helpers.delete( "category", session, queries.with_editor_role(queries.user_category(session, user, id)))