diff --git a/server/src/__pycache__/utils.cpython-39.pyc b/server/src/__pycache__/utils.cpython-39.pyc index 82cba1f..cfbe906 100644 Binary files a/server/src/__pycache__/utils.cpython-39.pyc and b/server/src/__pycache__/utils.cpython-39.pyc differ diff --git a/server/src/utils.py b/server/src/utils.py index f2c0a6d..1edb507 100644 --- a/server/src/utils.py +++ b/server/src/utils.py @@ -1,3 +1,6 @@ def get_actions_types(): return {0: 'заполнил(a) анкету', 1: 'отправил(а) чек оплаты на проверку организатору', - 2: 'отправил(а) подарок', 3: 'получил(а) подарок'} \ No newline at end of file + 2: 'отправил(а) подарок', 3: 'получил(а) подарок'} + +def get_delivery_types(): + return {'none': 0, 'ticket': 1, 'cert': 2, 'mail': 3, 'other': 4} \ No newline at end of file diff --git a/server/static/js/user_list.js b/server/static/js/user_list.js index 77d166c..05424a9 100644 --- a/server/static/js/user_list.js +++ b/server/static/js/user_list.js @@ -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(); @@ -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"; @@ -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)) +} diff --git a/server/templates/list_people.html b/server/templates/list_people.html index a58e4a3..2702ef7 100644 --- a/server/templates/list_people.html +++ b/server/templates/list_people.html @@ -39,10 +39,10 @@
Тип подарка
-
-
-
-
+
+
+
+
diff --git a/server/web_server.py b/server/web_server.py index e87e130..b47ec71 100644 --- a/server/web_server.py +++ b/server/web_server.py @@ -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__) @@ -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, @@ -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, @@ -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, @@ -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)