-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
98 lines (66 loc) · 2.39 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
from flask import Flask, render_template, request, url_for, flash, redirect
import os, datetime
import sqlite3
from flask_sqlalchemy import SQLAlchemy
from werkzeug.exceptions import abort
project_dir = os.path.dirname(os.path.abspath(__file__))
database_file = "sqlite:///{}".format(os.path.join(project_dir, "database.db"))
app = Flask('__name__')
app.config['SECRET_KEY'] = 'your secret key'
app.config["SQLALCHEMY_DATABASE_URI"] = database_file
db = SQLAlchemy(app)
class Posts(db.Model):
id = db.Column(db.Integer, primary_key=True)
created = db.Column(db.DateTime, default=datetime.datetime.utcnow)
title = db.Column(db.String(80), nullable=False)
content = db.Column(db.String(200), nullable=False)
@app.route('/')
def index():
posts = Posts.query.all()
return render_template('index.html', posts=posts)
def get_post(post_id):
post = Posts.query.filter_by(id=post_id).first()
if post is None:
abort(484)
return post
@app.route('/<int:post_id>')
def post(post_id):
post = get_post(post_id)
return render_template('post.html', post=post)
@app.route('/create', methods=('GET', 'POST'))
def create():
if request.method == 'POST':
title = request.form['title']
content = request.form['content']
if not title:
flash('O título é obrigatório !')
else:
post = Posts(title=title, content=content)
db.session.add(post)
db.session.commit()
return redirect(url_for('index'))
return render_template('create.html')
@app.route('/<int:id>/edit', methods=('GET', 'POST'))
def edit (id):
post = get_post(id)
if request.method == 'POST':
title = request.form['title']
content = request.form['content']
if not title:
flash("Título é obrigatório !")
else:
post.title = title
post.content = content
db.session.commit()
return redirect(url_for('index'))
return render_template('edit.html', post=post)
@app.route('/<int:id>/delete', methods=('POST',))
def delete(id):
post = get_post(id)
db.session.delete(post)
db.session.commit()
flash('O material localizado em {} foi coletado com sucesso !!'.format(post.title))
return redirect(url_for('index'))
@app.route('/projeto', methods=('GET', 'POST'))
def projeto():
return render_template('projeto.html')