diff --git a/server/db/database.py b/server/db/database.py index b79cf2e..c3d68da 100644 --- a/server/db/database.py +++ b/server/db/database.py @@ -1,13 +1,14 @@ from pymongo import MongoClient -from .get_db import find_one, get_games, get_users -from .add_db import add_game, add_user - +from .get_db import * +from .add_db import * +from .imp_exp import save_db, restore_db class Database: def __init__(self, connection_str, db_name): self.db = MongoClient(connection_str).get_database(db_name) self.game_id = 1 self.user_id = 1 + self.event_id = 1 def users_list(self): return get_users(self.db) @@ -17,7 +18,7 @@ def games_list(self): def register_game(self, team_info): host = team_info["admin"] - host.update({"id": self.user_id, "game_id":self.game_id, "is_host": True}) + host.update({"id": self.user_id, "game_id": self.game_id, "is_host": True}) add_user(self.db, host) self.user_id += 1 @@ -43,3 +44,27 @@ def get_user(self, user_id): def get_game(self, game_id): return find_one("games", self.db, game_id) + + def export_db(self, file_name): + return save_db(self.db, file_name) + + def import_db(self, import_json): + max_ids = (1, 1, 1) + try: + max_ids = restore_db(self.db, import_json) + except ValueError: + 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) + + def search_player(self, fields): + return search_player_w_fields(self.db, fields) + + def search_game(self, fields): + return search_games_w_fields(self.db, fields) + + def search_event(self, fields): + return search_events_w_fields(self.db, fields) diff --git a/server/db/get_db.py b/server/db/get_db.py index 8dbdad0..fa9ead8 100644 --- a/server/db/get_db.py +++ b/server/db/get_db.py @@ -42,4 +42,25 @@ def game_full_info(db, game_id): host = users_coll.find_one({"game_id": game_id, "is_host": True}) return {"admin_name": host["name"], "players_num": players_num, "form_num": status_count["form"], "accepted_cheques" :accepted_cheques, "not_accepted_cheques": not_accepted_cheques, - "sent_gifts": status_count["sent"], "got_gifts": got_gifts } \ No newline at end of file + "sent_gifts": status_count["sent"], "got_gifts": got_gifts } + + +def search_player_w_fields(db, fields): + documents = db["users"] + results = documents.find({"branch": {"$eq": "CSE"}}, {"branch": {"$exists": True}}) + return results + +def search_host_w_fields(db, fields): + documents = db["users"] + results = documents.find({"branch": {"$eq": "CSE"}}, {"branch": {"$exists": True}}) + return results + +def search_games_w_fields(db, fields): + documents = db["games"] + results = documents.find({"branch": {"$eq": "CSE"}}, {"branch": {"$exists": True}}) + return results + +def search_events_w_fields(db, fields): + documents = db["events"] + results = documents.find({"branch": {"$eq": "CSE"}}, {"branch": {"$exists": True}}) + return results \ No newline at end of file diff --git a/server/db/imp_exp.py b/server/db/imp_exp.py new file mode 100644 index 0000000..5407a52 --- /dev/null +++ b/server/db/imp_exp.py @@ -0,0 +1,38 @@ +from bson.json_util import dumps +import json + +def save_db(db, filename="dump.json"): + collections = db.collection_names() + db_dump_str = "{" + delim = "" + for i, collection_name in enumerate(collections): + col = getattr(db, collections[i]) + collection = col.find() + db_dump_str += delim + "{\"" + collection_name + "\":[" + db_dump_str += dumps(collection) + "]" + delim = "," + + db_dump_str += "}" + try: + with open(filename, 'wb') as jsonfile: + jsonfile.write(str.encode(db_dump_str)) + except (IOError, OSError): + print("ошибка записи") + return False + except (FileNotFoundError, PermissionError, OSError): + print("ошибка открытия файла") + return False + + return True + + +def restore_db(db, import_json): + json_colls = json.loads(import_json) + for col_name in json_colls: + col = db[col_name] + col.insert_many(json_colls[col_name]) + max_uid = len(json_colls["users"]) + 1 + max_gid = len(json_colls["games"]) + 1 + max_eid = len(json_colls["events"]) + 1 + + return max_uid, max_gid, max_eid \ No newline at end of file