Skip to content

Commit 3071883

Browse files
committed
prueba
1 parent 08647fb commit 3071883

File tree

7 files changed

+164
-18
lines changed

7 files changed

+164
-18
lines changed

app.py

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,19 @@
3232
migrate = Migrate(app, db)
3333

3434
# 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)
3642

3743
@app.route('/', methods=['GET'])
3844
def index():
3945
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)
4248

4349
@app.route('/<int:id>', methods=['GET'])
4450
def details(id):
@@ -114,6 +120,49 @@ def star_rating(id):
114120

115121
return dict(star_rating=star_rating)
116122

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+
117166
@app.route('/favicon.ico')
118167
def favicon():
119168
return send_from_directory(os.path.join(app.root_path, 'static'),

azureproject/production.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import os
22

3-
# DATABASE_URI = 'postgresql+psycopg2://{dbuser}:{dbpass}@{dbhost}/{dbname}'.format(
4-
# dbuser=os.getenv('AZURE_POSTGRESQL_USER'),
5-
# dbpass=os.getenv('AZURE_POSTGRESQL_PASSWORD'),
6-
# dbhost=os.getenv('AZURE_POSTGRESQL_HOST'),
7-
# dbname=os.getenv('AZURE_POSTGRESQL_NAME')
8-
# )
3+
DATABASE_URI = 'postgresql+psycopg2://{dbuser}:{dbpass}@{dbhost}/{dbname}'.format(
4+
dbuser=os.getenv('AZURE_POSTGRESQL_USER'),
5+
dbpass=os.getenv('AZURE_POSTGRESQL_PASSWORD'),
6+
dbhost=os.getenv('AZURE_POSTGRESQL_HOST'),
7+
dbname=os.getenv('AZURE_POSTGRESQL_NAME')
8+
)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""a
2+
3+
Revision ID: 2b81d0790f10
4+
Revises: f7006424e5cd
5+
Create Date: 2025-05-08 21:56:08.485113
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
from sqlalchemy.dialects import postgresql
11+
12+
# revision identifiers, used by Alembic.
13+
revision = '2b81d0790f10'
14+
down_revision = 'f7006424e5cd'
15+
branch_labels = None
16+
depends_on = None
17+
18+
19+
def upgrade():
20+
# ### commands auto generated by Alembic - please adjust! ###
21+
op.drop_table('image_data')
22+
# ### end Alembic commands ###
23+
24+
25+
def downgrade():
26+
# ### commands auto generated by Alembic - please adjust! ###
27+
op.create_table('image_data',
28+
sa.Column('id', sa.INTEGER(), autoincrement=True, nullable=False),
29+
sa.Column('filename', sa.VARCHAR(length=255), autoincrement=False, nullable=False),
30+
sa.Column('pixel_red', sa.INTEGER(), autoincrement=False, nullable=False),
31+
sa.Column('pixel_green', sa.INTEGER(), autoincrement=False, nullable=False),
32+
sa.Column('pixel_blue', sa.INTEGER(), autoincrement=False, nullable=False),
33+
sa.Column('username', sa.VARCHAR(length=100), autoincrement=False, nullable=False),
34+
sa.Column('upload_time', postgresql.TIMESTAMP(), autoincrement=False, nullable=False),
35+
sa.PrimaryKeyConstraint('id', name='image_data_pkey')
36+
)
37+
# ### end Alembic commands ###
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""b
2+
3+
Revision ID: e95f32cb7145
4+
Revises: 2b81d0790f10
5+
Create Date: 2025-05-08 22:07:14.980769
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
11+
12+
# revision identifiers, used by Alembic.
13+
revision = 'e95f32cb7145'
14+
down_revision = '2b81d0790f10'
15+
branch_labels = None
16+
depends_on = None
17+
18+
19+
def upgrade():
20+
# ### commands auto generated by Alembic - please adjust! ###
21+
op.create_table('image_data',
22+
sa.Column('id', sa.Integer(), nullable=False),
23+
sa.Column('filename', sa.String(length=255), nullable=False),
24+
sa.Column('pixel_red', sa.Integer(), nullable=False),
25+
sa.Column('pixel_green', sa.Integer(), nullable=False),
26+
sa.Column('pixel_blue', sa.Integer(), nullable=False),
27+
sa.Column('username', sa.String(length=100), nullable=False),
28+
sa.Column('upload_time', sa.DateTime(), nullable=False),
29+
sa.PrimaryKeyConstraint('id')
30+
)
31+
# ### end Alembic commands ###
32+
33+
34+
def downgrade():
35+
# ### commands auto generated by Alembic - please adjust! ###
36+
op.drop_table('image_data')
37+
# ### end Alembic commands ###

migrations/versions/d0c7b8e4b57c_initial_migration.py renamed to migrations/versions/f7006424e5cd_initial_migration.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Initial migration.
22
3-
Revision ID: d0c7b8e4b57c
3+
Revision ID: f7006424e5cd
44
Revises:
55
Create Date: 2022-11-08 17:00:02.151921
66
@@ -10,7 +10,7 @@
1010

1111

1212
# revision identifiers, used by Alembic.
13-
revision = 'd0c7b8e4b57c'
13+
revision = 'f7006424e5cd'
1414
down_revision = None
1515
branch_labels = None
1616
depends_on = None

models.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,17 @@ def validate_rating(self, key, value):
3030

3131
def __str__(self):
3232
return f"{self.user_name}: {self.review_date:%x}"
33+
34+
class ImageData(db.Model):
35+
__tablename__ = 'image_data'
36+
37+
id = Column(Integer, primary_key=True)
38+
filename = Column(String(255), nullable=False)
39+
pixel_red = Column(Integer, nullable=False)
40+
pixel_green = Column(Integer, nullable=False) #
41+
pixel_blue = Column(Integer, nullable=False)
42+
username = Column(String(100), nullable=False)
43+
upload_time = Column(DateTime, nullable=False, default=lambda: datetime.now(timezone.utc))
44+
45+
def __repr__(self):
46+
return f"<ImageData {self.filename} by {self.username} at {self.upload_time}>"

templates/index.html

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,26 @@ <h1>Restaurants</h1>
4747
<table class="table">
4848
<thead>
4949
<tr>
50-
<th>Name</th>
51-
<th>Rating</th>
52-
<th class="text-end">Details</th>
50+
<th>Usuario</th>
51+
<th>Nombre imagen</th>
52+
<th>Rojo</th>
53+
<th>verde</th>
54+
<th>azul</th>
55+
<th>fecha</th>
5356
</tr>
5457
</thead>
5558
<tbody>
56-
{% for restaurant in restaurants %}
59+
{% for image in images %}
5760
<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+
<td>{{ image.username }}</td>
62+
<td>{{ image.filename }}</td>
63+
<td>{{ image.pixel_red }}</td>
64+
<td>{{ image.pixel_green }}</td>
65+
<td>{{ image.pixel_blue }}</td>
66+
<td>{{ image.upload_time }}</td>
67+
68+
69+
6170
</tr>
6271
{% endfor %}
6372
</tbody>

0 commit comments

Comments
 (0)