Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump flask from 2.2.2 to 2.2.5 in /seccion_30_todo #6

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
bbe3351
Clase 228. Creando nuestro proyecto
onedrako Nov 24, 2022
653d6b8
Clase 229. Abstracción de base de datos
onedrako Nov 24, 2022
b840715
Clase 230. Creando nuestro esquema de base de datos
onedrako Nov 24, 2022
7b759fd
Clase 232. Definiendo el blueprint
onedrako Nov 24, 2022
330d5b5
Clase 233. Creando el HTML base
onedrako Nov 24, 2022
d406220
Clase 234. Agregando estilos a la base
onedrako Nov 24, 2022
6888fac
Clase 235. Listando Correos
onedrako Nov 24, 2022
f120704
Clase 236. Agregando estilo a los correos listados
onedrako Nov 24, 2022
6af8990
Clase 237. Creando botones de navegación
onedrako Nov 24, 2022
abff24c
Clase 238. Agregando ruta para crear emails
onedrako Nov 24, 2022
a78884d
Clase 239. Agregando estilo al formulario de correo
onedrako Nov 24, 2022
383d1b5
Clase 240. Validando los datos del formulario.
onedrako Nov 24, 2022
ff6ee6f
Clase 241. Insertando valores
onedrako Nov 24, 2022
4c2d8c2
Clase 242. Enviando correos con sendgrid
onedrako Nov 24, 2022
9b6b0c8
Clase 243. Agregando búqueda a nuestra aplicación
onedrako Nov 24, 2022
ee496f7
Obteniendo los requerimentos para subir la app
onedrako Dec 27, 2022
3db0748
cambiando Procfile
onedrako Dec 27, 2022
f2ba9dc
regresando procfile
onedrako Dec 27, 2022
1f1ad9d
cambiando archivos
onedrako Dec 27, 2022
f88aab7
cambiando archivos
onedrako Dec 27, 2022
fc281fa
Agregado port
onedrako Dec 28, 2022
9faa4ab
Agregado requeriments.txt
onedrako Dec 28, 2022
006eaca
procfile con creta_app
onedrako Dec 28, 2022
98258be
add requeriments gunicorn
onedrako Dec 29, 2022
80b489f
Agregada seccion 34 mailer a la rama main
onedrako Mar 5, 2024
d088e47
Bump flask from 2.2.2 to 2.2.5 in /seccion_30_todo
dependabot[bot] Mar 5, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# FLASK_DATABASE_HOST=localhost
# FLASK_DATABASE_PASSWORD=chamaco95
# FLASK_DATABASE_USER=root
# FLASK_DATABASE=mailerapp
SECRET_KEY=misecreto
SENDGRID_API_KEY=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
[email protected]


FLASK_DATABASE_HOST=containers-us-west-52.railway.app
FLASK_DATABASE_PASSWORD=Dho8qTGt9KunCjm4xOZc
FLASK_DATABASE_USER=root
FLASK_DATABASE=railway
FLASK_DATABASE_PORT=7318
2 changes: 1 addition & 1 deletion seccion_30_todo/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
click==8.1.3
Flask==2.2.2
Flask==2.2.5
importlib-metadata==5.0.0
itsdangerous==2.1.2
Jinja2==3.1.2
Expand Down
8 changes: 8 additions & 0 deletions seccion_34_mailer/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
venv/
.env

*.pyc
__pycache__/
app/__pycache__/

instance
1 change: 1 addition & 0 deletions seccion_34_mailer/Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: flask init-db; gunicorn app:'create_app()'
24 changes: 24 additions & 0 deletions seccion_34_mailer/app/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import os

from flask import Flask


def create_app():
app = Flask(__name__)
app.config.from_mapping(
FROM_EMAIL=os.environ.get('FROM_EMAIL'),
SENDGRID_KEY=os.environ.get('SENDGRID_API_KEY'),
SECRET_KEY=os.environ.get('SECRET_KEY'),
DATABASE_HOST=os.environ.get('FLASK_DATABASE_HOST'),
DATABASE_PASSWORD=os.environ.get('FLASK_DATABASE_PASSWORD'),
DATABASE_USER=os.environ.get('FLASK_DATABASE_USER'),
DATABASE=os.environ.get('FLASK_DATABASE'),
DATABASE_PORT=os.environ.get('FLASK_DATABASE_PORT'),
)

from . import db
db.init_app(app)
from . import mail
app.register_blueprint(mail.bp)

return app
41 changes: 41 additions & 0 deletions seccion_34_mailer/app/db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import mysql.connector

import click
from flask import current_app, g
from flask.cli import with_appcontext
from .schema import instructions

def get_db():
if "db" not in g:
g.db = mysql.connector.connect(
host=current_app.config["DATABASE_HOST"],
user=current_app.config["DATABASE_USER"],
password=current_app.config["DATABASE_PASSWORD"],
database=current_app.config["DATABASE"],
port=current_app.config["DATABASE_PORT"],
)
g.c = g.db.cursor(dictionary=True)
return g.db, g.c

def close_db(e=None):
db = g.pop("db", None)

if db is not None:
db.close()

def init_db():
db, c = get_db()

for i in instructions:
c.execute(i)
db.commit()

@click.command("init-db")
@with_appcontext
def init_db_command():
init_db()
click.echo("Base de Datos inicializada")

def init_app(app):
app.teardown_appcontext(close_db)
app.cli.add_command(init_db_command)
63 changes: 63 additions & 0 deletions seccion_34_mailer/app/mail.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import sendgrid
from sendgrid.helpers.mail import *
from flask import (
Blueprint, render_template, request, flash, redirect, url_for, current_app
)

from app.db import get_db

bp = Blueprint('mail', __name__, url_prefix='/')


@bp.route("/", methods=('GET', 'POST'))
def index():
search = request.args.get('search')
db, c = get_db()
if search is None:
mails = c.execute('SELECT * FROM email')
else:
mails = c.execute(
'SELECT * FROM email WHERE content LIKE %s', ('%'+search+'%',))
mails = c.fetchall()
return render_template('mail/index.html', mails=mails)


@bp.route("/create", methods=('GET', 'POST'))
def create():
if request.method == 'POST':
email = request.form.get("email")
subject = request.form.get("subject")
content = request.form.get("content")
errors = []

if not email:
errors.append("Email es obligatorio.")
if not subject:
errors.append("Subject es obligatorio.")
if not content:
errors.append("Content es obligatorio.")

if len(errors) == 0:
send(email, subject, content)
db, c = get_db()
c.execute('INSERT INTO email (email, subject, content) VALUES (%s, %s, %s)',
(email, subject, content))
db.commit()

return redirect(url_for('mail.index'))
else:
for error in errors:
flash(error)

print(email, subject, content)
return render_template('mail/create.html')


def send(to, subject, content):
sg = sendgrid.SendGridAPIClient(api_key=current_app.config['SENDGRID_KEY'])
from_email = Email(current_app.config['FROM_EMAIL'])
to_email = To(to)
content = Content("text/plain", content)
mail = Mail(from_email, to_email, subject, content)
response = sg.client.mail.send.post(request_body=mail.get())
print(response)
11 changes: 11 additions & 0 deletions seccion_34_mailer/app/schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
instructions = [
"DROP TABLE IF EXISTS email;",
"""
CREATE TABLE email (
id INT PRIMARY KEY AUTO_INCREMENT,
email TEXT NOT NULL,
subject TEXT NOT NULL,
content TEXT NOT NULL
)
"""
]
117 changes: 117 additions & 0 deletions seccion_34_mailer/app/static/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
body {
font-family: 'Raleway', sans-serif;
background-color: #f5f5f5;
}

nav {
background-color: #fff;
box-shadow: 0px 1px 2px #ddd;
position: fixed;
top: 0;
left: 0;
width: 100px;
height: 100%;
padding-left: 30px;
padding-right: 30px;
padding-top: 30px;
}

.content {
margin-left: 180px;
padding: 30px;
margin-right: 20px;
}

.top-bar {
display: flex;
justify-content: space-between;
}

form input {
font-family: 'Raleway', sans-serif;
border: 0;
height: 30px;
padding: 5px 15px;
border-radius: 30px;
box-shadow: 0 1px 5px #ddd;
outline: none;
width: 450px;
display: block;
}

ul {
padding-left: 0;
}

.mail {
background-color: #fff;
list-style: none;
margin: 0;
margin-bottom: 15px;
padding: 10px 15px;
box-shadow: 0 1px 5px #ddd;
border-radius: 5px;
}

.mail-content {
margin: 5px 0 0 0;
color: #aaa;
}

.email {
color: #674dd4;
}

.button {
font-size: 14px;
transition: background-color 0.5s ease;
background-color: #674dd4;
color: #fff;
align-self: center;
padding: 10px 15px;
border-radius: 30px;
text-decoration: none;
border: none;
}

.button:hover {
background-color: #7b62e3;
}

hr {
border: none;
height: 1px;
background-color: #eee;
}

textarea {
font-family: 'Raleway', sans-serif;
border: 0;
height: 250px;
padding: 10px 15px;
border-radius: 30px;
box-shadow: 0 1px 5px #ddd;
resize: none;
width: 450px;
display: block;
outline: none;
}

.form-group {
margin-bottom: 15px;
}

form label {
display: inline-block;
margin-left: 10px;
margin-bottom: 10px;
}

.flash {
background-color: red;
color: white;
padding: 15px;
border-radius: 5px;
border: 1px solid rgba(0, 0, 0, 0.1);
margin: 10px 0;
}
26 changes: 26 additions & 0 deletions seccion_34_mailer/app/templates/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<title>{% block title %}{% endblock %} - Mailer</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Raleway:wght@400;600&display=swap" rel="stylesheet">

<nav>Mail<b>er</b> </nav>

<section class="content">
<div class="top-bar">
<form methods="get">
<input type="text" name="search" placeholder="¿Quieres buscar algo?">
</form>
{% block action %}{% endblock %}
</div>
<header>
{% block header %}{% endblock %}
</header>

{% for message in get_flashed_messages() %}
<div class="flash">{{ message }}</div>
{% endfor %}

{% block content %}{% endblock %}
</section>
25 changes: 25 additions & 0 deletions seccion_34_mailer/app/templates/mail/create.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{% extends "base.html" %}

{% block action %}
<a class="button" href="{{url_for('mail.index')}}">Volver</a>
{% endblock %}

{% block header %}
<h1>{% block title %} Enviar correo {% endblock %}</h1>
{% endblock %}

{% block content %}
<form method="post">
<div class="form-group">
<label for="email">Correo</label>
<input type="email" name="email" id="email" placeholder="Email">
<label for="subject">Asunto</label>
<input type="text" name="subject" id="subject" placeholder="subject">
<label for="content">Content</label>
<textarea name="content" id="content" placeholder="content"></textarea>
</div>
<div class="form-group">
<button class="button">Enviar</button>
</div>
</form>
{% endblock %}
28 changes: 28 additions & 0 deletions seccion_34_mailer/app/templates/mail/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{% extends "base.html" %}

{% block header %}
<h1>{% block title %} Correos Enviados {% endblock %}</h1>
{% endblock %}


{% block action %}
<a href="{{ url_for('mail.create') }}" class="button">Nuevo Correo</a>
{% endblock %}

{% block content %}
<ul>
{% for mail in mails %}
<li class="mail">
<div>
<span class="email">{{ mail["email"]}}</span>
<span class="subject">{{ mail["subject"]}}</span>
<hr>
<p class="mail-content">{{ mail["content"] }}</p>

</div>
</a>
</li>
{% endfor %}
</ul>

{% endblock %}
Loading