class DataBase: def __init__(self, app): app.config['MONGO_URI'] = f"mongodb+srv://admin:{PASSWORD}@{CLUSTER}/" \ f"{NAME}?retryWrites=true&w=majority" self.db = PyMongo(app).db def get_user(self, collection, user_id): return self.db[collection].find_one({'id': user_id}) def get_users(self, collection): size = self.db[collection].count_documents({}) if size == 0: return [], [] results = self.db[collection].find() ids = [None] * size embeds = [[]] * size for i in range(size): ids[i] = results[i]['id'] embeds[i] = results[i]['embed'] return ids, embeds def create(self, collection, user): try: if self.db[collection].count_documents({}) == 0: self.db[collection].create_index([('id', pymongo.TEXT)], unique=True) self.db[collection].insert_one(user) return True except Exception as ex: print(f'Insert: {ex}') return False def update(self, collection, user_id, data): try: self.db[collection].update_one({'id': user_id}, {"$set": data}) return True except Exception as ex: print(f'Update: {ex}') return False def remove(self, collection, user_id): try: self.db[collection].delete_one({'id': user_id}) return True except Exception as ex: print(f'Delete: {ex}') return False def count(self, collection): try: return self.db[collection].count_documents({}) except Exception as ex: print(f'Count: {ex}') return -1 def rename(self, collection, name): try: self.db[collection].rename(name) return True except Exception as ex: print(f'Rename: {ex}') return False def drop(self, collection): try: self.db.drop_collection(collection) return True except Exception as ex: print(f'Drop: {ex}') return False
class AdminData: def __init__(self, app): self.db = PyMongo(app, uri=MONGO_URI + USER_DB).db.db self.face_db = PyMongo(app, uri=MONGO_URI + FACE_DB).db if self.db.count_documents({}) == 0: self.db.create_index([('collection', TEXT), ('key', TEXT)], unique=True) self.log = logger() def get_data(self, data: dict): return self.db.find_one(data) def get(self): results = self.db.find().sort([("collection", ASCENDING)]) return list(results) def search(self, keyword): results = self.db.find( {"$text": {"$search": keyword}}, {"score": {"$meta": "textScore"}} ).sort([("score", {"$meta": "textScore"})]) return list(results) def create(self, collection: str): try: key = secrets.token_urlsafe() while self.db.find_one({'key': key}): key = secrets.token_urlsafe() self.db.insert_one({ 'collection': collection, 'key': key }) return key except Exception as ex: self.log.exception(ex) return '' def update(self, collection: str): try: key = secrets.token_urlsafe() while self.db.find_one({'key': key}): key = secrets.token_urlsafe() self.db.update_one( { 'collection': collection }, { "$set": { 'key': key } } ) return key except Exception as ex: self.log.exception(ex) return '' def remove(self, collection: str): data = {'collection': collection} for model in MODELS: self.face_db.drop_collection(model + collection) self.db.delete_one(data) return not self.db.find_one(data)
class DataBase: def __init__(self, app): app.config['MONGO_URI'] = f"mongodb+srv://admin:{PASSWORD}@{CLUSTER}/" \ f"{NAME}?retryWrites=true&w=majority" self.db = PyMongo(app).db def get_user(self, collection, user_id): return self.db[collection].find_one({'id': user_id}) def create(self, collection, user): try: if self.db[collection].count_documents({}) == 0: self.db[collection].create_index([('id', pymongo.TEXT)], unique=True) self.db[collection].insert_one(user) return True except Exception as ex: print(f'Insert: {ex}') return False def update(self, collection, user): try: self.db[collection].update_one( { 'id': user['id'] }, { "$set": { 'left': user['left'], 'right': user['right'], 'front': user['front'] } } ) return True except Exception as ex: print(f'Update: {ex}') return False def remove(self, collection, user_id): try: self.db[collection].delete_one({'id': user_id}) return True except Exception as ex: print(f'Delete: {ex}') return False def count(self, collection): try: return self.db[collection].count_documents({}) except Exception as ex: print(f'Count: {ex}') return -1 def rename(self, collection, name): try: self.db[collection].rename(name) return True except Exception as ex: print(f'Rename: {ex}') return False def drop(self, collection): try: return self.db.drop_collection(collection) except Exception as ex: print(f'Drop: {ex}') return False