Skip to content

Commit

Permalink
refactoring db.py, added CodeClimate badge
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey-Barinov committed Apr 21, 2024
1 parent 2d4410f commit c6e3507
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
### Hexlet tests and linter status:
[![Actions Status](https://github.com/Andrey-Barinov/python-project-83/actions/workflows/hexlet-check.yml/badge.svg)](https://github.com/Andrey-Barinov/python-project-83/actions)
[![Actions Status](https://github.com/Andrey-Barinov/python-project-83/actions/workflows/pyci.yml/badge.svg)](https://github.com/Andrey-Barinov/python-project-83/actions)
[![Maintainability](https://api.codeclimate.com/v1/badges/e0fb12dbfecbfbbae2c5/maintainability)](https://codeclimate.com/github/Andrey-Barinov/python-project-83/maintainability)

<b>Page Analyzer</b> is a website that analyzes the specified pages for SEO suitability.
<p>You can try it at the link https://python-project-83-rael.onrender.com </p>
16 changes: 10 additions & 6 deletions page_analyzer/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@
DATABASE_URL = os.getenv('DATABASE_URL')


def get_connection():
return psycopg2.connect(DATABASE_URL)


def add_url_to_db(url):
conn = psycopg2.connect(DATABASE_URL)
conn = get_connection()
with conn.cursor() as cur:
cur.execute("INSERT INTO urls (name) VALUES (%s)", (url,))
conn.commit()
conn.close()


def get_url_by_name(url):
conn = psycopg2.connect(DATABASE_URL)
conn = get_connection()

with conn.cursor(cursor_factory=NamedTupleCursor) as cur:
cur.execute("SELECT * FROM urls WHERE name = %s", (url,))
Expand All @@ -36,7 +40,7 @@ def get_url_by_name(url):


def get_url_by_id(url_id):
conn = psycopg2.connect(DATABASE_URL)
conn = get_connection()

with conn.cursor(cursor_factory=NamedTupleCursor) as cur:
cur.execute("SELECT * FROM urls WHERE id = %s", (url_id,))
Expand All @@ -50,7 +54,7 @@ def get_url_by_id(url_id):


def get_all_urls_desc():
conn = psycopg2.connect(DATABASE_URL)
conn = get_connection()

with conn.cursor(cursor_factory=NamedTupleCursor) as cur:
cur.execute("SELECT * FROM urls ORDER BY id DESC")
Expand All @@ -64,7 +68,7 @@ def get_all_urls_desc():


def add_check_to_db(url_id, status_code, h1, title, description):
conn = psycopg2.connect(DATABASE_URL)
conn = get_connection()

with conn.cursor() as cur:
cur.execute("INSERT INTO url_checks ("
Expand All @@ -82,7 +86,7 @@ def add_check_to_db(url_id, status_code, h1, title, description):


def get_url_with_latest_check():
conn = psycopg2.connect(DATABASE_URL)
conn = get_connection()

with conn.cursor(cursor_factory=NamedTupleCursor) as cur:
cur.execute(
Expand Down

0 comments on commit c6e3507

Please sign in to comment.