Skip to content

Commit

Permalink
start for /users
Browse files Browse the repository at this point in the history
  • Loading branch information
MarinaProsche committed Apr 11, 2024
1 parent c6383cb commit a01c910
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/db/db_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ def get_user(username):
else:
return None

def get_all_users():
return users_collection.find()

# Returns True if user was found and updated and false if not (username can not be changed!)
def edit_user(user):
Expand Down
29 changes: 29 additions & 0 deletions app/routes/users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from flask import abort, Blueprint, render_template
from flask_login import current_user
from functools import wraps
from app.db.db_methods import get_all_users, get_user

users = Blueprint('users', __name__, template_folder='templates', static_folder='static')


def admin_required(route_func):
@wraps(route_func)
def my_wrapper(*args, **kwargs):
if current_user and current_user.is_admin:
return route_func(*args, **kwargs)
abort(403)
return my_wrapper


@users.route('/', methods=["GET"])
@admin_required
def index():
users = list(get_all_users())
usernames = [(user['name'], user['username']) for user in users]
return render_template('user_list.html', usernames=usernames)

@users.route('/<username>', methods=["GET"])
@admin_required
def user_info(username):
user_info = get_user(username)
return f"{user_info}"
2 changes: 2 additions & 0 deletions app/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from utils import checklist_filter, decorator_assertion, get_file_len, format_check
from app.main.checks import CRITERIA_INFO
from routes.admin import admin
from routes.users import users

logger = get_root_logger('web')
UPLOAD_FOLDER = '/usr/src/project/files'
Expand All @@ -52,6 +53,7 @@
app.config['CELERY_BROKER_URL'] = os.environ.get("CELERY_BROKER_URL", "redis://localhost:6379")

app.register_blueprint(admin, url_prefix='/admin')
app.register_blueprint(users, url_prefix='/users')


app.logger.addHandler(get_logging_stdout_handler())
Expand Down
16 changes: 16 additions & 0 deletions app/templates/user_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>User List</title>
</head>
<body>
<h1>Список пользователей</h1>
<ul>
{% for name, username in usernames %}
<p><a href="{{ url_for('users.user_info', username=username) }}">{{ name }}</a></p>
{% endfor %}
</ul>
</body>
</html>

0 comments on commit a01c910

Please sign in to comment.