-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
116 lines (91 loc) · 3.67 KB
/
app.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
from flask import Flask, render_template, redirect, url_for, request, flash
from datetime import datetime
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager, UserMixin, current_user, login_user, logout_user, login_required
from werkzeug.security import check_password_hash, generate_password_hash
from sqlalchemy.exc import IntegrityError
import os
app = Flask('henlo')
db_url = os.environ.get('DATABASE_URL') or 'sqlite:///app.db'
app.config['SQLALCHEMY_DATABASE_URI'] = db_url.replace('postgres', 'postgresql')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SECRET_KEY'] = 'animu'
db = SQLAlchemy(app)
login = LoginManager(app)
class Post(db.Model):
__tablename__ = 'posts'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
title = db.Column(db.String(70), nullable=False)
content = db.Column(db.String(500))
created_at = db.Column(db.DateTime, nullable=False, default=datetime.now)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
class User(UserMixin, db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
username = db.Column(db.String(25), nullable=False,
unique=True, index=True)
email = db.Column(db.String(64), nullable=False, unique=True)
password_hash = db.Column(db.String(128), nullable=False)
posts = db.relationship('Post', backref='author')
def set_password(self, password):
self.password_hash = generate_password_hash(password)
def check_password(self, password):
return check_password_hash(self.password_hash, password)
@login.user_loader
def load_user(id):
return User.query.get(int(id))
db.create_all()
@app.route('/')
def index():
posts = Post.query.all()
return render_template('blog.html', posts=posts)
@app.route('/register', methods=['GET', 'POST'])
def register():
if current_user.is_authenticated:
return redirect(url_for('blog'))
if request.method == 'POST':
username = request.form['username']
email = request.form['email']
password = request.form['password']
try:
new_user = User(username=username, email=email)
new_user.set_password(password)
db.session.add(new_user)
db.session.commit()
except IntegrityError:
flash('username or email already registered')
else:
return redirect(url_for('login'))
return render_template('register.html')
@app.route('/login', methods=['GET', 'POST'])
@login_required
def login():
if current_user.is_authenticated:
return redirect(url_for('blog'))
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
user = User.query.filter_by(username=username).first()
if user is None or not user.check_password(password):
flash('wrong username/password')
return redirect(url_for('login'))
login_user(user)
return redirect(url_for('blog'))
return render_template('login.html')
@app.route('/logout')
def logout():
logout_user()
return redirect(url_for('blog'))
@app.route('/create', methods=['GET', 'POST'])
def create():
if request.method == 'POST':
title = request.form['title']
content = request.form['content']
try:
post = Post(title=title, content=content, author=current_user)
db.session.add()
db.session.commit()
return redirect(url_for('blog'))
except IntegrityError:
flash('error on creating post') # add info about limit of chars
return render_template('post.html')