-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
94 lines (74 loc) · 2.86 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# flask
from flask import Flask, render_template, request, redirect, url_for
from flask_login import LoginManager, logout_user, current_user, login_user, login_required
from werkzeug.urls import url_parse
# local
from forms import SignupForm, PostForm, LoginForm
from models import users, get_user, User
app = Flask(__name__)
app.config['SECRET_KEY'] = '7110c8ae51a4b5af97be6534caef90e4bb9bdcb3380af008f90b23a5d1616bf319bc298105da20fe'
login_manager = LoginManager(app)
login_manager.login_view = "login"
posts = []
@app.route("/")
def index():
return render_template("index.html", posts=posts)
@app.route("/p/<string:slug>/")
def show_post(slug):
return render_template("post_view.html", slug_title=slug)
@app.route("/admin/post/", methods=['GET', 'POST'], defaults={'post_id': None})
@app.route("/admin/post/<int:post_id>/", methods=['GET', 'POST'])
@login_required
def post_form(post_id):
form = PostForm()
if form.validate_on_submit():
title = form.title.data
title_slug = form.title_slug.data
content = form.content.data
post = {'title': title, 'title_slug': title_slug, 'content': content}
posts.append(post)
return redirect(url_for('index'))
return render_template("admin/post_form.html", form=form)
@app.route("/signup/", methods=["GET", "POST"])
def show_signup_form():
if current_user.is_authenticated:
return redirect(url_for('index'))
form = SignupForm()
if form.validate_on_submit():
name = form.name.data
email = form.email.data
password = form.password.data
# Creamos el usuario y lo guardamos
user = User(len(users) + 1, name, email, password)
users.append(user)
# Dejamos al usuario logueado
login_user(user, remember=True)
next_page = request.args.get('next', None)
if not next_page or url_parse(next_page).netloc != '':
next_page = url_for('index')
return redirect(next_page)
return render_template("signup_form.html", form=form)
@login_manager.user_loader
def load_user(user_id):
for user in users:
if user.id == int(user_id):
return user
return None
@app.route('/login', methods=['GET', 'POST'])
def login():
if current_user.is_authenticated:
return redirect(url_for('index'))
form = LoginForm()
if form.validate_on_submit():
user = get_user(form.email.data)
if user is not None and user.check_password(form.password.data):
login_user(user, remember=form.remember_me.data)
next_page = request.args.get('next')
if not next_page or url_parse(next_page).netloc != '':
next_page = url_for('index')
return redirect(next_page)
return render_template('login_form.html', form=form)
@app.route('/logout')
def logout():
logout_user()
return redirect(url_for('index'))