Skip to content

Commit

Permalink
URl_checks реализация для статуса
Browse files Browse the repository at this point in the history
  • Loading branch information
KAnanev committed Jul 13, 2023
1 parent e439ff2 commit 90d1635
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 8 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)
4 changes: 2 additions & 2 deletions page_analyzer/services/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def get_all_urls(self) -> List[URLModel] | None:
items = [URLModel(**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 +79,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
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
1 change: 1 addition & 0 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def test_post_url_check(client):

response = client.post('/urls/1/checks', follow_redirects=True)
assert '5' in response.text
assert '200' in response.text

response = client.post('/urls/1/checks', follow_redirects=True)
assert '6' in response.text
Expand Down

0 comments on commit 90d1635

Please sign in to comment.