|
1 | 1 | import os |
| 2 | + |
2 | 3 | import psycopg2 |
3 | 4 | from flask import ( |
4 | | - get_flashed_messages, |
5 | | - flash, |
6 | 5 | Flask, |
| 6 | + flash, |
| 7 | + get_flashed_messages, |
7 | 8 | redirect, |
8 | 9 | render_template, |
9 | 10 | request, |
10 | | - url_for |
| 11 | + url_for, |
11 | 12 | ) |
| 13 | + |
12 | 14 | from user_repository import UserRepository |
13 | 15 |
|
14 | 16 | 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") |
17 | 19 |
|
18 | 20 |
|
19 | | -conn = psycopg2.connect(app.config['DATABASE_URL']) |
| 21 | +conn = psycopg2.connect(app.config["DATABASE_URL"]) |
20 | 22 | repo = UserRepository(conn) |
21 | 23 |
|
22 | 24 |
|
23 | | -@app.route('/') |
| 25 | +@app.route("/") |
24 | 26 | def index(): |
25 | | - return render_template('home.html') |
| 27 | + return render_template("home.html") |
26 | 28 |
|
27 | 29 |
|
28 | | -@app.route('/users/') |
| 30 | +@app.route("/users/") |
29 | 31 | def users_get(): |
30 | 32 | messages = get_flashed_messages(with_categories=True) |
31 | | - term = request.args.get('term', '') |
| 33 | + term = request.args.get("term", "") |
32 | 34 | if term: |
33 | 35 | users = repo.get_by_term(term) |
34 | 36 | else: |
35 | 37 | users = repo.get_content() |
36 | 38 | 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 |
41 | 40 | ) |
42 | 41 |
|
43 | 42 |
|
44 | | -@app.post('/users') |
| 43 | +@app.post("/users") |
45 | 44 | def users_post(): |
46 | 45 | user_data = request.form.to_dict() |
47 | 46 | errors = validate(user_data) |
48 | 47 | if errors: |
49 | 48 | return render_template( |
50 | | - 'users/new.html', |
| 49 | + "users/new.html", |
51 | 50 | user=user_data, |
52 | 51 | errors=errors, |
53 | 52 | ) |
54 | 53 | repo.save(user_data) |
55 | 54 |
|
56 | | - flash('Пользователь успешно добавлен', 'success') |
57 | | - return redirect(url_for('users_get'), code=302) |
| 55 | + flash("Пользователь успешно добавлен", "success") |
| 56 | + return redirect(url_for("users_get"), code=302) |
58 | 57 |
|
59 | 58 |
|
60 | | -@app.route('/users/new') |
| 59 | +@app.route("/users/new") |
61 | 60 | def users_new(): |
62 | | - user = {'name': '', 'email': ''} |
| 61 | + user = {"name": "", "email": ""} |
63 | 62 | errors = {} |
64 | 63 | return render_template( |
65 | | - 'users/new.html', |
| 64 | + "users/new.html", |
66 | 65 | user=user, |
67 | 66 | errors=errors, |
68 | 67 | ) |
69 | 68 |
|
70 | 69 |
|
71 | | -@app.route('/users/<id>/edit') |
| 70 | +@app.route("/users/<id>/edit") |
72 | 71 | def users_edit(id): |
73 | 72 | user = repo.find(id) |
74 | 73 | errors = {} |
75 | 74 |
|
76 | 75 | return render_template( |
77 | | - 'users/edit.html', |
| 76 | + "users/edit.html", |
78 | 77 | user=user, |
79 | 78 | errors=errors, |
80 | 79 | ) |
81 | 80 |
|
82 | 81 |
|
83 | | -@app.route('/users/<id>/patch', methods=['POST']) |
| 82 | +@app.route("/users/<id>/patch", methods=["POST"]) |
84 | 83 | def users_patch(id): |
85 | 84 | user = repo.find(id) |
86 | 85 | data = request.form.to_dict() |
87 | 86 |
|
88 | 87 | errors = validate(data) |
89 | 88 | if errors: |
90 | 89 | return render_template( |
91 | | - 'users/edit.html', |
| 90 | + "users/edit.html", |
92 | 91 | user=user, |
93 | 92 | errors=errors, |
94 | 93 | ), 422 |
95 | | - data['id'] = user['id'] |
| 94 | + data["id"] = user["id"] |
96 | 95 | repo.save(data) |
97 | | - flash('Пользователь успешно обновлен', 'success') |
98 | | - return redirect(url_for('users_get')) |
| 96 | + flash("Пользователь успешно обновлен", "success") |
| 97 | + return redirect(url_for("users_get")) |
99 | 98 |
|
100 | 99 |
|
101 | | -@app.route('/users/<id>/delete', methods=['POST']) |
| 100 | +@app.route("/users/<id>/delete", methods=["POST"]) |
102 | 101 | def users_delete(id): |
103 | 102 | repo.destroy(id) |
104 | | - flash('Пользователь удален', 'success') |
105 | | - return redirect(url_for('users_get')) |
| 103 | + flash("Пользователь удален", "success") |
| 104 | + return redirect(url_for("users_get")) |
106 | 105 |
|
107 | 106 |
|
108 | | -@app.route('/users/<id>') |
| 107 | +@app.route("/users/<id>") |
109 | 108 | def users_show(id): |
110 | 109 | user = repo.find(id) |
111 | 110 | return render_template( |
112 | | - 'users/show.html', |
| 111 | + "users/show.html", |
113 | 112 | user=user, |
114 | 113 | ) |
115 | 114 |
|
116 | 115 |
|
117 | 116 | def validate(user): |
118 | 117 | 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" |
123 | 122 | return errors |
0 commit comments