Skip to content

Commit

Permalink
add users filters
Browse files Browse the repository at this point in the history
  • Loading branch information
denisova-ok committed Dec 22, 2024
1 parent f36a723 commit 83ee9da
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 51 deletions.
Binary file modified server/src/__pycache__/utils.cpython-39.pyc
Binary file not shown.
5 changes: 4 additions & 1 deletion server/src/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
def get_actions_types():
return {0: 'заполнил(a) анкету', 1: 'отправил(а) чек оплаты на проверку организатору',
2: 'отправил(а) подарок', 3: 'получил(а) подарок'}
2: 'отправил(а) подарок', 3: 'получил(а) подарок'}

def get_delivery_types():
return {'none': 0, 'ticket': 1, 'cert': 2, 'mail': 3, 'other': 4}
163 changes: 122 additions & 41 deletions server/static/js/user_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const favDialog = document.getElementById("favDialog");
const dialog = document.querySelector("dialog");
const Button2 = document.getElementById("closeF2");
const FD2 = document.getElementById("FDialog2");
const filterButton = document.getElementById('Bfilter');

Button.addEventListener('click', () => {
dialog.close();
Expand All @@ -12,16 +13,121 @@ Button2.addEventListener('click', () => {
FD2.close();
})

window.addEventListener("load", async function(){
await fetch(new URL('/get_list_people', 'http://localhost:8000').href, {
method: "GET",
filterButton.addEventListener('click', applyFilters);


window.addEventListener("load", init);

function Filter2(){
FD2.showModal();
}

document.querySelectorAll('input[type="radio"][name="role[]"]').forEach(radio => {
radio.addEventListener('change', () => {
if (document.getElementById('org').checked){
document.getElementById('1A').disabled = true;
document.getElementById('2A').disabled = true;
document.getElementById('3A').disabled = true;
document.getElementById('1S').disabled = true;
document.getElementById('2S').disabled = true;
document.getElementById('3S').disabled = true;
document.getElementById('4S').disabled = true;
document.getElementById('name-player').disabled = true;
document.getElementById('address').disabled = true;
document.getElementById('wishlist').disabled = true;
document.getElementById('stoplist').disabled = true;
}
if (document.getElementById('play').checked){
document.getElementById('name-player').disabled = false;
document.getElementById('address').disabled = false;
document.getElementById('wishlist').disabled = false;
document.getElementById('stoplist').disabled = false;
document.getElementById('1A').disabled = false;
document.getElementById('2A').disabled = false;
document.getElementById('3A').disabled = false;
document.getElementById('1S').disabled = false;
document.getElementById('2S').disabled = false;
document.getElementById('3S').disabled = false;
document.getElementById('4S').disabled = false;
}
});
});

async function applyFilters() {
if (!document.getElementById('org').checked && !document.getElementById('play').checked) {
let request = new XMLHttpRequest();
request.open('GET', 'http://localhost:8000/get_list_people', false);
request.send(null);

let data = [];
if (request.status == 200) {
data = JSON.parse(request.responseText);
}
insertInfo(data);
}
if (document.getElementById('org').checked) {
let request = new XMLHttpRequest();
request.open('GET', 'http://localhost:8000/get_hosts', false);
request.send(null);

let data = [];
if (request.status == 200) {
data = JSON.parse(request.responseText);
}
insertInfo(data);
}
if (document.getElementById('play').checked) {
let status = -1;
if (document.getElementById('1A').checked) {
status = 0;
}
if (document.getElementById('2A').checked) {
status = 1;
}
if (document.getElementById('3A').checked) {
status = 2;
}

let delivery_type = 'none';
if (document.getElementById('1S').checked) {
delivery_type = 'ticket';
}
if (document.getElementById('2S').checked) {
delivery_type = 'cert';
}
if (document.getElementById('3S').checked) {
delivery_type = 'mail';
}
if (document.getElementById('4S').checked) {
delivery_type = 'other';
}

let name = document.getElementById('name-player').value;
let address = document.getElementById('address').value;
let wishlist = document.getElementById('wishlist').value;
let stoplist = document.getElementById('stoplist').value;

let filtersInfo = {status: status, delivery_type: delivery_type,
name: name, address: address, wishlist: wishlist,
stoplist: stoplist};
await fetch(new URL('/get_filtered_players', 'http://localhost:8000').href, {
method: "POST",
headers: {
'Accept': 'application/json',
"Content-type": "application/json; charset=UTF-8"
}})
.then(res => res.json())
.then(content => {
},
body: JSON.stringify(filtersInfo)
})
.then(res => res.json())
.then(content => insertInfo(content))
}

FD2.close();
}

function insertInfo(content) {
let list = this.document.getElementById('list_people');
list.innerHTML = "";
for (let i =0; i < content.length; i++){
let div = document.createElement('div');
div.style.display = "flex";
Expand Down Expand Up @@ -70,42 +176,17 @@ window.addEventListener("load", async function(){
favDialog.showModal();
});
}
})
});

function Filter2(){
FD2.showModal();
}

document.querySelectorAll('input[type="radio"][name="role[]"]').forEach(radio => {
radio.addEventListener('change', () => {
if (document.getElementById('org').checked){
document.getElementById('1A').disabled = true;
document.getElementById('2A').disabled = true;
document.getElementById('3A').disabled = true;
document.getElementById('1S').disabled = true;
document.getElementById('2S').disabled = true;
document.getElementById('3S').disabled = true;
document.getElementById('4S').disabled = true;
document.getElementById('name-player').disabled = true;
document.getElementById('address').disabled = true;
document.getElementById('wishlist').disabled = true;
document.getElementById('stoplist').disabled = true;
}
if (document.getElementById('play').checked){
document.getElementById('name-player').disabled = false;
document.getElementById('address').disabled = false;
document.getElementById('wishlist').disabled = false;
document.getElementById('stoplist').disabled = false;
document.getElementById('1A').disabled = false;
document.getElementById('2A').disabled = false;
document.getElementById('3A').disabled = false;
document.getElementById('1S').disabled = false;
document.getElementById('2S').disabled = false;
document.getElementById('3S').disabled = false;
document.getElementById('4S').disabled = false;
}
});
});
async function init() {
await fetch(new URL('/get_list_people', 'http://localhost:8000').href, {
method: "GET",
headers: {
'Accept': 'application/json',
"Content-type": "application/json; charset=UTF-8"
}})
.then(res => res.json())
.then(content => insertInfo(content))
}


8 changes: 4 additions & 4 deletions server/templates/list_people.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@
<div style="display: flex; flex-direction: column; margin-left: 4%;">
<span style="text-align: center;"><b>Тип подарка</b></span>
<div id="type-send">
<div class="checkbox"><label><input type="checkbox" name="type_send[]" value="1" id="1S"> Билет</label></div>
<div class="checkbox"><label><input type="checkbox" name="type_send[]" value="2" id="2S"> Сертификат</label></div>
<div class="checkbox"><label><input type="checkbox" name="type_send[]" value="3" id="3S"> Подарок по почте</label></div>
<div class="checkbox"><label><input type="checkbox" name="type_send[]" value="4" id="4S"> Подарок другими сервисами</label></div>
<div class="radio"><label><input type="radio" name="type_send[]" value="1" id="1S"> Билет</label></div>
<div class="radio"><label><input type="radio" name="type_send[]" value="2" id="2S"> Сертификат</label></div>
<div class="radio"><label><input type="radio" name="type_send[]" value="3" id="3S"> Подарок по почте</label></div>
<div class="radio"><label><input type="radio" name="type_send[]" value="4" id="4S"> Подарок другими сервисами</label></div>
</div>
</div>
</div>
Expand Down
67 changes: 62 additions & 5 deletions server/web_server.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import flask
from flask import Flask
import json
import re
import datetime
from db import database
from src.utils import get_actions_types
from src.utils import get_actions_types, get_delivery_types

db = database.Database("mongodb://root:example@db:27017/", "secret_santa_db")
app = Flask(__name__)
Expand Down Expand Up @@ -68,7 +69,7 @@ def host(id):

@app.route('/get_list_people')
def get_list_people():
return db.users_list()
return db.users_list()
# return [{
# #'_id': ObjectId('6722d278ee5e086ddmeff8b0'),
# 'id': 1,
Expand All @@ -91,8 +92,8 @@ def get_list_people():
# 'phone': 89999999999,
# 'status': 1,
# 'delivery_type': 1,
# 'wishlist': 'flowers',
# 'stoplist': 'candles',
# 'wishlist': 'sweets, computer',
# 'stoplist': 'books ',
# 'recipient': 3,
# 'santa': 3,
# 'got_gift': False,
Expand All @@ -112,7 +113,7 @@ def get_list_people():
# 'phone': 89999999999,
# 'status': 1,
# 'delivery_type': 1,
# 'wishlist': 'flowers',
# 'wishlist': 'flowers;money',
# 'stoplist': 'candles',
# 'recipient': 2,
# 'santa': 2,
Expand Down Expand Up @@ -348,6 +349,62 @@ def get_game_statistics_sends(game_id):
return [sends, len(game['users']) - 1 - sends]
return [0, 0]

@app.route('/get_hosts')
def get_hosts():
users = get_list_people()
hosts = []
for user in users:
if user['is_host']:
hosts.append(user)
return hosts

@app.route('/get_filtered_players', methods=['POST'])
def get_filtered_players():
filters_info = json.loads(flask.request.data)
delivery_types = get_delivery_types()
users = get_list_people()
filtered_users = []
delimiters = [' ', ',', ';']
pattern = '|'.join(map(re.escape, delimiters))

for user in users:
if user['is_host']:
continue
if filters_info['status'] > -1:
if user['status'] < filters_info['status']:
continue
if filters_info['delivery_type'] != 'none' and delivery_types[filters_info['delivery_type']] != user['delivery_type']:
continue
if filters_info['name'] != '' and filters_info['name'].lower() != user['name'].lower():
continue
if filters_info['address'] != '' and filters_info['address'].lower() != user['address'].lower():
continue
if filters_info['wishlist'] != '':
filters_wishlist = re.split(pattern, filters_info['wishlist'])
user_wishlist = re.split(pattern, user['wishlist'])
found = False
for user_wishlist_item in user_wishlist:
for filters_wishlist_item in filters_wishlist:
if user_wishlist_item.lower() == filters_wishlist_item.lower():
found = True
if not found:
continue
if filters_info['stoplist'] != '':
filters_stoplist = re.split(pattern, filters_info['stoplist'])
user_stoplist = re.split(pattern, user['stoplist'])
found = False
for user_stoplist_item in user_stoplist:
for filters_stoplist_item in filters_stoplist:
if user_stoplist_item.lower() == filters_stoplist_item.lower():
found = True
if not found:
continue
filtered_users.append(user)
return filtered_users




@app.route('/import_db', methods=['POST'])
def import_db():
return app.response_class(status=200)
Expand Down

0 comments on commit 83ee9da

Please sign in to comment.