diff --git a/imaginate_api/app.py b/imaginate_api/app.py index 0acbd5f..ca3ae47 100644 --- a/imaginate_api/app.py +++ b/imaginate_api/app.py @@ -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): @@ -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 diff --git a/imaginate_api/templates/verification_portal.html b/imaginate_api/templates/verification_portal.html index 2b20c15..c6ea8ec 100644 --- a/imaginate_api/templates/verification_portal.html +++ b/imaginate_api/templates/verification_portal.html @@ -5,20 +5,20 @@ Imaginate Verification @@ -39,7 +39,6 @@ -

{{obj_data}}

{% else %}

no images needing verification found

{% endif %}