-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
28f653e
commit 93b2ea3
Showing
50 changed files
with
2,344 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
from flask import Flask, request, render_template, redirect, url_for | ||
from utils.validations import validate_form | ||
from werkzeug.utils import secure_filename | ||
from database.db import register_crafter | ||
import hashlib, filetype, os, uuid, database.db as db | ||
|
||
UPLOAD_FOLDER = "static/uploads" | ||
|
||
app = Flask(__name__) | ||
|
||
app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER | ||
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 | ||
|
||
# Default route | ||
@app.route("/") | ||
def index(): | ||
show = request.args.get("show") | ||
return render_template("index.html", show=show) | ||
|
||
# Transitions | ||
@app.route("/agregar-hincha") | ||
def agregar_hincha(): | ||
return render_template("agregar-hincha.html") | ||
|
||
@app.route("/agregar-artesanos") | ||
def agregar_artesano(): | ||
return render_template("agregar-artesano.html") | ||
|
||
@app.route("/ver-hinchas") | ||
def ver_hinchas(): | ||
return render_template("ver-hinchas.html") | ||
|
||
# Artesanos | ||
@app.route("/registrar-artesano", methods=["GET"]) | ||
def registrar_artesano(): | ||
errors = request.args.getlist("errors") | ||
return render_template("agregar-artesano.html", errors=errors) | ||
|
||
@app.route("/informacion-hincha") | ||
def informacion_hincha(): | ||
return render_template("informacion-hincha.html") | ||
|
||
@app.route("/registrar-artesano/errors", methods=["POST"]) | ||
def show_errors(): | ||
# Request form | ||
artesano_form = request.form | ||
artesano_imgs = request.files.getlist("images") | ||
|
||
# Validations | ||
errors = validate_form(artesano_form, artesano_imgs) | ||
if errors == []: | ||
imgs = [] | ||
# Encrypt imgs filename | ||
for img in artesano_imgs: | ||
_filename = hashlib.sha256( | ||
secure_filename(img.filename).encode("UTF-8") | ||
).hexdigest() | ||
_extension = filetype.guess(img).extension | ||
img_filename = f"{_filename}_{str(uuid.uuid4())}.{_extension}" | ||
img_path = os.path.join(app.config["UPLOAD_FOLDER"], img_filename).replace("\\", "/") | ||
img.save(img_path) | ||
imgs.append((app.config["UPLOAD_FOLDER"], img_filename)) | ||
register_crafter(artesano_form, imgs) | ||
return redirect(url_for("index", show=True)) | ||
return redirect(url_for("registrar_artesano", errors=errors)) | ||
|
||
@app.route("/ver-artesanos", methods=["GET", "POST"]) | ||
def ver_artesanos(): | ||
if request.method == "GET": | ||
page = request.args.get("page") | ||
pagina = request.args.get("pagina") | ||
# Special cases | ||
if (int(db.get_artesanos_count()[0][0])) == 0: | ||
return render_template("ver-artesanos.html", page=1, max_page=1) | ||
if not page: | ||
page = 1 | ||
if pagina: | ||
page = pagina | ||
page = int(page) | ||
|
||
# Cycle through data | ||
if page > db.get_max_page()[0]: | ||
return redirect(url_for("ver_artesanos", page=1)) | ||
elif page <= 0: | ||
return redirect(url_for("ver_artesanos", page=db.get_max_page())) | ||
|
||
# Format data | ||
data = [] | ||
for artesano in db.get_artesanos(page, page_size=5): | ||
_id, commune_id, _, name, _, phone = artesano | ||
commune = db.get_commune_by_id(commune_id) | ||
types = db.get_types(_id) | ||
img_path = db.get_imgs(_id) | ||
|
||
data.append({ | ||
"id": _id, | ||
"name": name, | ||
"phone": phone, | ||
"commune": commune[0], | ||
"crafts": types, | ||
"photo": img_path[0][0] + "/" + img_path[0][1] | ||
}) | ||
return render_template("ver-artesanos.html", data=data, page=page, max_page=db.get_max_page()[0]) | ||
|
||
# Update current page | ||
elif request.method == "POST": | ||
show_page = request.form | ||
page = show_page.get("page") | ||
if not page: | ||
page = 1 | ||
page = int(page) | ||
if show_page.get("next"): | ||
page += 1 | ||
elif show_page.get("prev"): | ||
page -= 1 | ||
return redirect(url_for("ver_artesanos", page=page)) | ||
|
||
@app.route("/informacion-artesano", methods=["GET", "POST"]) | ||
def informacion_artesano(): | ||
if request.method == "GET": | ||
# Format data | ||
artesano_id = request.args.get("artesano_id") | ||
pagina = request.args.get("pagina") | ||
artesano = db.get_artesano_by_id(artesano_id) | ||
region = db.get_region_by_artesano_id(artesano_id) | ||
types = db.get_types(artesano_id) | ||
imgs = db.get_imgs(artesano_id) | ||
data = { | ||
"region": region[0], | ||
"commune": artesano[1], | ||
"types": types, | ||
"desc": artesano[2], | ||
"imgs": imgs, | ||
"name": artesano[3], | ||
"email": artesano[4], | ||
"phone": artesano[5] | ||
} | ||
return render_template("informacion-artesano.html", data=data, pagina=pagina) | ||
|
||
elif request.method == "POST": | ||
artesano_id = request.form.get("artesano-info") | ||
pagina = request.form.get("pagina") | ||
return redirect(url_for("informacion_artesano", artesano_id=artesano_id, pagina=pagina)) | ||
|
||
if __name__ == "__main__": | ||
app.run(debug=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import pymysql | ||
|
||
DB_NAME = "tarea2" | ||
DB_USERNAME = "cc5002" | ||
DB_PASSWORD = "programacionweb" | ||
DB_HOST = "localhost" | ||
DB_PORT = 3306 | ||
DB_CHARSET = "utf8" | ||
|
||
def get_conn(): | ||
conn = pymysql.connect( | ||
db=DB_NAME, | ||
user=DB_USERNAME, | ||
passwd=DB_PASSWORD, | ||
host=DB_HOST, | ||
port=DB_PORT, | ||
charset=DB_CHARSET | ||
) | ||
return conn | ||
|
||
def get_artesanos_count(): | ||
conn = get_conn() | ||
cursor = conn.cursor() | ||
cursor.execute("SELECT COUNT(*) FROM artesano") | ||
count = cursor.fetchall() | ||
return count | ||
|
||
def get_artesanos(page, page_size): | ||
conn = get_conn() | ||
cursor = conn.cursor() | ||
cursor.execute("SELECT * FROM artesano ORDER BY id DESC LIMIT %s, %s;", ((int(page) - 1) * 5, page_size,)) | ||
artesanos = cursor.fetchall() | ||
return artesanos | ||
|
||
def get_artesano_by_id(_id): | ||
conn = get_conn() | ||
cursor = conn.cursor() | ||
cursor.execute("SELECT artesano.id, comuna.nombre, descripcion_artesania, artesano.nombre, email, celular FROM artesano, comuna WHERE artesano.comuna_id=comuna.id AND artesano.id=%s;", (_id, )) | ||
artesano = cursor.fetchone() | ||
return artesano | ||
|
||
def get_region_by_artesano_id(_id): | ||
conn = get_conn() | ||
cursor = conn.cursor() | ||
cursor.execute("SELECT RE.nombre FROM artesano AT, comuna CO, region RE WHERE AT.id=%s AND AT.comuna_id=CO.id AND CO.region_id=RE.id;", (_id, )) | ||
region = cursor.fetchone() | ||
return region | ||
|
||
def get_max_page(): | ||
conn = get_conn() | ||
cursor = conn.cursor() | ||
cursor.execute("SELECT (COUNT(*)+4) DIV 5 FROM artesano;") | ||
amount = cursor.fetchone() | ||
return amount | ||
|
||
def get_regions(): | ||
conn = get_conn() | ||
cursor = conn.cursor() | ||
cursor.execute("SELECT * FROM region;") | ||
regions = cursor.fetchall() | ||
return regions | ||
|
||
def get_communes_per_region(region_id): | ||
conn = get_conn() | ||
cursor = conn.cursor() | ||
cursor.execute("SELECT nombre FROM comuna WHERE region_id=%s;", (region_id,)) | ||
communes = cursor.fetchall() | ||
return communes | ||
|
||
def get_commune_id_by_name(name): | ||
conn = get_conn() | ||
cursor = conn.cursor() | ||
cursor.execute("SELECT id FROM comuna WHERE nombre=%s;", (name,)) | ||
commune_id = cursor.fetchone() | ||
return commune_id | ||
|
||
def get_commune_by_id(_id): | ||
conn = get_conn() | ||
cursor = conn.cursor() | ||
cursor.execute("SELECT nombre FROM comuna WHERE id=%s;", (_id,)) | ||
commune = cursor.fetchone() | ||
return commune | ||
|
||
def get_types(_id): | ||
conn = get_conn() | ||
cursor = conn.cursor() | ||
cursor.execute("SELECT TA.nombre FROM tipo_artesania TA, artesano_tipo AT WHERE AT.tipo_artesania_id=TA.id AND AT.artesano_id=%s;", (_id,)) | ||
types = cursor.fetchall() | ||
return types | ||
|
||
def get_imgs(_id): | ||
conn = get_conn() | ||
cursor = conn.cursor() | ||
cursor.execute("SELECT ruta_archivo, nombre_archivo FROM foto WHERE artesano_id=%s;", (_id,)) | ||
imgs = cursor.fetchall() | ||
return imgs | ||
|
||
def get_imgs_path(_id): | ||
conn = get_conn() | ||
cursor = conn.cursor() | ||
cursor.execute("SELECT ruta_archivo FROM foto WHERE artesano_id=%s;", (_id,)) | ||
paths = cursor.fetchall() | ||
return paths | ||
|
||
def create_artesano(commune_name, desc, name, email, phone): | ||
conn = get_conn() | ||
cursor = conn.cursor() | ||
commune_id = get_commune_id_by_name(commune_name) | ||
cursor.execute("INSERT INTO artesano (comuna_id, descripcion_artesania, nombre, email, celular) VALUES (%s, %s, %s, %s, %s);", (commune_id, desc, name, email, phone)) | ||
conn.commit() | ||
cursor.execute("SELECT LAST_INSERT_ID();") | ||
return cursor.fetchone() | ||
|
||
def create_artesano_tipo(id, types): | ||
conn = get_conn() | ||
cursor = conn.cursor() | ||
types_ids = [] | ||
for type in types: | ||
cursor.execute("SELECT id FROM tipo_artesania WHERE nombre=%s;", (type)) | ||
types_ids += cursor.fetchone() | ||
for type_id in types_ids: | ||
cursor.execute("INSERT INTO artesano_tipo (artesano_id, tipo_artesania_id) VALUES (%s, %s);", (id, type_id)) | ||
conn.commit() | ||
|
||
def create_imgs(imgs, id): | ||
conn = get_conn() | ||
cursor = conn.cursor() | ||
for img in imgs: | ||
cursor.execute("INSERT INTO foto (ruta_archivo, nombre_archivo, artesano_id) VALUES (%s, %s, %s);", (img[0], img[1], id)) | ||
conn.commit() | ||
|
||
def register_crafter(form, imgs): | ||
name = form.get("name") | ||
email = form.get("email") | ||
phone = form.get("phone") | ||
crafts = form.getlist("craft") | ||
commune = form.get("commune") | ||
desc = form.get("desc") | ||
artesano_id = create_artesano(commune, desc, name, email, phone) | ||
create_artesano_tipo(artesano_id, crafts) | ||
create_imgs(imgs, artesano_id) |
Oops, something went wrong.