Skip to content

Commit

Permalink
Merge pull request #5 from hexlet-components/flash
Browse files Browse the repository at this point in the history
Flash
  • Loading branch information
sgmdlt authored Aug 14, 2024
2 parents 67ae564 + 6af4bc3 commit 1221949
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 25 deletions.
18 changes: 15 additions & 3 deletions example.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,38 @@
import json
import uuid
from flask import Flask, redirect, render_template, request
from flask import (
get_flashed_messages,
flash,
Flask,
redirect,
render_template,
request,
url_for
)

app = Flask(__name__)
app.secret_key = "secret_key"

users = json.load(open("./users.json", 'r'))


@app.route('/')
def hello_world():
def index():
return 'Welcome to Flask!'


@app.route('/users/')
def users_get():
with open("./users.json", "r") as f:
users = json.load(f)
messages = get_flashed_messages(with_categories=True)
term = request.args.get('term', '')
filtered_users = [user for user in users if term in user['name']]
return render_template(
'users/index.html',
users=filtered_users,
search=term,
messages=messages
)


Expand All @@ -44,7 +55,8 @@ def users_post():
users.append(user)
with open("./users.json", "w") as f:
json.dump(users, f)
return redirect('/users', code=302)
flash('Пользователь успешно добавлен', 'success')
return redirect(url_for('users_get'), code=302)


@app.route('/users/new')
Expand Down
15 changes: 12 additions & 3 deletions templates/users/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,25 @@
{% block title %}Пользователи{% endblock %}

{% block content %}
<form action="/users" method="get">

{% if messages %}
<ul class=flashes>
{% for category, message in messages %}
<li class="{{ category }}">{{ message }}</li>
{% endfor %}
</ul>
{% endif %}

<form action="{{ url_for('users_get') }}" method="get">
<input type="search" name="term" value="{{ search }}" />
<input type="submit" value="Search" />
</form>

<a href="/users/new">Добавить пользователя</a>
<a href="{{ url_for('users_new') }}">Добавить пользователя</a>

<ul>
{% for user in users %}
<li>Имя: <a href="/users/{{user.id}}">{{ user.name }}</a>, Email: {{ user.email}}</li>
<li>Имя: <a href="{{ url_for('show_user', id=user.id)}}"> {{ user.name }}</a>, Email: {{ user.email}}</li>
{% endfor %}
</ul>
{% endblock %}
4 changes: 2 additions & 2 deletions templates/users/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
<div id="sidebar">
{% block sidebar %}
<ul>
<li><a href="/">Главная</a></li>
<li><a href="/users/">Пользователи</a></li>
<li><a href="{{ url_for('index') }}">Главная</a></li>
<li><a href="{{ url_for('users_get') }}">Пользователи</a></li>
</ul>
{% endblock %}
</div>
Expand Down
19 changes: 10 additions & 9 deletions templates/users/new.html
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
<h1>Новый пользователь</h1>
<hr>
<form action="/users" method="post">
<div>
{% if errors %}
<ul class=flashes>
{% for _, error in errors.items() %}
<li class="error">{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
<form action="{{ url_for('users_post') }}" method="post">
<div>
<label for="user-nickname">
Имя:
<input type="text" id="user-name" name="name" value="{{ user.name }}">
</label>
{% if errors['name'] %}
<div>{{ errors['name'] }}</div>
{% endif %}
</div>
<div>
<label for="user-email">
Email:
<input type="text" id="user-email" name="email" value="{{ user.email }}">
</label>
{% if errors['email'] %}
<div>{{ errors['email'] }}</div>
{% endif %}
</div>
<div>
<button type="submit" title="Submit form">Добавить</button>
</div>
</form>

<p><a href="/users">Назад к списку пользователей</a></p>
<p><a href="{{ url_for('users_get') }}">Назад к списку пользователей</a></p>
2 changes: 1 addition & 1 deletion templates/users/show.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div>Имя = {{ user.name }}</div>
<div>Email = {{ user.email }}</div>

<p><a href="/users">Назад к списку пользователей</a></p>
<p><a href="{{ url_for('users_get') }}">Назад к списку пользователей</a></p>
8 changes: 1 addition & 7 deletions users.json
Original file line number Diff line number Diff line change
@@ -1,7 +1 @@
[
{ "id": 1, "name": "mike" },
{ "id": 2, "name": "mishel" },
{ "id": 3, "name": "adel" },
{ "id": 4, "name": "keks" },
{ "id": 5, "name": "kamila" }
]
[{"id": 1, "name": "mike"}, {"id": 2, "name": "mishel"}, {"id": 3, "name": "adel"}, {"id": 4, "name": "keks"}, {"id": 5, "name": "kamila"}, {"id": "986e398a-b9ac-4195-bf40-bc85e57da274", "name": "dfdf", "email": "dfdgdgf"}, {"id": "4e409df7-c732-4075-a117-de013b464c69", "name": "13", "email": "34"}]

0 comments on commit 1221949

Please sign in to comment.