-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
102 lines (78 loc) · 3 KB
/
app.py
File metadata and controls
102 lines (78 loc) · 3 KB
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
from flask import Flask, render_template, url_for, request, redirect
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
from asgiref.wsgi import WsgiToAsgi
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///blog.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class Article(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(80), nullable=False)
intro = db.Column(db.String(100), nullable=False)
text = db.Column(db.Text, nullable=False)
date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
author = db.Column(db.String(80), nullable=False)
def __repr__(self):
return '<Article %r>' % self.id
@app.route('/')
def index():
articles = Article.query.order_by(Article.date_posted.desc()).all()
return render_template('index.html', articles=articles)
@app.route('/posts')
def posts():
articles = Article.query.order_by(Article.date_posted.desc()).all()
return render_template('posts.html', articles=articles)
@app.route('/posts/<int:post_id>')
def posts_detail(post_id):
article = Article.query.get(post_id)
return render_template('post_detail.html', article=article)
@app.route('/posts/<int:post_id>/delete')
def posts_delete(post_id):
article = Article.query.get_or_404(post_id)
try:
db.session.delete(article)
db.session.commit()
return redirect('/posts')
except Exception as e:
return f"Failed to delete article: {e}"
@app.route('/create', methods=['GET', 'POST'])
def create():
if request.method == 'POST':
title = request.form['title']
intro = request.form['intro']
text = request.form['text']
author = request.form['author']
article = Article(title=title, intro=intro, text=text, author=author)
try:
db.session.add(article)
db.session.commit()
return redirect('/posts')
except Exception as e:
return f"Failed to create article: {e}"
else:
return render_template('create.html')
@app.route('/posts/<int:post_id>/update', methods=['GET', 'POST'])
def post_update(post_id):
article = Article.query.get(post_id)
if request.method == 'POST':
article.title = request.form['title']
article.intro = request.form['intro']
article.text = request.form['text']
article.author = request.form['author']
try:
db.session.commit()
return redirect('/posts')
except Exception as e:
return f"При редактировании статьи произошла ошибка -> : {e}"
else:
return render_template('post_update.html', article=article)
@app.route('/about')
def about():
return render_template('about.html')
if __name__ == '__main__':
import hypercorn.asyncio
from hypercorn.config import Config
config = Config()
config.bind = ["127.0.0.1:8000"]
var = hypercorn.asyncio.run