Chapter 4

This commit is contained in:
2023-06-03 16:59:29 +02:00
parent c640e3ca82
commit f9020e921f
16 changed files with 396 additions and 0 deletions

15
app/__init__.py Normal file
View File

@@ -0,0 +1,15 @@
from flask import Flask
from config import Config
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
app = Flask(__name__)
app.config.from_object(Config)
# Puestos fuera del tutorial
app.jinja_env.auto_reload = True
app.config["TEMPLATES_AUTO_RELOAD"] = True
db = SQLAlchemy(app)
migrate = Migrate(app, db)
from app import routes, models

10
app/forms.py Normal file
View File

@@ -0,0 +1,10 @@
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, SubmitField
from wtforms.validators import DataRequired
class LoginForm(FlaskForm):
username = StringField("Username", validators=[DataRequired()])
password = PasswordField("Password", validators=[DataRequired()])
remember_me = BooleanField("Remember Me")
submit = SubmitField("Sign In")

23
app/models.py Normal file
View File

@@ -0,0 +1,23 @@
from datetime import datetime
from app import db
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(64), index=True, unique=True)
email = db.Column(db.String(120), index=True, unique=True)
password_hash = db.Column(db.String(128))
posts = db.relationship("Post", backref="author", lazy="dynamic")
def __repr__(self):
return "<User {}>".format(self.username)
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
body = db.Column(db.String(140))
timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
user_id = db.Column(db.Integer, db.ForeignKey("user.id"))
def __repr__(self):
return "<Post {}>".format(self.body)

27
app/routes.py Normal file
View File

@@ -0,0 +1,27 @@
from flask import render_template, flash, redirect, url_for
from app import app
from app.forms import LoginForm
@app.route("/")
@app.route("/index")
def index():
user = {"username": "Miguel"}
posts = [
{"author": {"username": "John"}, "body": "Beautiful day in Portland!"},
{"author": {"username": "Susan"}, "body": "The Avengers movie was so cool!"},
]
return render_template("index.html", title="Home", user=user, posts=posts)
@app.route("/login", methods=["GET", "POST"])
def login():
form = LoginForm()
if form.validate_on_submit():
flash(
"Login requested for user {}, remember_me={}".format(
form.username.data, form.remember_me.data
)
)
return redirect(url_for("index"))
return render_template("login.html", title="Sign In", form=form)

29
app/templates/base.html Normal file
View File

@@ -0,0 +1,29 @@
<html>
<head>
{% if title %}
<title>{{ title }} - microblog</title>
{% else %}
<title>microblog</title>
{% endif %}
</head>
<body>
<div>
Microblog:
<a href="{{ url_for('index') }}">Home</a>
<a href="{{ url_for('login') }}">Login</a>
</div>
<hr />
{% with messages = get_flashed_messages() %}
{% if messages %}
<ul>
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
{% block content %}
{% endblock %}
</body>
</html>

8
app/templates/index.html Normal file
View File

@@ -0,0 +1,8 @@
{% extends "base.html" %}
{% block content %}
<h1>Hi, {{ user.username }}!</h1>
{% for post in posts %}
<div><p>{{ post.author.username }} says: <b>{{ post.body }}</b></p></div>
{% endfor %}
{% endblock %}

24
app/templates/login.html Normal file
View File

@@ -0,0 +1,24 @@
{% extends 'base.html' %}
{% block content %}
<h1>Sign In</h1>
<form action="" method="post" novalidate>
{{ form.hidden_tag() }}
<p>
{{ form.username.label }}<br />
{{ form.username(size = 32) }}<br />
{% for error in form.username.errors %}
<span style="color: red;">[{{ error }}]</span>
{% endfor %}
</p>
<p>
{{ form.password.label }}<br />
{{ form.password(size = 32) }}<br />
{% for error in form.password.errors %}
<span style="color: red;">[{{ error }}]</span>
{% endfor %}
</p>
<p>{{ form.remember_me() }} {{ form.remember_me.label }}</p>
<p>{{ form.submit() }}</p>
</form>
{% endblock %}