Skip to content

Commit

Permalink
add queries
Browse files Browse the repository at this point in the history
  • Loading branch information
mantsevat authored Dec 12, 2024
1 parent 994e1ff commit c29a724
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 5 deletions.
33 changes: 29 additions & 4 deletions server/db/database.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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

Expand All @@ -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)
23 changes: 22 additions & 1 deletion server/db/get_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
"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
38 changes: 38 additions & 0 deletions server/db/imp_exp.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit c29a724

Please sign in to comment.