roundabout,
created on Saturday, 14 September 2024, 13:01:52 (1726318912),
received on Saturday, 14 September 2024, 13:01:55 (1726318915)
Author identity: vlad <vlad.muntoiu@gmail.com>
a94b803e4b9bb3f7bd724afcc2fbecc1bed78a44
app.py
@@ -1621,3 +1621,122 @@ def api_gallery(id):
return flask.jsonify(json_gallery) @app.route("/api/gallery/<int:id>/edit", methods=["POST"]) def api_edit_gallery(id): gallery = db.session.get(Gallery, id) if gallery is None: flask.abort(404) current_user = db.session.get(User, flask.session.get("username")) if current_user is None: flask.abort(401) if current_user != gallery.owner and not current_user.admin: flask.abort(403) title = flask.request.json.get("title", gallery.title) description = flask.request.json.get("description", gallery.description) if not title: return flask.jsonify({"error": "Give a title"}), 400 if not description: description = "" gallery.title = title gallery.description = description db.session.commit() return flask.jsonify({"message": "Gallery updated successfully"}) @app.route("/api/new-gallery", methods=["POST"]) def api_new_gallery(): if "username" not in flask.session: return flask.jsonify({"error": "You must be logged in to create galleries"}), 401 title = flask.request.json.get("title") description = flask.request.json.get("description", "") if not title: return flask.jsonify({"error": "Give a title"}), 400 gallery = Gallery(title, description, db.session.get(User, flask.session["username"])) db.session.add(gallery) db.session.commit() return flask.jsonify({"message": "Gallery created successfully"}) @app.route("/api/gallery/<int:id>/add-picture", methods=["POST"]) def api_gallery_add_picture(id): gallery = db.session.get(Gallery, id) if gallery is None: flask.abort(404) if "username" not in flask.session: return flask.jsonify({"error": "You must be logged in to add pictures to galleries"}), 401 current_user = db.session.get(User, flask.session.get("username")) if flask.session["username"] != gallery.owner_name and not current_user.admin and not UserInGallery.query.filter_by(user=current_user, gallery=gallery).first(): return flask.jsonify({"error": "You do not have permission to add pictures to this gallery"}), 403 picture_id = flask.request.json.get("picture_id") try: picture_id = int(picture_id) except ValueError: return flask.jsonify({"error": "Invalid picture ID"}), 400 picture = db.session.get(PictureResource, picture_id) if picture is None: return flask.jsonify({"error": "The picture doesn't exist"}), 404 if PictureInGallery.query.filter_by(resource=picture, gallery=gallery).first(): return flask.jsonify({"error": "This picture is already in the gallery"}), 400 db.session.add(PictureInGallery(picture, gallery)) db.session.commit() return flask.jsonify({"message": "Picture added to gallery"}) @app.route("/api/gallery/<int:id>/remove-picture", methods=["POST"]) def api_gallery_remove_picture(id): gallery = db.session.get(Gallery, id) if gallery is None: flask.abort(404) if "username" not in flask.session: return flask.jsonify({"error": "You must be logged in to remove pictures from galleries"}), 401 current_user = db.session.get(User, flask.session.get("username")) if flask.session["username"] != gallery.owner_name and not current_user.admin and not UserInGallery.query.filter_by(user=current_user, gallery=gallery).first(): return flask.jsonify({"error": "You do not have permission to remove pictures from this gallery"}), 403 picture_id = flask.request.json.get("picture_id") try: picture_id = int(picture_id) except ValueError: return flask.jsonify({"error": "Invalid picture ID"}), 400 picture = db.session.get(PictureResource, picture_id) if picture is None: return flask.jsonify({"error": "The picture doesn't exist"}), 404 picture_in_gallery = PictureInGallery.query.filter_by(resource=picture, gallery=gallery).first() if picture_in_gallery is None: return flask.jsonify({"error": "This picture isn't in the gallery"}), 400 db.session.delete(picture_in_gallery) db.session.commit() return flask.jsonify({"message": "Picture removed from gallery"})