Skip to content

Commit

Permalink
Merge pull request #20 from KAnanev/dev
Browse files Browse the repository at this point in the history
#19 closed
  • Loading branch information
KAnanev committed Jul 13, 2023
2 parents 9d704d5 + 1cb5e21 commit 13a6ac6
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 15 deletions.
14 changes: 11 additions & 3 deletions page_analyzer/services/check_url.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import logging

import requests

from page_analyzer.services.url import URLService

from page_analyzer.models import URLChecks
from page_analyzer.services.db import PostgresDB

Expand Down Expand Up @@ -28,6 +32,10 @@ def check(self, url_id):
return 'Страница успешно проверена', 'success'
return 'Произошла ошибка при проверке', 'danger'

@staticmethod
def check_url(url_id):
return URLChecks(url_id=url_id)
def check_url(self, url_id):
url_service = URLService(self.db)
url = url_service.get_json_by_id(url_id).name
if url:
request = requests.get(url)
if request:
return URLChecks(url_id=url_id, status_code=request.status_code)
36 changes: 27 additions & 9 deletions page_analyzer/services/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,29 @@
from page_analyzer.services.db import PostgresDB

GET_ITEMS = """SELECT
json_build_object(
'id', id,
'name', name,
'created_at', created_at
) AS result
FROM urls;"""
json_build_object(
'id', urls.id,
'name', urls.name,
'created_at', urls.created_at,
'url_checks', COALESCE(json_agg(json_build_object(
'id', latest_url_checks.id,
'url_id', latest_url_checks.url_id,
'status_code', latest_url_checks.status_code,
'h1', latest_url_checks.h1,
'title', latest_url_checks.title,
'description', latest_url_checks.description,
'created_at', latest_url_checks.created_at
)) FILTER (WHERE latest_url_checks.id IS NOT NULL), '[]'::json)
) AS result
FROM urls
LEFT JOIN LATERAL (
SELECT *
FROM url_checks
WHERE url_checks.url_id = urls.id
ORDER BY url_checks.created_at DESC
LIMIT 1
) AS latest_url_checks ON true
GROUP BY urls.id;"""

GET_JSON_BY_ID = """SELECT
json_build_object(
Expand Down Expand Up @@ -63,10 +80,11 @@ def get_all_urls(self) -> List[URLModel] | None:
items = self.db.execute_query(GET_ITEMS, many=True)
if items:
sorted_items = sorted(items, key=lambda item: -item['result']['id'])
items = [URLModel(**item['result']) for item in sorted_items]
print(sorted_items)
items = [URLSModel(**item['result']) for item in sorted_items]
return items

def _get_url_id_by_url_name(self, item: URLModel) -> Optional[URLModel]:
def get_url_id_by_url_name(self, item: URLModel) -> Optional[URLModel]:

exist_item = self.db.execute_query(GET_JSON_BY_URL, (item.name,), )
if exist_item:
Expand All @@ -79,7 +97,7 @@ def insert_url(self, url: str) -> dict[str, tuple[str, str] | URLModel]:

try:
item = URLModel(name=url)
item = self._get_url_id_by_url_name(item)
item = self.get_url_id_by_url_name(item)

if item.id:
message = ('Страница уже существует', 'info')
Expand Down
7 changes: 7 additions & 0 deletions page_analyzer/templates/urls.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,15 @@
{{ item.name }}
</a>
</td>
{% if item.url_checks %}
{% for url_checks in item.url_checks %}
<td>{{ url_checks.created_at }}</td>
<td>{{ url_checks.status_code }}</td>
{% endfor %}
{% else %}
<td></td>
<td></td>
{% endif %}
</tr>
{% endfor %}
{% endif %}
Expand Down
151 changes: 150 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ psycopg-binary = "^3.1.9"
psycopg-c = "^3.1.9"
psycopg = "^3.1.9"
pydantic = "^2.0"
requests = "^2.31.0"


[tool.poetry.group.dev.dependencies]
Expand Down
4 changes: 2 additions & 2 deletions tests/test_url_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ def test__get_url_id_by_url_name(app):
db = get_db()
url_service = URLService(db=db)
item = URLModel(name='https://chatbot.theb.ai')
data = url_service._get_url_id_by_url_name(item)
data = url_service.get_url_id_by_url_name(item)
assert not data.id

item = URLModel(name='http:https://www.ya.ru/')
data = url_service._get_url_id_by_url_name(item)
data = url_service.get_url_id_by_url_name(item)
assert data.id


Expand Down
Loading

0 comments on commit 13a6ac6

Please sign in to comment.