Skip to content

Commit

Permalink
final polishing and applying suggestions #8
Browse files Browse the repository at this point in the history
  • Loading branch information
zachale committed Jul 14, 2024
1 parent 257277b commit 0357e5d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 25 deletions.
36 changes: 25 additions & 11 deletions imaginate_api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@
# Other
from dotenv import load_dotenv
from flask import Flask, abort, json, jsonify, make_response, render_template, request
from base64 import b64encode
from bson.objectid import ObjectId


# MongoDB related
from pymongo import MongoClient
from werkzeug.exceptions import HTTPException

# Constants
from schemas.image_info import ImageStatus


# Helper function to get boolean
def str_to_bool(string: str):
Expand Down Expand Up @@ -186,31 +192,39 @@ def delete_image(id):

#Image Verification Routes

# GET /image/verification-portal: returns HTML page for image verification
@app.route("/image/verification-portal", methods=["GET"])
def verification_portal():
obj = list(db['fs.files'].aggregate([{ '$sample': { 'size': 1 } }]))[0] #TODO: MAKE IT BASED ON STATUS
print(obj)
obj = db['fs.files'].find_one({
'$or': [
{ 'status':'unverified' },
{ 'status': { '$exists': False } }
]
})
if obj:
grid_out = fs.find_one({"_id":obj['_id']})
grid_out = fs.find_one({ "_id": obj['_id'] })
data = grid_out.read()
base64_data = base64.b64encode(data).decode('ascii');
return render_template('verification_portal.html', id=obj['_id'], img_found=True, img_src=base64_data, obj_data=obj)
base64_data = b64encode(data).decode('ascii');
return render_template('verification_portal.html', id=obj['_id'], img_found=True, img_src=base64_data)
return render_template('verification_portal.html', img_found=False)

# POST /image/update-status: used to mark images as verified / rejected
@app.route("/image/update-status", methods=["POST"])
def update_status():
status = request.form['status']
if status:
query_filter = { '_id':request.form['id'] }
query_filter = { '_id': ObjectId(request.form['id']) }
update_operation = { "$set" : { "status" : status } }
db['fs.files'].find_one_and_update(query_filter, update_operation)
response = db['fs.files'].find_one_and_update(query_filter, update_operation)
return {"message": "Status updated"}
else:
return "new status not recieved",400
return "status updated",200
abort(400, description="New status not received")


@app.route("/image/delete_rejected", methods=["DELETE"])
# DELETE /image/delete-rejected: used to delete all images marked as rejected, returns all deleted images
@app.route("/image/delete-rejected", methods=["DELETE"])
def delete_rejected():
filter = {"status":"rejected"}
filter = {"status": ImageStatus.REJECTED}
results = db["fs.files"].delete_many(filter)
return results, 200

Expand Down
27 changes: 13 additions & 14 deletions imaginate_api/templates/verification_portal.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Imaginate Verification</title>
<script>
function submitForm(event) {
event.preventDefault();
const form = event.target;
const formData = new FormData(form);
function submitForm(event) {
event.preventDefault();
const form = event.target;
const formData = new FormData(form);

fetch(form.action, {
method: form.method,
body: formData,
}).then(response => {
window.location.reload()
}).catch(error => {
console.error('Error:', error);
});
}
fetch(form.action, {
method: form.method,
body: formData,
}).then(response => {
window.location.reload()
}).catch(error => {
console.error('Error:', error);
});
}
</script>
</head>
<body>
Expand All @@ -39,7 +39,6 @@
</fieldset>
<button type="submit">Submit</button>
</form>
<p>{{obj_data}}</p>
{% else %}
<p>no images needing verification found</p>
{% endif %}
Expand Down

0 comments on commit 0357e5d

Please sign in to comment.