Skip to content

Commit 3f2a60e

Browse files
committed
step 3 done.
1 parent 6ce95a4 commit 3f2a60e

File tree

10 files changed

+161
-26
lines changed

10 files changed

+161
-26
lines changed

.env

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
SECRET_KEY="SECRETFORMYPROJECT007"
1+
SECRET_KEY="SECRETFORMYPROJECT007"
2+
DATABASE_URL=postgresql://tirion:1234@localhost:5432/hexlet

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.python-version
22
.venv
33
.idea
4+
.env

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ build:
1414
render-start:
1515
gunicorn -w 5 -b 0.0.0.0:$(PORT) page_analyzer:app
1616

17+
lint:
18+
ruff check .

build.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#!/usr/bin/env bash
2+
23
curl -LsSf https://astral.sh/uv/install.sh | sh
34
source $HOME/.local/bin/env
4-
make install
5+
6+
make install && psql -a -d $DATABASE_URL -f database.sql

page_analyzer/app.py

Lines changed: 68 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,81 @@
11
import os
2-
from flask import Flask, render_template
2+
from flask import Flask, render_template, request, redirect, flash, url_for
3+
import psycopg2
34
from dotenv import load_dotenv
5+
import validators
46

57
load_dotenv()
8+
DATABASE_URL = os.getenv('DATABASE_URL')
69

710
app = Flask(__name__)
8-
app.config['SECRET_KEY'] = os.getenv('SECRET_KEY')
11+
app.secret_key = 'your_secret_key' # Замените на ваш секретный ключ
912

10-
@app.route('/')
13+
14+
def get_db_connection():
15+
conn = psycopg2.connect(DATABASE_URL)
16+
return conn
17+
18+
19+
@app.route('/', methods=['GET', 'POST'])
1120
def index():
21+
if request.method == 'POST':
22+
url = request.form['url']
23+
if not validators.url(url) or len(url) > 255:
24+
flash('Неверный URL-адрес!', 'error')
25+
return redirect(url_for('index'))
26+
27+
conn = get_db_connection()
28+
cur = conn.cursor()
29+
try:
30+
cur.execute('INSERT INTO urls (name) VALUES (%s)', (url,))
31+
conn.commit()
32+
flash('URL успешно добавлен!', 'success')
33+
except psycopg2.IntegrityError:
34+
conn.rollback()
35+
flash('Этот URL уже существует!', 'error')
36+
finally:
37+
cur.close()
38+
conn.close()
39+
40+
return redirect(url_for('index'))
41+
1242
return render_template('index.html')
1343

14-
@app.route('/urls')
15-
def all_urls():
16-
pass
1744

18-
@app.route('/urls', methods=['POST'])
19-
def add_url():
20-
pass
45+
@app.route('/urls', methods=['GET'])
46+
def urls():
47+
conn = get_db_connection()
48+
cur = conn.cursor()
49+
cur.execute('SELECT * FROM urls ORDER BY created_at DESC')
50+
urls = cur.fetchall()
51+
cur.close()
52+
conn.close()
53+
54+
return render_template('urls.html', urls=urls)
55+
56+
57+
@app.route('/urls/<int:url_id>', methods=['GET'])
58+
def url_detail(url_id):
59+
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()
64+
conn.close()
65+
66+
if url is None:
67+
flash('URL не найден!', 'error')
68+
return redirect(url_for('urls'))
69+
70+
return render_template('url_detail.html', url=url)
71+
2172

22-
__all__ = ['app']
2373
if __name__ == '__main__':
24-
app.run()
74+
app.run(debug=True)
75+
76+
77+
78+
#
79+
# @app.route('/urls', methods=['POST'])
80+
# def add_url():
81+
# pass

page_analyzer/templates/index.html

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,21 @@
1919
<div class="col-12 col-md-11 col-lg-8 mx-auto border rounded-3 bg-light p-5">
2020
<h1 class="display-3">Анализатор страниц</h1>
2121
<p class="lead">Бесплатно проверяйте сайты на SEO-пригодность</p>
22-
<form action="{{ url_for('add_url') }}" method="post" class="d-flex justify-content-center">
23-
<div class="input-group input-group-lg">
24-
<input
22+
<form method="POST">
23+
<div class="input-group input-group-lg">
24+
<input
2525
type="url"
2626
name="url"
2727
value="{{ url if url is defined else '' }}"
2828
placeholder="https://www.example.com"
2929
class="form-control form-control-lg"
3030
required
31-
>
32-
<input type="submit" value="Проверить" class="btn btn-primary btn-lg ms-3 px-5 text-uppercase mx-2">
33-
</div>
34-
</form>
31+
>
32+
<input type="submit" value="Проверить" class="btn btn-primary btn-lg ms-3 px-5 text-uppercase mx-2">
33+
34+
</div>
35+
</form>
3536
</div>
3637
</div>
3738
</body>
38-
{% endblock %}
39+
{% endblock %}

page_analyzer/templates/layout.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<div class="collapse navbar-collapse" id="navbarNav">
1818
<ul class="navbar-nav">
1919
<li class="nav-item">
20-
<a class="nav-link active" aria-current="page" href="{{ url_for('all_urls') }}">Сайты</a>
20+
<a class="nav-link active" aria-current="page" href="{{ url_for('urls') }}">Сайты</a>
2121
</li>
2222
</ul>
2323
</div>

page_analyzer/templates/urls.html

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ <h1 class="mb-4">Сайты</h1>
1515
<tbody>
1616
{% for url in urls %}
1717
<tr>
18-
<td>{{ url.id }}</td>
19-
<td><a href="{{ url_for('url_detail', id=url.id) }}">{{ url.name }}</a></td>
20-
<td>{{ url.last_check.strftime('%d-%m-%Y %H:%M') if url.last_check else 'Ещё не проверялся' }}</td>
21-
<td>{{ url.status_code if url.status_code else '' }}</td>
18+
<td>{{ url[0] }}</td>
19+
<td><a href="{{ url_for('url_detail', url_id=url[0]) }}">{{ url[1] }}</a></td>
20+
<td>{{ url[2] }}</td>
21+
<td>{{ url_id }}</td>
2222
</tr>
2323
{% endfor %}
2424
</tbody>
2525
</table>
26+
<a href="{{ url_for('index') }}" class="btn btn-secondary">Назад</a>
2627
</div>
2728
{% endblock %}

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ requires-python = ">=3.10"
77
dependencies = [
88
"flask>=3.1.0",
99
"gunicorn>=23.0.0",
10+
"psycopg2-binary>=2.9.10",
1011
"pytest>=8.3.5",
1112
"python-dotenv>=1.1.0",
1213
"ruff>=0.11.7",
14+
"validators>=0.35.0",
1315
]

0 commit comments

Comments
 (0)