Skip to content

Commit fa2c1a6

Browse files
committed
step 4 (add new table)
1 parent 53ed507 commit fa2c1a6

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

database.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,13 @@ CREATE TABLE IF NOT EXISTS urls (
33
name VARCHAR(255) UNIQUE NOT NULL,
44
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
55
);
6+
7+
CREATE TABLE IF NOT EXISTS url_checks (
8+
id SERIAL PRIMARY KEY,
9+
url_id INTEGER NOT NULL REFERENCES urls(id),
10+
status_code INTEGER,
11+
h1 TEXT,
12+
title TEXT,
13+
description TEXT,
14+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
15+
);

page_analyzer/app.py

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import psycopg2
44
from dotenv import load_dotenv
55
import validators
6+
from datetime import datetime
67

78
load_dotenv()
89
DATABASE_URL = os.getenv('DATABASE_URL')
@@ -57,25 +58,36 @@ def urls():
5758
@app.route('/urls/<int:url_id>', methods=['GET'])
5859
def url_detail(url_id):
5960
conn = get_db_connection()
60-
cur = conn.cursor()
61-
cur.execute('SELECT * FROM urls WHERE id = %s', (url_id,))
62-
url = cur.fetchone()
63-
cur.close()
61+
cursor = conn.cursor()
62+
cursor.execute('SELECT * FROM urls WHERE id = %s', (url_id,))
63+
url = cursor.fetchone()
64+
cursor.close()
6465
conn.close()
65-
6666
if url is None:
67-
flash('URL не найден!', 'error')
67+
flash('Сайт не найден.', 'error')
6868
return redirect(url_for('urls'))
69+
return render_template('result.html', url=url)
6970

70-
return render_template('url_detail.html', url=url)
71+
72+
@app.route('/urls/<int:url_id>/checks', methods=['POST'])
73+
def add_check(url_id):
74+
conn = get_db_connection()
75+
cursor = conn.cursor()
76+
try:
77+
# Здесь вы можете добавить логику для получения status_code, h1, title, description
78+
# На данный момент заполняем только url_id и created_at
79+
cursor.execute('INSERT INTO url_checks (url_id, created_at) VALUES (%s, %s)', (url_id, datetime.now()))
80+
conn.commit()
81+
flash('Проверка успешно добавлена!', 'success')
82+
except Exception as e:
83+
flash('Ошибка при добавлении проверки: ' + str(e), 'error')
84+
finally:
85+
cursor.close()
86+
conn.close()
87+
88+
return redirect(url_for('url_detail', url_id=url_id))
7189

7290

7391
if __name__ == '__main__':
7492
app.run(debug=True)
7593

76-
77-
78-
#
79-
# @app.route('/urls', methods=['POST'])
80-
# def add_url():
81-
# pass

0 commit comments

Comments
 (0)