Skip to content

Commit a8df79d

Browse files
committed
fix app
1 parent 0b431a0 commit a8df79d

4 files changed

Lines changed: 54 additions & 41 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ run:
1717
prod:
1818
poetry run gunicorn --workers=4 --bind 0.0.0.0:$(PORT) example:app --log-file -
1919

20-
compose-production-run-app:
20+
compose-production-run:
2121
docker compose -p python_page_analyzer_ru-production down
2222
docker compose -p python_page_analyzer_ru-production build
2323
docker compose -p python_page_analyzer_ru-production up

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ make install
1616
# run linter
1717
make lint
1818

19-
# run localy
20-
make run
19+
# run in docker
20+
make compose-production-run
2121
```
2222

2323

example.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import psycopg2
23
from flask import (
34
get_flashed_messages,
45
flash,
@@ -14,7 +15,9 @@
1415
app.config['SECRET_KEY'] = os.getenv('SECRET_KEY')
1516
app.config['DATABASE_URL'] = os.getenv('DATABASE_URL')
1617

17-
repo = UserRepository(app.config['DATABASE_URL'])
18+
19+
conn = psycopg2.connect(app.config['DATABASE_URL'])
20+
repo = UserRepository(conn)
1821

1922

2023
@app.route('/')
@@ -26,11 +29,13 @@ def index():
2629
def users_get():
2730
messages = get_flashed_messages(with_categories=True)
2831
term = request.args.get('term', '')
29-
users = repo.get_content()
30-
filtered_users = [user for user in users if term in user['name']]
32+
if term:
33+
users = repo.get_by_term(term)
34+
else:
35+
users = repo.get_content()
3136
return render_template(
3237
'users/index.html',
33-
users=filtered_users,
38+
users=users,
3439
search=term,
3540
messages=messages
3641
)

user_repository.py

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,55 @@
1-
import psycopg2
21
from psycopg2.extras import RealDictCursor
32

43

54
class UserRepository:
6-
def __init__(self, db_url):
7-
self.db_url = db_url
8-
9-
def get_connection(self):
10-
return psycopg2.connect(self.db_url)
5+
def __init__(self, conn):
6+
self.conn = conn
117

128
def get_content(self):
13-
with self.get_connection() as conn:
14-
with conn.cursor(cursor_factory=RealDictCursor) as cur:
15-
cur.execute("SELECT * FROM users")
16-
return cur.fetchall()
9+
with self.conn.cursor(cursor_factory=RealDictCursor) as cur:
10+
cur.execute("SELECT * FROM users")
11+
return cur.fetchall()
12+
13+
def get_by_term(self, search_term=''):
14+
with self.conn.cursor(cursor_factory=RealDictCursor) as cur:
15+
cur.execute("SELECT * FROM users WHERE name ILIKE %s", (f'%{search_term}%',))
16+
return cur.fetchall()
1717

1818
def find(self, id):
19-
with self.get_connection() as conn:
20-
with conn.cursor(cursor_factory=RealDictCursor) as cur:
21-
cur.execute("SELECT * FROM users WHERE id = %s", (id,))
22-
return cur.fetchone()
19+
with self.conn.cursor(cursor_factory=RealDictCursor) as cur:
20+
cur.execute("SELECT * FROM users WHERE id = %s", (id,))
21+
return cur.fetchone()
2322

2423
def save(self, user_data):
25-
with self.get_connection() as conn:
26-
with conn.cursor() as cur:
27-
if 'id' not in user_data:
28-
# New user
29-
cur.execute(
30-
"INSERT INTO users (name, email) VALUES (%s, %s) RETURNING id",
31-
(user_data['name'], user_data['email'])
32-
)
33-
user_data['id'] = cur.fetchone()[0]
34-
else:
35-
# Existing user
36-
cur.execute(
37-
"UPDATE users SET name = %s, email = %s WHERE id = %s",
38-
(user_data['name'], user_data['email'], user_data['id'])
39-
)
40-
conn.commit()
24+
if 'id' not in user_data:
25+
id = self._create(user_data)
26+
else:
27+
id = self._update(user_data)
28+
return id
29+
30+
31+
def _update(self, user_data):
32+
with self.conn.cursor() as cur:
33+
cur.execute(
34+
"UPDATE users SET name = %s, email = %s WHERE id = %s",
35+
(user_data['name'], user_data['email'], user_data['id'])
36+
)
37+
self.conn.commit()
4138
return user_data['id']
4239

40+
41+
def _create(self, user_data):
42+
with self.conn.cursor() as cur:
43+
cur.execute(
44+
"INSERT INTO users (name, email) VALUES (%s, %s) RETURNING id",
45+
(user_data['name'], user_data['email'])
46+
)
47+
user_data['id'] = cur.fetchone()[0]
48+
self.conn.commit()
49+
return user_data['id']
50+
51+
4352
def destroy(self, id):
44-
with self.get_connection() as conn:
45-
with conn.cursor() as cur:
46-
cur.execute("DELETE FROM users WHERE id = %s", (id,))
47-
conn.commit()
53+
with self.conn.cursor() as cur:
54+
cur.execute("DELETE FROM users WHERE id = %s", (id,))
55+
self.conn.commit()

0 commit comments

Comments
 (0)