Skip to content

Commit

Permalink
init lesson crud-update
Browse files Browse the repository at this point in the history
  • Loading branch information
sgmdlt committed Aug 14, 2024
1 parent 0b2caef commit 2fa50cd
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 2 deletions.
34 changes: 34 additions & 0 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,40 @@ def users_new():
)


@app.route('/users/<id>/edit')
def users_edit(id):
repo = UserRepository()
user = repo.find(id)
errors = {}

return render_template(
'users/edit.html',
user=user,
errors=errors,
)


@app.route('/users/<id>/patch', methods=['POST'])
def users_patch(id):
repo = UserRepository()
user = repo.find(id)
data = request.form.to_dict()

errors = validate(data)
if errors:
return render_template(
'users/edit.html',
user=user,
errors=errors,
), 422

user['name'] = data['name']
user['email'] = data['email']
repo.save(user)
flash('Пользователь успешно обновлен', 'success')
return redirect(url_for('users_get'))


@app.route('/users/<id>')
def users_show(id):
repo = UserRepository()
Expand Down
27 changes: 27 additions & 0 deletions templates/users/edit.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<h1>Редактировать пользователя</h1>
<hr>
{% if errors %}
<ul class=flashes>
{% for _, error in errors.items() %}
<li class="error">{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
<hr>
<form action="{{ url_for('users_patch', id=user.id) }}" method="post">
<div>
<label for="user-nickname">
Имя:
<input type="text" id="user-name" name="name" value="{{ user.name }}">
</label>
</div>
<div>
<label for="user-email">
Email:
<input type="text" id="user-email" name="email" value="{{ user.email }}">
</label>
</div>
<div>
<button type="submit" title="Submit form">Обновить</button>
</div>
</form>
1 change: 1 addition & 0 deletions templates/users/new.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ <h1>Новый пользователь</h1>
{% endfor %}
</ul>
{% endif %}
<hr>
<form action="{{ url_for('users_post') }}" method="post">
<div>
<label for="user-nickname">
Expand Down
2 changes: 2 additions & 0 deletions templates/users/show.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<div>Имя = {{ user.name }}</div>
<div>Email = {{ user.email }}</div>

<a href="{{ url_for('users_edit', id=user.id) }}">Редактировать пользователя</a>

<p><a href="{{ url_for('users_get') }}">Назад к списку пользователей</a></p>
7 changes: 5 additions & 2 deletions user_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sys
import uuid


class UserRepository():
def __init__(self):
self.users = json.load(open("./users.json", 'r'))
Expand All @@ -24,8 +25,10 @@ def save(self, new_user):
raise Exception(f'Wrong data: {json.loads(new_user)}')
# replace already existed user
if new_user.get('id'):
current_user = self.find(new_user['id'])
self.users.pop(current_user)
for current in self.users:
if new_user['id'] == current['id']:
del current
break
self.users.append(new_user)
# or add new
else:
Expand Down

0 comments on commit 2fa50cd

Please sign in to comment.