11import os
22from datetime import datetime
3-
4- from flask import Flask , redirect , render_template , request , send_from_directory , url_for
3+ from flask import Flask , redirect , render_template , request , send_from_directory , url_for , jsonify
54from flask_migrate import Migrate
65from flask_sqlalchemy import SQLAlchemy
76from flask_wtf .csrf import CSRFProtect
87
9- # This is the main entry point for the application.
8+ # Main application
109app = Flask (__name__ , static_folder = 'static' )
1110csrf = CSRFProtect (app )
1211
13- # WEBSITE_HOSTNAME exists only in production environment
12+ # Configuración según entorno
1413if 'WEBSITE_HOSTNAME' not in os .environ :
15- # local development, where we'll use environment variables
1614 print ("Loading config.development and environment variables from .env file." )
1715 app .config .from_object ('azureproject.development' )
1816else :
19- # production
2017 print ("Loading config.production." )
2118 app .config .from_object ('azureproject.production' )
2219
2522 SQLALCHEMY_TRACK_MODIFICATIONS = False ,
2623)
2724
28- # Initialize the database connection
25+ # Inicializar base de datos
2926db = SQLAlchemy (app )
30-
31- # Enable Flask-Migrate commands "flask db init/migrate/upgrade" to work
3227migrate = Migrate (app , db )
3328
34- # The import must be done after db initialization due to circular import issue
35- from models import Restaurant , Review
29+ # Importar modelos después de inicializar db
30+ from models import Restaurant , Review , ImageData
3631
3732@app .route ('/' , methods = ['GET' ])
3833def index ():
@@ -59,7 +54,6 @@ def add_restaurant():
5954 street_address = request .values .get ('street_address' )
6055 description = request .values .get ('description' )
6156 except (KeyError ):
62- # Redisplay the question voting form.
6357 return render_template ('add_restaurant.html' , {
6458 'error_message' : "You must include a restaurant name, address, and description" ,
6559 })
@@ -70,7 +64,6 @@ def add_restaurant():
7064 restaurant .description = description
7165 db .session .add (restaurant )
7266 db .session .commit ()
73-
7467 return redirect (url_for ('details' , id = restaurant .id ))
7568
7669@app .route ('/review/<int:id>' , methods = ['POST' ])
@@ -81,7 +74,6 @@ def add_review(id):
8174 rating = request .values .get ('rating' )
8275 review_text = request .values .get ('review_text' )
8376 except (KeyError ):
84- #Redisplay the question voting form.
8577 return render_template ('add_review.html' , {
8678 'error_message' : "Error adding review" ,
8779 })
@@ -101,19 +93,39 @@ def add_review(id):
10193def utility_processor ():
10294 def star_rating (id ):
10395 reviews = Review .query .where (Review .restaurant == id )
104-
10596 ratings = []
10697 review_count = 0
10798 for review in reviews :
108- ratings += [ review .rating ]
99+ ratings . append ( review .rating )
109100 review_count += 1
110-
111101 avg_rating = sum (ratings ) / len (ratings ) if ratings else 0
112102 stars_percent = round ((avg_rating / 5.0 ) * 100 ) if review_count > 0 else 0
113103 return {'avg_rating' : avg_rating , 'review_count' : review_count , 'stars_percent' : stars_percent }
114-
115104 return dict (star_rating = star_rating )
116105
106+ @app .route ('/images' , methods = ['GET' ])
107+ def image_table ():
108+ print ('Request for image table page received' )
109+ images = ImageData .query .order_by (ImageData .upload_time .desc ()).all ()
110+ return render_template ('image_table.html' , images = images )
111+
112+ @app .route ('/api/images' , methods = ['GET' ])
113+ def image_json ():
114+ images = ImageData .query .order_by (ImageData .upload_time .desc ()).all ()
115+ data = [
116+ {
117+ "id" : img .id ,
118+ "filename" : img .filename ,
119+ "username" : img .username ,
120+ "upload_time" : img .upload_time .isoformat (),
121+ "pixel_rojo" : img .pixel_rojo ,
122+ "pixel_verde" : img .pixel_verde ,
123+ "pixel_azul" : img .pixel_azul
124+ }
125+ for img in images
126+ ]
127+ return jsonify (data )
128+
117129@app .route ('/favicon.ico' )
118130def favicon ():
119131 return send_from_directory (os .path .join (app .root_path , 'static' ),
0 commit comments