Skip to content

Commit 4d21f4d

Browse files
committed
Добавили поддержку базы джанных Flask-SQLAlchemy
1 parent df97fe4 commit 4d21f4d

File tree

7 files changed

+45
-11
lines changed

7 files changed

+45
-11
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.vscode/
22
__pycache__/
33
env/
4-
config.py
4+
config.py
5+
*.db

create_db.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from webapp import db, create_app
2+
3+
db.create_all(app=create_app())

get_all_news.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from webapp import create_app
2+
from webapp.python_org_news import get_python_news
3+
4+
app = create_app()
5+
with app.app_context():
6+
get_python_news()

webapp/__init__.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
from flask import Flask, render_template
22

3-
from webapp.python_org_news import get_python_news
3+
from webapp.model import db, News
44
from webapp.weather import weather_by_city
55

66
def create_app():
77
app = Flask(__name__)
88
app.config.from_pyfile('config.py')
9+
db.init_app(app)
910

1011
@app.route('/')
1112
def index():
1213
title = "Новости Python"
1314
weather = weather_by_city(app.config['WEATHER_DEFAULT_CITY'])
14-
news_list = get_python_news()
15+
news_list = News.query.order_by(News.published.desc()).all()
1516
return render_template('index.html', page_title=title, weather=weather, news_list=news_list)
1617

1718
return app

webapp/model.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from flask_sqlalchemy import SQLAlchemy
2+
3+
db = SQLAlchemy()
4+
5+
class News(db.Model):
6+
id = db.Column(db.Integer, primary_key=True)
7+
title = db.Column(db.String, nullable=False)
8+
url = db.Column(db.String, unique=True, nullable=False)
9+
published = db.Column(db.DateTime, nullable=False)
10+
text = db.Column(db.Text, nullable=True)
11+
12+
def __repr__(self):
13+
return '<News {} {}>'.format(self.title, self.url)

webapp/python_org_news.py

+17-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
from datetime import datetime
2+
13
import requests
24
from bs4 import BeautifulSoup
35

6+
from webapp.model import db, News
7+
48
def get_html(url):
59
try:
610
result = requests.get(url)
@@ -20,10 +24,16 @@ def get_python_news():
2024
title = news.find('a').text
2125
url = news.find('a')['href']
2226
published = news.find('time').text
23-
result_news.append({
24-
"title": title,
25-
"url": url,
26-
"published": published
27-
})
28-
return result_news
29-
return False
27+
try:
28+
published = datetime.strptime(published, '%Y-%m-%d')
29+
except ValueError:
30+
published = datetime.now()
31+
save_news(title, url, published)
32+
33+
def save_news(title, url, published):
34+
news_exists = News.query.filter(News.url == url).count()
35+
print(news_exists)
36+
if not news_exists:
37+
news_news = News(title=title, url=url, published=published)
38+
db.session.add(news_news)
39+
db.session.commit()

webapp/templates/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ <h1>{{ page_title }}</h1>
1818
<h2>Новости</h2>
1919
{% for news in news_list %}
2020
<h3><a href="{{ news.url }}">{{ news.title }}</a></h3>
21-
<p>{{ news.published }}</p>
21+
<p>{{ news.published.strftime('%d.%m.%Y') }}</p>
2222
<hr />
2323
{% endfor %}
2424
</div>

0 commit comments

Comments
 (0)