-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added postres db, added 3 pages, added main logic
- Loading branch information
1 parent
5a4de76
commit 59c7fb1
Showing
13 changed files
with
347 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,4 @@ dist/ | |
coverage.xml | ||
.pytest_cache/ | ||
.idea/ | ||
main.py | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/usr/bin/env bash | ||
|
||
make install && psql -a -d $DATABASE_URL -f database.sql |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
DROP TABLE IF EXISTS urls; | ||
|
||
CREATE TABLE urls ( | ||
id serial PRIMARY KEY, | ||
name varchar(255) UNIQUE NOT NULL, | ||
created_at date DEFAULT CURRENT_DATE | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,75 @@ | ||
from flask import Flask, render_template | ||
import os | ||
from dotenv import load_dotenv | ||
from page_analyzer.url_validator import validate | ||
from urllib.parse import urlparse | ||
from flask import ( | ||
Flask, | ||
render_template, | ||
request, | ||
url_for, | ||
redirect, | ||
flash, | ||
get_flashed_messages, | ||
) | ||
from page_analyzer.db import ( | ||
add_url_to_db, | ||
get_url_by_id, | ||
get_url_by_name, | ||
get_all_urls_desc | ||
) | ||
|
||
|
||
load_dotenv() | ||
app = Flask(__name__) | ||
app.config['SECRET_KEY'] = os.getenv('SECRET_KEY') | ||
|
||
@app.route('/') | ||
|
||
@app.get('/') | ||
def page_analyzer(): | ||
return render_template('index.html') | ||
message = get_flashed_messages(with_categories=True) | ||
|
||
return render_template('index.html', message=message) | ||
|
||
|
||
@app.post('/urls') | ||
def add_url(): | ||
new_url = request.form.get('url') | ||
|
||
error = validate(new_url) | ||
|
||
if error: | ||
flash(f'{error}', 'danger') | ||
return redirect(url_for('page_analyzer')) | ||
|
||
parsed_url = urlparse(new_url) | ||
normal_url = f"{parsed_url.scheme}://{parsed_url.netloc}" | ||
|
||
if get_url_by_name(normal_url): | ||
old_url_data = get_url_by_name(normal_url) | ||
|
||
flash('Страница уже существует', 'primary') | ||
|
||
return redirect(url_for('show_url', id=old_url_data['id'])) | ||
|
||
add_url_to_db(normal_url) | ||
|
||
new_url_data = get_url_by_name(normal_url) | ||
|
||
flash('Страница успешно добавлена', 'success') | ||
|
||
return redirect(url_for('show_url', id=new_url_data['id'])) | ||
|
||
|
||
@app.get('/urls') | ||
def show_all_urls(): | ||
all_urls = get_all_urls_desc() | ||
|
||
return render_template('urls.html', all_urls=all_urls) | ||
|
||
|
||
@app.get('/urls/<id>') | ||
def show_url(id): | ||
url_data = get_url_by_id(id) | ||
message = get_flashed_messages(with_categories=True) | ||
|
||
return render_template('url.html', url_data=url_data, message=message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import psycopg2 | ||
import os | ||
from dotenv import load_dotenv | ||
from psycopg2.extras import NamedTupleCursor | ||
|
||
load_dotenv() | ||
|
||
DATABASE_URL = os.getenv('DATABASE_URL') | ||
|
||
# conn = psycopg2.connect(DATABASE_URL) | ||
# | ||
# | ||
# sql_file_path = os.path.join('..', 'database.sql') | ||
# | ||
# with conn.cursor() as cur, open(sql_file_path, 'r') as data_base: | ||
# cur.execute(data_base.read()) | ||
# | ||
# conn.commit() | ||
# conn.close() | ||
|
||
|
||
def add_url_to_db(url): | ||
conn = psycopg2.connect(DATABASE_URL) | ||
with conn.cursor() as cur: | ||
cur.execute("INSERT INTO urls (name) VALUES (%s)", (url,)) | ||
conn.commit() | ||
conn.close() | ||
|
||
|
||
def trans_urls(urls): | ||
result = [] | ||
if not isinstance(urls, list): | ||
return { | ||
'id': urls.id, | ||
'name': urls.name, | ||
'created_at': str(urls.created_at) | ||
} | ||
|
||
for url in urls: | ||
url_data = { | ||
'id': url.id, | ||
'name': url.name, | ||
'created_at': str(url.created_at) | ||
} | ||
|
||
result.append(url_data) | ||
|
||
return result | ||
|
||
|
||
def get_url_by_name(url): | ||
conn = psycopg2.connect(DATABASE_URL) | ||
|
||
with conn.cursor(cursor_factory=NamedTupleCursor) as cur: | ||
cur.execute("SELECT * FROM urls WHERE name = %s", (url,)) | ||
|
||
url = cur.fetchone() | ||
|
||
if url: | ||
url_data = trans_urls(url) | ||
else: | ||
url_data = None | ||
|
||
conn.commit() | ||
conn.close() | ||
|
||
return url_data | ||
|
||
|
||
def get_url_by_id(url_id): | ||
conn = psycopg2.connect(DATABASE_URL) | ||
|
||
with conn.cursor(cursor_factory=NamedTupleCursor) as cur: | ||
cur.execute("SELECT * FROM urls WHERE id = %s", (url_id,)) | ||
|
||
url = cur.fetchone() | ||
|
||
url_data = trans_urls(url) | ||
|
||
conn.commit() | ||
conn.close() | ||
|
||
return url_data | ||
|
||
|
||
def get_all_urls_desc(): | ||
conn = psycopg2.connect(DATABASE_URL) | ||
|
||
with conn.cursor(cursor_factory=NamedTupleCursor) as cur: | ||
cur.execute("SELECT * FROM urls ORDER BY id DESC") | ||
|
||
all_urls = cur.fetchall() | ||
all_urls_data = trans_urls(all_urls) | ||
|
||
conn.commit() | ||
conn.close() | ||
|
||
return all_urls_data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{% extends "layout.html" %} | ||
{% block sidebar %} | ||
{% if message %} | ||
<div class="alert alert-{{ message[0][0] }} d-flex" role="alert">{{ message[0][1] }}</div> | ||
{% endif %} | ||
{% endblock sidebar %} | ||
{% block content %} | ||
<h1>Сайт: {{url_data.name}}</h1> | ||
<div class="table-responsive"> | ||
<table class="table table-bordered table-hover text-nowrap" data-test="url"> | ||
<tbody> | ||
<tr> | ||
<td>ID</td> | ||
<td>{{url_data.id}}</td> | ||
</tr> | ||
<tr> | ||
<td>Имя</td> | ||
<td>{{url_data.name}}</td> | ||
</tr> | ||
<tr> | ||
<td>Дата создания</td> | ||
<td>{{url_data.created_at}}</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
</div> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{% extends "layout.html" %} | ||
{% block content %} | ||
<h1>Сайты</h1> | ||
<div class="table-responsive"> | ||
<table class="table table-bordered table-hover text-nowrap" data-test="urls"> | ||
<thead> | ||
<tr> | ||
<th>ID</th> | ||
<th>Имя</th> | ||
<th>Последняя проверка</th> | ||
<th>Код ответа</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{% for url in all_urls %} | ||
<tr> | ||
<td>{{ url.id }}</td> | ||
<td><a href="{{ url_for('show_url', id=url.id )}}">{{ url.name }}</a></td> | ||
<td></td> | ||
<td></td> | ||
</tr> | ||
{% endfor %} | ||
</tbody> | ||
</table> | ||
</div> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import validators | ||
|
||
|
||
def validate(url): | ||
if len(url) > 255: | ||
return 'URL превышает 255 символов' | ||
elif validators.url(url) is not True: | ||
return 'Некорректный URL' | ||
else: | ||
return '' |
Oops, something went wrong.