|
32 | 32 | migrate = Migrate(app, db) |
33 | 33 |
|
34 | 34 | # The import must be done after db initialization due to circular import issue |
35 | | -from models import Restaurant, Review |
| 35 | +from models import Restaurant, Review, ImageData |
| 36 | + |
| 37 | +#@app.route('/', methods=['GET']) |
| 38 | +#def index(): |
| 39 | +# print('Request for index page received') |
| 40 | +# restaurants = Restaurant.query.all() |
| 41 | +# return render_template('index.html', restaurants=restaurants) |
36 | 42 |
|
37 | 43 | @app.route('/', methods=['GET']) |
38 | 44 | def index(): |
39 | 45 | print('Request for index page received') |
40 | | - restaurants = Restaurant.query.all() |
41 | | - return render_template('index.html', restaurants=restaurants) |
| 46 | + images = ImageData.query.all() |
| 47 | + return render_template('index.html', images=images) |
42 | 48 |
|
43 | 49 | @app.route('/<int:id>', methods=['GET']) |
44 | 50 | def details(id): |
@@ -114,6 +120,49 @@ def star_rating(id): |
114 | 120 |
|
115 | 121 | return dict(star_rating=star_rating) |
116 | 122 |
|
| 123 | +@app.route('/images', methods=['GET']) |
| 124 | +def image_table(): |
| 125 | + print('Request for image table page received') |
| 126 | + images = ImageData.query.order_by(ImageData.upload_time.desc()).all() |
| 127 | + return render_template('index.html', images=images) |
| 128 | + |
| 129 | +@csrf.exempt |
| 130 | +@app.route('/upload_image', methods=['POST']) |
| 131 | +def upload_image(): |
| 132 | + print('Request to upload image received') |
| 133 | + if not request.is_json: |
| 134 | + return jsonify({"error": "Request must be JSON"}), 400 |
| 135 | + |
| 136 | + data = request.get_json() |
| 137 | + filename = data.get('filename') |
| 138 | + pixel_red = data.get('pixel_red') |
| 139 | + pixel_green = data.get('pixel_green') |
| 140 | + pixel_blue = data.get('pixel_blue') |
| 141 | + username = data.get('username') |
| 142 | + |
| 143 | + |
| 144 | + if not all([filename, pixel_red, pixel_green, pixel_blue, username]): |
| 145 | + return jsonify({"error": "All fields ('filename', 'pixel_red', 'pixel_green', 'pixel_blue', 'username') are required"}), 400 |
| 146 | + |
| 147 | + try: |
| 148 | + new_image = ImageData( |
| 149 | + filename=filename, |
| 150 | + pixel_red=pixel_red, |
| 151 | + pixel_green=pixel_green, |
| 152 | + pixel_blue=pixel_blue, |
| 153 | + username=username, |
| 154 | + upload_time=datetime.now(timezone.utc) |
| 155 | + |
| 156 | + ) |
| 157 | + #db.session.create_all() |
| 158 | + #db.session.commit() |
| 159 | + db.session.add(new_image) |
| 160 | + db.session.commit() |
| 161 | + return jsonify({"message": "Image uploaded successfully"}), 201 |
| 162 | + except Exception as e: |
| 163 | + db.session.rollback() |
| 164 | + return jsonify({"error": str(e)}), 500 |
| 165 | + |
117 | 166 | @app.route('/favicon.ico') |
118 | 167 | def favicon(): |
119 | 168 | return send_from_directory(os.path.join(app.root_path, 'static'), |
|
0 commit comments