Skip to content

Commit ffe98ed

Browse files
mokevninclaude
andcommitted
fix: fix ruff lint errors (import order, line length, formatting)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent c90d57f commit ffe98ed

2 files changed

Lines changed: 49 additions & 50 deletions

File tree

‎example.py‎

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,123 +1,122 @@
11
import os
2+
23
import psycopg2
34
from flask import (
4-
get_flashed_messages,
5-
flash,
65
Flask,
6+
flash,
7+
get_flashed_messages,
78
redirect,
89
render_template,
910
request,
10-
url_for
11+
url_for,
1112
)
13+
1214
from user_repository import UserRepository
1315

1416
app = Flask(__name__)
15-
app.config['SECRET_KEY'] = os.getenv('SECRET_KEY')
16-
app.config['DATABASE_URL'] = os.getenv('DATABASE_URL')
17+
app.config["SECRET_KEY"] = os.getenv("SECRET_KEY")
18+
app.config["DATABASE_URL"] = os.getenv("DATABASE_URL")
1719

1820

19-
conn = psycopg2.connect(app.config['DATABASE_URL'])
21+
conn = psycopg2.connect(app.config["DATABASE_URL"])
2022
repo = UserRepository(conn)
2123

2224

23-
@app.route('/')
25+
@app.route("/")
2426
def index():
25-
return render_template('home.html')
27+
return render_template("home.html")
2628

2729

28-
@app.route('/users/')
30+
@app.route("/users/")
2931
def users_get():
3032
messages = get_flashed_messages(with_categories=True)
31-
term = request.args.get('term', '')
33+
term = request.args.get("term", "")
3234
if term:
3335
users = repo.get_by_term(term)
3436
else:
3537
users = repo.get_content()
3638
return render_template(
37-
'users/index.html',
38-
users=users,
39-
search=term,
40-
messages=messages
39+
"users/index.html", users=users, search=term, messages=messages
4140
)
4241

4342

44-
@app.post('/users')
43+
@app.post("/users")
4544
def users_post():
4645
user_data = request.form.to_dict()
4746
errors = validate(user_data)
4847
if errors:
4948
return render_template(
50-
'users/new.html',
49+
"users/new.html",
5150
user=user_data,
5251
errors=errors,
5352
)
5453
repo.save(user_data)
5554

56-
flash('Пользователь успешно добавлен', 'success')
57-
return redirect(url_for('users_get'), code=302)
55+
flash("Пользователь успешно добавлен", "success")
56+
return redirect(url_for("users_get"), code=302)
5857

5958

60-
@app.route('/users/new')
59+
@app.route("/users/new")
6160
def users_new():
62-
user = {'name': '', 'email': ''}
61+
user = {"name": "", "email": ""}
6362
errors = {}
6463
return render_template(
65-
'users/new.html',
64+
"users/new.html",
6665
user=user,
6766
errors=errors,
6867
)
6968

7069

71-
@app.route('/users/<id>/edit')
70+
@app.route("/users/<id>/edit")
7271
def users_edit(id):
7372
user = repo.find(id)
7473
errors = {}
7574

7675
return render_template(
77-
'users/edit.html',
76+
"users/edit.html",
7877
user=user,
7978
errors=errors,
8079
)
8180

8281

83-
@app.route('/users/<id>/patch', methods=['POST'])
82+
@app.route("/users/<id>/patch", methods=["POST"])
8483
def users_patch(id):
8584
user = repo.find(id)
8685
data = request.form.to_dict()
8786

8887
errors = validate(data)
8988
if errors:
9089
return render_template(
91-
'users/edit.html',
90+
"users/edit.html",
9291
user=user,
9392
errors=errors,
9493
), 422
95-
data['id'] = user['id']
94+
data["id"] = user["id"]
9695
repo.save(data)
97-
flash('Пользователь успешно обновлен', 'success')
98-
return redirect(url_for('users_get'))
96+
flash("Пользователь успешно обновлен", "success")
97+
return redirect(url_for("users_get"))
9998

10099

101-
@app.route('/users/<id>/delete', methods=['POST'])
100+
@app.route("/users/<id>/delete", methods=["POST"])
102101
def users_delete(id):
103102
repo.destroy(id)
104-
flash('Пользователь удален', 'success')
105-
return redirect(url_for('users_get'))
103+
flash("Пользователь удален", "success")
104+
return redirect(url_for("users_get"))
106105

107106

108-
@app.route('/users/<id>')
107+
@app.route("/users/<id>")
109108
def users_show(id):
110109
user = repo.find(id)
111110
return render_template(
112-
'users/show.html',
111+
"users/show.html",
113112
user=user,
114113
)
115114

116115

117116
def validate(user):
118117
errors = {}
119-
if not user['name']:
120-
errors['name'] = "Can't be blank"
121-
if not user['email']:
122-
errors['email'] = "Can't be blank"
118+
if not user["name"]:
119+
errors["name"] = "Can't be blank"
120+
if not user["email"]:
121+
errors["email"] = "Can't be blank"
123122
return errors

‎user_repository.py‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ def get_content(self):
1010
cur.execute("SELECT * FROM users")
1111
return cur.fetchall()
1212

13-
def get_by_term(self, search_term=''):
13+
def get_by_term(self, search_term=""):
1414
with self.conn.cursor(cursor_factory=RealDictCursor) as cur:
15-
cur.execute("SELECT * FROM users WHERE name ILIKE %s", (f'%{search_term}%',))
15+
cur.execute(
16+
"SELECT * FROM users WHERE name ILIKE %s",
17+
(f"%{search_term}%",),
18+
)
1619
return cur.fetchall()
1720

1821
def find(self, id):
@@ -21,33 +24,30 @@ def find(self, id):
2124
return cur.fetchone()
2225

2326
def save(self, user_data):
24-
if 'id' not in user_data:
27+
if "id" not in user_data:
2528
id = self._create(user_data)
2629
else:
2730
id = self._update(user_data)
2831
return id
2932

30-
3133
def _update(self, user_data):
3234
with self.conn.cursor() as cur:
3335
cur.execute(
34-
"UPDATE users SET name = %s, email = %s WHERE id = %s",
35-
(user_data['name'], user_data['email'], user_data['id'])
36-
)
36+
"UPDATE users SET name = %s, email = %s WHERE id = %s",
37+
(user_data["name"], user_data["email"], user_data["id"]),
38+
)
3739
self.conn.commit()
38-
return user_data['id']
39-
40+
return user_data["id"]
4041

4142
def _create(self, user_data):
4243
with self.conn.cursor() as cur:
4344
cur.execute(
4445
"INSERT INTO users (name, email) VALUES (%s, %s) RETURNING id",
45-
(user_data['name'], user_data['email'])
46+
(user_data["name"], user_data["email"]),
4647
)
47-
user_data['id'] = cur.fetchone()[0]
48+
user_data["id"] = cur.fetchone()[0]
4849
self.conn.commit()
49-
return user_data['id']
50-
50+
return user_data["id"]
5151

5252
def destroy(self, id):
5353
with self.conn.cursor() as cur:

0 commit comments

Comments
 (0)