Skip to content

Commit

Permalink
1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mantsevat committed Dec 24, 2024
1 parent bd6cb5b commit ecae66e
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 108 deletions.
42 changes: 41 additions & 1 deletion server/db/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from .get_db import *
from .add_db import *
from .imp_exp import *
from .statistic import *
from .filter_db import *

class Database:
def __init__(self, connection_str, db_name):
Expand Down Expand Up @@ -59,7 +61,7 @@ def import_db(self, import_json):
print("синтаксически неверный JSON")
return False
self.user_id, self.game_id, self.event_id = max_ids

def search_host(self, fields):
return search_host_w_fields(self.db, fields)

Expand All @@ -75,3 +77,41 @@ def search_game(self, fields):

def search_event(self, fields):
return search_events_w_fields(self.db, fields)

def get_game_participants(self, game_id):
game_filter = {"game_id": game_id}
return search_player_w_fields(self.db, game_filter)

def count_forms(self):
all = count_type("users")
forms = count_type_w_filter("users", "status")
return

def count_purchases(self):
pass

def count_senders(self):
pass

def count_all_users(self):
pass

def count_all_events(self):
pass

def count_all_games(self):
pass

def update_user(self, user_info):
add_user(user_info)

def update_users(self, users_info):
self.db["users"].drop()
self.db.create_collection("users")
self.db["users"].insert_many(users_info)

def import_game(self, game_json):
restore_game(self.db, game_json)

def export_game(self, game_id):
return export_game(self.db, game_id)
7 changes: 4 additions & 3 deletions server/db/imp_exp.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def restore_game(db, import_json):
col = db["users"]
col.insert_many(users)

def export_game(db):
users = {}
return users
def export_game(db, game_id):
users = db["users"]
gamers = users.find({"is_host": False})
return gamers
7 changes: 7 additions & 0 deletions server/db/statistic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def count_type(db, collection):
documents = db[collection]
return documents.count_documents()

def count_type_w_filter(db, collection, filt):
documents = db[collection]
return documents.count_documents(filt)
120 changes: 16 additions & 104 deletions server/web_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,59 +70,6 @@ def host(id):
@app.route('/get_list_people')
def get_list_people():
return db.users_list()
# return [{
# #'_id': ObjectId('6722d278ee5e086ddmeff8b0'),
# 'id': 1,
# 'name': 'test_user1',
# 'email': '[email protected]',
# 'password': 'aafewfwefg',
# 'is_host': True,
# 'created_at': datetime.datetime(2024, 11, 11, 0, 0),
# 'updated_at': datetime.datetime(2024, 11, 12, 0, 0)
# },
# {
# #'_id': ObjectId('6722d278ee5e086effffuyuggb0'),
# 'id': 2,
# 'name': 'test_user2',
# 'email': '[email protected]',
# 'password': 'hnhytresfsd',
# 'is_host': False,
# 'address': 'nevsky str. 17',
# 'index': 111111,
# 'phone': 89999999999,
# 'status': 1,
# 'delivery_type': 1,
# 'wishlist': 'sweets, computer',
# 'stoplist': 'books ',
# 'recipient': 3,
# 'santa': 3,
# 'got_gift': False,
# 'created_at': datetime.datetime(2024, 11, 11, 0, 0),
# 'updated_at': datetime.datetime(2024, 11, 12, 0, 0),
# 'wrong_gift': False,
# },
# {
# #'_id': ObjectId('6722d278ee5e086effffuyuggb0'),
# 'id': 3,
# 'name': 'test_user3',
# 'email': '[email protected]',
# 'password': 'hnhytresfsd',
# 'is_host': False,
# 'address': 'nevsky str. 17',
# 'index': 111111,
# 'phone': 89999999999,
# 'status': 1,
# 'delivery_type': 1,
# 'wishlist': 'flowers;money',
# 'stoplist': 'candles',
# 'recipient': 2,
# 'santa': 2,
# 'got_gift': False,
# 'created_at': datetime.datetime(2024, 11, 11, 0, 0),
# 'updated_at': datetime.datetime(2024, 11, 12, 0, 0),
# 'wrong_gift': False,
# },
# ]

@app.route('/get_list_people_from_team/<game_id>')
def get_list_people_from_team(game_id):
Expand All @@ -140,43 +87,6 @@ def get_list_people_from_team(game_id):
@app.route('/get_list_team')
def get_list_team():
answer = db.games_list()
# answer = [{
# 'id': 1,
# 'lowest_price': 100,
# 'highest_price': 200,
# 'form_deadline': datetime.datetime(2025, 1, 24, 0, 0),
# 'purchase_deadline': datetime.datetime(2025, 1, 25, 0, 0),
# 'send_deadline': datetime.datetime(2025, 1, 26, 0, 0),
# 'created_at': datetime.datetime(2025, 1, 23, 0, 0),
# 'updated_at': datetime.datetime(2025, 1, 23, 0, 0),
# 'users': [1, 2],
# 'events': [1, 2]
# },
# {
# 'id': 2,
# 'lowest_price': 1000,
# 'highest_price': 2000,
# 'form_deadline': datetime.datetime(2025, 1, 24, 0, 0),
# 'purchase_deadline': datetime.datetime(2025, 1, 25, 0, 0),
# 'send_deadline': datetime.datetime(2025, 1, 26, 0, 0),
# 'created_at': datetime.datetime(2025, 1, 23, 0, 0),
# 'updated_at': datetime.datetime(2025, 1, 23, 0, 0),
# 'users': [1, 2],
# 'events': [1, 2]
# },
# {
# 'id': 3,
# 'lowest_price': 10000,
# 'highest_price': 20000,
# 'form_deadline': datetime.datetime(2025, 1, 24, 0, 0),
# 'purchase_deadline': datetime.datetime(2025, 1, 25, 0, 0),
# 'send_deadline': datetime.datetime(2025, 1, 26, 0, 0),
# 'created_at': datetime.datetime(2025, 1, 23, 0, 0),
# 'updated_at': datetime.datetime(2025, 1, 23, 0, 0),
# 'users': [1, 2],
# 'events': [1, 2]
# }
# ]
return answer

@app.route('/get_teams_info')
Expand All @@ -193,16 +103,14 @@ def post_new_team():
@app.route('/update_user', methods=['POST'])
def update_user():
user_info = json.loads(flask.request.data)
print(user_info)
#обновить информацию об игроке в бд
db.update_user(user_info)
return app.response_class(status=200)


@app.route('/update_users', methods=['POST'])
def update_users():
users = json.loads(flask.request.data)
print(users)
#обновить всю информацию о юзерах (по сути полностью стереть старую и записать новую)
db.update_users(users)
return app.response_class(status=200)


Expand Down Expand Up @@ -477,24 +385,28 @@ def get_filtered_games():
@app.route('/import_db', methods=['POST'])
def import_db():
new_data = json.loads(flask.request.data)
print(new_data)
db.import_game(new_data)
return app.response_class(status=200)

@app.route('/export_db', methods=['POST'])
def export_db():
export_status = 200
# вместо filename - название файла для экспорта. если всё норм, то export_db вернёт True
if not db.export_db("filename"):
export_status = 500
#if not db.export_db("filename"):
# export_status = 500
# в return можно прокинуть данные как словарик (пример ниже) и они сохранятся в json
# return {'abc': 'abc', 'dfd': 5, 'dsas': 6}
return app.response_class(status=export_status)

#@app.route('/import_db', methods=['POST'])
#def import_db():
# # db.import_db()
# return app.response_class(status=200)
return db.export_game(1)

@app.route('/export_db_all', methods=['POST'])
def export_all():
export_status = 200
# вместо filename - название файла для экспорта. если всё норм, то export_db вернёт True
# if not db.export_db("filename"):
# export_status = 500
# в return можно прокинуть данные как словарик (пример ниже) и они сохранятся в json
# return {'abc': 'abc', 'dfd': 5, 'dsas': 6}
return db.export_db()

if __name__ == '__main__':
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8000)

0 comments on commit ecae66e

Please sign in to comment.