def addItem(): if request.method == "GET": return render_template( "add_item.html", your_categories=act.categories(user_id=g.USER.id), others_categories=act.all_categories(), ) elif request.method == "POST": allowed_image_extension = {"png", "jpg", "jpeg", "gif"} item_image = request.files["image"] item_image_name = [""] # Check if image is in a correct formats and extensions if item_image and allowed_file(item_image.filename, allowed_image_extension): item_image_extension = item_image.filename.split(".")[-1] item_image_name[0] = random_filename(item_image_extension) image_exist = True # Generate a random name for image safely while image_exist: try: image = open( os.path.join(__root_directory__ + "/resources/image", item_image_name[0]), "r", ) image.close() item_image_name[0] = random_filename(item_image_extension) except BaseException: image_exist = False # Save the new image item_image.save( os.path.join(__root_directory__ + "/resources/image", item_image_name[0])) # Make add action on database and check if passed correctly if act.add_item( name=request.form.get("name", ""), description=request.form.get("description", ""), image=(url_for("resources", filename="image/%s" % item_image_name[0]) if act.not_empty(item_image_name[0]) else ""), category_id=request.form.get("category", ""), user_id=g.USER.id, ): flash( Markup("The item has been added successfully. " 'Go to your <a href="/me">profile</a>.')) else: flash(Markup("An error occurred adding the item.")) return redirect(request.args.get("next", ""))
def profileNested(pointer): try: # Fetch the data from database user = act.user(pointer=pointer) return render_template( "profile.html", categories=act.categories(user_id=user.id), items=act.items(For="user", pointer=user.id), user=user, ) except BaseException: return redirect(url_for("notFound"))
def api_v1_categories(): category_id = request.args.get("id", "") view_type = request.args.get("view", "") if view_type == "full": view_properity = "serialize" else: view_properity = "mini_serialize" if category_id: try: category = act.category(id=category_id) return jsonify(getattr(category, view_properity)) except BaseException: return jsonify(error="NOT FOUND"), 404 else: category_owner = request.args.get("for", "all") if category_owner == "all": return jsonify(all_categories=[ getattr(category, view_properity) for category in act.all_categories() ]) elif category_owner == "me": return jsonify(my_categories=[ getattr(category, view_properity) for category in act.categories(user_id=g.USER.id) ]) else: try: categories = act.categories(user_id=category_owner) return jsonify(user_categories=[ getattr(category, view_properity) for category in categories ]) except BaseException: return jsonify(error="NOT FOUND"), 404
def profile(username): try: try: # Check that it's not a user id # to make sure it's only allowed # to pass usernames after the root directory int(username) return redirect(url_for("notFound")) except BaseException: # Fetch the data from database user = act.user(pointer=username) return render_template( "profile.html", categories=act.categories(user_id=user.id), items=act.items(For="user", pointer=user.id), user=user, ) except BaseException: return redirect(url_for("notFound"))
def editItem(item_id): try: # Fetch the data from database item = act.item(id=item_id) # Check the authority of the logged-in user if item.user_id == g.USER.id: if request.method == "GET": return render_template( "edit_item.html", item=item, your_categories=act.categories(user_id=g.USER.id), others_categories=act.all_categories(), ) elif request.method == "POST": allowed_image_extension = {"png", "jpg", "jpeg", "gif"} item_image = request.files["image"] item_image_name = [""] # Check if image is in a correct formats and extensions if item_image and allowed_file(item_image.filename, allowed_image_extension): # Check if image is already exist # and remove it to replace it with the new one if item.image: try: os.remove( os.path.join(__root_directory__, item.image[1:])) except BaseException: pass item_image_extension = item_image.filename.split(".")[-1] item_image_name[0] = random_filename(item_image_extension) image_exist = True # Generate a random name for image safely while image_exist: try: image = open( os.path.join( __root_directory__ + "/resources/image", item_image_name[0]), "r", ) image.close() item_image_name[0] = random_filename( item_image_extension) except BaseException: image_exist = False # Save the new image item_image.save( os.path.join(__root_directory__ + "/resources/image", item_image_name[0])) # Make edit action on database and check if passed correctly if act.edit_item( item=item, name=request.form.get("name", ""), description=request.form.get("description", ""), image=(url_for( "resources", filename="image/%s" % item_image_name[0], ) if act.not_empty(item_image_name[0]) else ""), category_id=request.form.get("category", ""), ): flash( Markup("""The item has been edited successfully. \ Go to your <a href="/me">profile</a>.""")) else: flash(Markup("An error occurred editing your item.")) return redirect(request.args.get("next", "")) except BaseException: pass return redirect(url_for("notFound"))