Skip to content

Commit

Permalink
Merge pull request #538 from moevm/531_page_with_admin_pages
Browse files Browse the repository at this point in the history
admin_pages_list.html is added
  • Loading branch information
HadronCollider authored Jun 20, 2024
2 parents f54ac44 + 2888c3a commit e275cef
Show file tree
Hide file tree
Showing 12 changed files with 604 additions and 2 deletions.
17 changes: 17 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 Expand Up @@ -306,6 +308,21 @@ def get_logs_cursor(filter={}, limit=10, offset=0, sort=None, order=None):

return rows, count

def get_user_cursor(filter={}, limit=10, offset=0, sort=None, order=None):
sort = 'username' if sort == 'username' else sort

count = users_collection.count_documents(filter)
rows = users_collection.find(filter)

if sort and order in ("asc, desc"):
rows = rows.sort(sort, pymongo.ASCENDING if order ==
"asc" else pymongo.DESCENDING)

rows = rows.skip(offset) if offset else rows
rows = rows.limit(limit) if limit else rows

return rows, count


# Get stats for one user, return a list in the form
# [check_id, login, time of check_id's creation, result(0/1)]
Expand Down
2 changes: 1 addition & 1 deletion app/routes/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def my_wrapper(*args, **kwargs):
@admin.route('/', methods=["GET"])
@admin_required
def index():
return "There will be a list of all admin pages here"
return render_template('admin_pages_list.html')


@admin.route('/criterions', methods=["GET"])
Expand Down
91 changes: 91 additions & 0 deletions app/routes/users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import json
from flask import abort, Blueprint, render_template, request, jsonify
from flask_login import current_user
from functools import wraps
from app.db.db_methods import get_all_users, get_user
from utils import checklist_filter, format_check_for_table
from db import db_methods

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("/data")
@admin_required
def users_data():
filters = request.args.get("filter", "{}")
try:
filters = json.loads(filters)
filters = filters if filters else {}
except Exception as e:
# logger.warning("Can't parse filters")
# logger.warning(repr(e))
filters = {}
filter_query = {}
if f_username := filters.get("username", None):
filter_query["username"] = {"$regex": f_username}

if f_name := filters.get("name", None):
filter_query["name"] = {"$regex": f_name}

if f_formats := filters.get("all_formats", None):
filter_query["formats"] = {"$regex": f_formats}

if f_criteria := filters.get("all_criteria", None):
filter_query["criteria"] = {"$regex": f_criteria}

if f_check_counts := filters.get("check_counts", None):
try:
f_check_counts_value, f_check_counts_cond = int(f_check_counts.split()[1]), f_check_counts.split()[0]
if f_check_counts_cond == '>':
filter_query["$expr"] = {"$gte": [{"$size": "$presentations"}, f_check_counts_value]}
elif f_check_counts_cond == "<":
filter_query["$expr"] = {"$lte": [{"$size": "$presentations"}, f_check_counts_value]}
except ValueError:
pass

limit = request.args.get("limit", "")
limit = int(limit) if limit.isnumeric() else 10

offset = request.args.get("offset", "")
offset = int(offset) if offset.isnumeric() else 0

sort = request.args.get("sort", "")
sort = 'username' if not sort else sort

order = request.args.get("order", "")
order = 'username' if not order else order

rows, count = db_methods.get_user_cursor(filter=filter_query, limit=limit, offset=offset, sort=sort, order=order)

response = {
"total": count,
"rows": [{
"username": item["username"],
"name": item["name"],
"all_formats": item["formats"],
"all_criteria": item["criteria"],
"check_counts": len(item["presentations"]),

} for item in rows]
}
return jsonify(response)


@users.route('/', methods=["GET"])
@admin_required
def index():
return render_template('user_list.html')

@users.route('/<username>', methods=["GET"])
@admin_required
def user_info(username):
user_info = get_user(username)
return render_template('one_user_info.html', user_info=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/admin_pages_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{% extends "root.html" %}

{% block title %}Список страниц для администраторов{% endblock %}

{% block main %}

<div class="header row">{% include "header.html" %}</div>
<div class="justify-content-left">
<h3 class="texteous ins">Список страниц для администраторов:</h3>
<div class="text-left" style="margin-left: 2em;">
<li><a class="linked text-left" href="/admin/criterions">Таблица с информацией о критериях</a></li>
<li><a class="linked text-left" href="/users">Таблица с информацией о пользователях</a></li>
</div>
</div>

{% endblock main %}
1 change: 1 addition & 0 deletions app/templates/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<a class="dropdown-item" href="/criterion_packs">Наборы критериев</a>
<a class="dropdown-item" href="/logs">Логи</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="/admin">Список админ-страниц</a>
<a class="dropdown-item" href="/monitoring/" target="_blank" rel="noopener noreferrer">Celery</a>
<a class="dropdown-item" href="/version" target="_blank" rel="noopener noreferrer">Version</a>
{% endif %}
Expand Down
65 changes: 65 additions & 0 deletions app/templates/one_user_info.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{# Accepts: header dependicies, results, id, filename #}


{% extends "root.html" %}

{% block title %}Информация о пользователях{% endblock %}

{% block main %}
<style>
.fht-cell {
margin: 0 0.2rem 0.2rem;
}

/* .user-list-table th td { border: #54585d; } */

#user-list-table {
table-layout: fixed;
word-wrap: break-word;
}

#user-list-table.table-bordered,
#user-list-table.table-bordered > thead > tr > th,
#user-list-table.table-bordered > tbody > tr > td {
border: 1px solid #a1a1a1;
}
</style>

<div class="header row">{% include "header.html" %}</div>
<div class="holder row">
<div class="container-fluid table-responsive-xl">
<h3 id="user_title" class="texteous ins">
Страница пользователя: <b>{{ user_info.username }}</b>
</h3>
<a href="{{ url_for('check_list', filter_user=user_info.username) }}"
class="col text-center link">Список всех загрузок пользователя</a>
<table id="one-user-table" class="table bg-white"
data-filter-control="true"
data-pagination="true"
data-page-list="[5, 10, 25, 50, All]"
data-ajax="ajaxRequest"
data-query-params="queryParams"
data-show-refresh="false"
data-auto-refresh="true"
data-auto-refresh-interval="10"
data-show-auto-refresh="false"
data-side-pagination="server"
data-icon-size="lg"
data-buttons="buttons"
data-show-filter-control-switch="true"
data-show-button-icons="false"
data-show-button-text="true">
<thead>
<tr>
<th data-field="username" data-filter-control="input" data-sortable="true">Username</th>
<th data-field="name" data-filter-control="input" data-sortable="true">Name</th>
<th data-field="all_formats" data-filter-control="input" data-sortable="true">Formats</th>
<th data-field="all_criteria" data-filter-control="input" data-sortable="true">Criteria</th>
<th data-field="check_counts" data-filter-control="input" data-sortable="true" title="insert operator and value with space (f.e > 1000)">Count of checks</th>
</tr>
</thead>
</table>
</div>
</div>

{% endblock main %}
65 changes: 65 additions & 0 deletions app/templates/user_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{# Accepts: header dependicies, results, id, filename #}


{% extends "root.html" %}

{% block title %}Информация о пользователях{% endblock %}

{% block main %}
<style>
.fht-cell {
margin: 0 0.2rem 0.2rem;
}

/* .user-list-table th td { border: #54585d; } */

#user-list-table {
table-layout: fixed;
word-wrap: break-word;
}

#user-list-table.table-bordered,
#user-list-table.table-bordered > thead > tr > th,
#user-list-table.table-bordered > tbody > tr > td {
border: 1px solid #a1a1a1;
}
</style>

<div class="header row">{% include "header.html" %}</div>
<div class="holder row">
<div class="container-fluid table-responsive-xl">
<h3 id="user_title" class="texteous ins">
Список пользователей:
</h3>
<table id="user-list-table" class="table bg-white"
data-filter-control="true"
data-pagination="true"
data-page-list="[5, 10, 25, 50, All]"
data-ajax="ajaxRequest"
data-query-params="queryParams"
data-show-refresh="false"
data-auto-refresh="true"
data-auto-refresh-interval="10"
data-show-auto-refresh="false"
data-side-pagination="server"
data-icon-size="lg"
data-buttons="buttons"
data-show-filter-control-switch="true"
data-show-button-icons="false"
data-show-button-text="true">
<thead>
<tr>
<th data-field="username" data-filter-control="input" data-sortable="true">Username</th>
<th data-field="name" data-filter-control="input" data-sortable="true">Name</th>
<th data-field="all_formats" data-filter-control="input" data-sortable="true">Formats</th>
<th data-field="all_criteria" data-filter-control="input" data-sortable="true">Criteria</th>
<th data-field="check_counts" data-filter-control="input" data-sortable="true" title="insert operator and value with space (f.e > 1000)">Count of checks</th>
</tr>
</thead>
</table>
</div>
</div>

{% endblock main %}


2 changes: 1 addition & 1 deletion assets/scripts/check_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,4 @@ function downdloadBlob(blob, filename) {
document.body.appendChild(a);
a.click();
a.remove();
}
}
2 changes: 2 additions & 0 deletions assets/scripts/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import './version';
import './check_list';
import './logs';
import './admin_criterions';
import './user_list';
import './one_user_info'

import '../favicon.ico';
import '../styles/404.css';
Expand Down
Loading

0 comments on commit e275cef

Please sign in to comment.