def itemCreate(): """ Creates a new item owned by the logged-in user """ state = request.values.get('state') if not check_nonce(state): return bad_request_error() cat_name = bleach.clean(request.values.get("item_create_parent")) cat = dal.get_category_by_name(cat_name) if not cat: return not_found_error() active_user = get_active_user() if not active_user: return not_authenticated_error() item_name = bleach.clean(request.values.get("item_create_name")) duplicate = dal.get_item_by_name(cat.cat_id, item_name) if duplicate: return already_exists_error() try: pic_data = validate_picture(request.files["item_create_pic"]) except InvalidPictureError: return bad_request_error() # All checks passed generate_nonce() desc = bleach.clean(request.values.get("item_create_description")) item_id = dal.create_item(item_name, cat.cat_id, active_user.user_id, pic_data, desc) if not item_id: logging.error( "Unable to create item: did not receive an item_id from database") return internal_error() item = dal.get_item(item_id) if not item: logging.error( "Unable to create item: an instance was not created for item_id {}" .format(item_id)) return internal_error() return redirect("/catalog/{}/{}/".format(cat_name, item_name))
def itemCreate(): """ Creates a new item owned by the logged-in user """ state = request.values.get('state') if not check_nonce(state): return bad_request_error() cat_name = bleach.clean(request.values.get("item_create_parent")) cat = dal.get_category_by_name(cat_name) if not cat: return not_found_error() active_user = get_active_user() if not active_user: return not_authenticated_error() item_name = bleach.clean(request.values.get("item_create_name")) duplicate = dal.get_item_by_name(cat.cat_id, item_name) if duplicate: return already_exists_error() try: pic_data = validate_picture(request.files["item_create_pic"]) except InvalidPictureError: return bad_request_error() # All checks passed generate_nonce() desc = bleach.clean(request.values.get("item_create_description")) item_id = dal.create_item( item_name, cat.cat_id, active_user.user_id, pic_data, desc) if not item_id: logging.error("Unable to create item: did not receive an item_id from database") return internal_error() item = dal.get_item(item_id) if not item: logging.error( "Unable to create item: an instance was not created for item_id {}".format(item_id)) return internal_error() return redirect("/catalog/{}/{}/".format(cat_name, item_name))
def categoryCreate(): """ Creates a new category owned by the logged-in user """ state = request.values.get('state') if not check_nonce(state): return bad_request_error() active_user = get_active_user() if not active_user: return not_authenticated_error() cat_name = bleach.clean(request.values.get("cat_create_name")) duplicate = dal.get_category_by_name(cat_name) if duplicate: return already_exists_error() # All checks passed generate_nonce() cat_id = dal.create_category(cat_name, active_user.user_id) return redirect("/")
def categoryCreate(): """ Creates a new category owned by the logged-in user """ state = request.values.get('state') if not check_nonce(state): return bad_request_error() active_user = get_active_user() if not active_user: return not_authenticated_error() cat_name = bleach.clean(request.values.get("cat_create_name")) duplicate = dal.get_category_by_name(cat_name) if duplicate: return already_exists_error() # All checks passed generate_nonce() cat_id = dal.create_category(cat_name, active_user.user_id) return redirect("/")