Skip to content

Commit

Permalink
added postres db, added 3 pages, added main logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey-Barinov committed Apr 15, 2024
1 parent 5a4de76 commit 59c7fb1
Show file tree
Hide file tree
Showing 13 changed files with 347 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ dist/
coverage.xml
.pytest_cache/
.idea/
main.py
.env
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ PORT ?= 8000
start:
poetry run gunicorn -w 5 -b 0.0.0.0:$(PORT) page_analyzer:app

build:
./build.sh

lint:
poetry run flake8 page_analyzer

Expand All @@ -20,3 +23,4 @@ selfcheck:
check:
selfcheck lint

.PHONY: install lint selfcheck check build
3 changes: 3 additions & 0 deletions build.sh
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
7 changes: 7 additions & 0 deletions database.sql
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
);
69 changes: 66 additions & 3 deletions page_analyzer/app.py
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)
98 changes: 98 additions & 0 deletions page_analyzer/db.py
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
10 changes: 8 additions & 2 deletions page_analyzer/templates/index.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
{% 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 %}
<div class="row">
<div class="col-12 col-md-10 col-lg-8 mx-auto border rounded-3 bg-light p-5">
<h1 class="display-3">Анализатор страниц</h1>
<p class="lead">Бесплатно проверяйте сайты на SEO-пригодность</p>
<form class="d-flex justify-content-center" action="#" method="post">
<input type="text" name="url" class="form-control form-control-lg" placeholder="https://www.example.com" required>
<form class="d-flex justify-content-center" action="{{ url_for('add_url') }}" method="post">
<label for="write-url" class="sr-only">url</label>
<input type="text" id="write-url" name="url" class="form-control form-control-lg" placeholder="https://www.example.com" required>
<input type="submit" class="btn btn-primary btn-lg ms-3 px-5 text-uppercase mx-3" value="Проверить">
</form>
</div>
Expand Down
3 changes: 2 additions & 1 deletion page_analyzer/templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
<a class="navbar-brand" href="/">Анализатор страниц</a>
<div class="collapse navbar-collapse">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item"><a href="#" class="nav-link">Сайты</a></li>
<li class="nav-item"><a href="/urls" class="nav-link">Сайты</a></li>
</ul>
</div>
</nav>
</header>
{% block sidebar %}{% endblock sidebar %}
<main class="flex-grow-1">
<div id="content" class="container-lg mt-3">
{% block content %}{% endblock %}
Expand Down
27 changes: 27 additions & 0 deletions page_analyzer/templates/url.html
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 %}
26 changes: 26 additions & 0 deletions page_analyzer/templates/urls.html
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 %}
10 changes: 10 additions & 0 deletions page_analyzer/url_validator.py
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 ''
Loading

0 comments on commit 59c7fb1

Please sign in to comment.