Skip to content

Commit e42f297

Browse files
committed
eee hola
1 parent d1ad5bc commit e42f297

File tree

5 files changed

+117
-134
lines changed

5 files changed

+117
-134
lines changed

app.py

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
import os
22
from 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
54
from flask_migrate import Migrate
65
from flask_sqlalchemy import SQLAlchemy
76
from flask_wtf.csrf import CSRFProtect
87

9-
# This is the main entry point for the application.
8+
# Main application
109
app = Flask(__name__, static_folder='static')
1110
csrf = CSRFProtect(app)
1211

13-
# WEBSITE_HOSTNAME exists only in production environment
12+
# Configuración según entorno
1413
if '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')
1816
else:
19-
# production
2017
print("Loading config.production.")
2118
app.config.from_object('azureproject.production')
2219

@@ -25,14 +22,12 @@
2522
SQLALCHEMY_TRACK_MODIFICATIONS=False,
2623
)
2724

28-
# Initialize the database connection
25+
# Inicializar base de datos
2926
db = SQLAlchemy(app)
30-
31-
# Enable Flask-Migrate commands "flask db init/migrate/upgrade" to work
3227
migrate = 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'])
3833
def 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):
10193
def 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')
118130
def favicon():
119131
return send_from_directory(os.path.join(app.root_path, 'static'),

migrations/versions/d0c7b8e4b57c_initial_migration.py

Lines changed: 0 additions & 45 deletions
This file was deleted.

models.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from sqlalchemy import Column, DateTime, ForeignKey, Integer, String
22
from sqlalchemy.orm import validates
3+
from datetime import datetime
34

45
from app import db
56

@@ -30,3 +31,19 @@ def validate_rating(self, key, value):
3031

3132
def __str__(self):
3233
return f"{self.user_name}: {self.review_date:%x}"
34+
35+
36+
37+
class ImageData(db.Model):
38+
__tablename__ = 'image_data'
39+
40+
id = Column(Integer, primary_key=True)
41+
filename = Column(String(255), nullable=False)
42+
pixel_red = Column(String, nullable=False) # Ej: "1234"
43+
pixel_green = Column(String, nullable=False) # Ej: "5678"
44+
pixel_blue = Column(String, nullable=False) # Ej: "9101"
45+
username = Column(String(100), nullable=False)
46+
upload_time = Column(DateTime, nullable=False, default=datetime.utcnow)
47+
48+
def __repr__(self):
49+
return f"<ImageData {self.filename} by {self.username} at {self.upload_time}>"

templates/index.html

Lines changed: 50 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,51 @@
1-
{% extends "base.html" %}
2-
{% block title %}Restaurant List{% endblock %}
3-
{% block head %}
4-
{{ super() }}
1+
<!DOCTYPE html>
2+
<html lang="es">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Tabla de Imágenes Subidas</title>
56
<style>
6-
body {
7-
min-height: 75rem;
8-
padding-top: 4.5rem;
9-
}
10-
11-
.score {
12-
display: block;
13-
font-size: 16px;
14-
position: relative;
15-
overflow: hidden;
16-
}
17-
18-
.score-wrap {
19-
display: inline-block;
20-
position: relative;
21-
height: 19px;
22-
}
23-
24-
.score .stars-active {
25-
color: #EEBD01;
26-
position: relative;
27-
z-index: 10;
28-
display: inline-block;
29-
overflow: hidden;
30-
white-space: nowrap;
31-
}
32-
33-
.score .stars-inactive {
34-
color: grey;
35-
position: absolute;
36-
top: 0;
37-
left: 0;
38-
-webkit-text-stroke: initial;
39-
/* overflow: hidden; */
40-
}
41-
</style>
42-
{% endblock %}
43-
{% block content %}
44-
<h1>Restaurants</h1>
45-
46-
{% if True %}
47-
<table class="table">
48-
<thead>
49-
<tr>
50-
<th>Name</th>
51-
<th>Rating</th>
52-
<th class="text-end">Details</th>
53-
</tr>
54-
</thead>
55-
<tbody>
56-
{% for restaurant in restaurants %}
57-
<tr>
58-
<td>{{ restaurant.name }}</td>
59-
<td>{% include "star_rating.html" %}</td>
60-
<td class="text-end"><a href="{{ url_for('details', id=restaurant.id) }}" class="btn btn-sm btn-primary">Details</a></td>
61-
</tr>
62-
{% endfor %}
63-
</tbody>
64-
</table>
65-
{% else %}
66-
<p>No restaurants exist. Select Add new restaurant to add one.</p>
67-
{% endif %}
68-
69-
<div class="d-flex justify-content-end">
70-
<a href="{{ url_for('create_restaurant') }}" class="btn btn-success px-4 gap-3">Add new restaurant</a>
71-
</div>
72-
{% endblock %}
7+
table {
8+
border-collapse: collapse;
9+
width: 90%;
10+
margin: auto;
11+
}
12+
th, td {
13+
border: 1px solid #ccc;
14+
padding: 8px;
15+
text-align: center;
16+
}
17+
th {
18+
background-color: #f3f3f3;
19+
}
20+
</style>
21+
</head>
22+
<body>
23+
<h1 style="text-align:center;">Imágenes Subidas</h1>
24+
<table>
25+
<thead>
26+
<tr>
27+
<th>ID</th>
28+
<th>Archivo</th>
29+
<th>Usuario</th>
30+
<th>Fecha</th>
31+
<th>Nº Pixeles Rojos</th>
32+
<th>Nº Pixeles Verdes</th>
33+
<th>Nº Pixeles Azules</th>
34+
</tr>
35+
</thead>
36+
<tbody>
37+
{% for img in images %}
38+
<tr>
39+
<td>{{ img.id }}</td>
40+
<td>{{ img.filename }}</td>
41+
<td>{{ img.username }}</td>
42+
<td>{{ img.upload_time }}</td>
43+
<td>{{ img.pixel_rojo }}</td>
44+
<td>{{ img.pixel_verde }}</td>
45+
<td>{{ img.pixel_azul }}</td>
46+
</tr>
47+
{% endfor %}
48+
</tbody>
49+
</table>
50+
</body>
51+
</html>

templates/uploads.html

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Uploads</title>
6+
</head>
7+
<body>
8+
<h1>Uploaded Images</h1>
9+
{% for upload in uploads %}
10+
<div>
11+
<h2>{{ upload.file_name }}</h2>
12+
<p>Uploaded by: {{ upload.user_name }}</p>
13+
<p>Red Pixels: {{ upload.red_pixels }}</p>
14+
<p>Green Pixels: {{ upload.green_pixels }}</p>
15+
<p>Blue Pixels: {{ upload.blue_pixels }}</p>
16+
<img src="{{ url_for('uploaded_file', filename=upload.file_name) }}" alt="Uploaded Image">
17+
</div>
18+
{% endfor %}
19+
</body>
20+
</html>

0 commit comments

Comments
 (0)