def get_user_by_id(cls, user_id): connection = DB.get_connection() collection = connection.get_collection(cls.collection_name) user = collection.find_one({"_id": ObjectId(user_id)}) if user is None: raise NoDocumentFound(404, "user not found with id : {}".format(user_id)) user = Utils.parse_document(user) return user
def get_role_by_id(cls, role_id): data_to_return = [] connection = DB.get_connection() collection = connection.get_collection(cls.collection_name) result = collection.find_one({"_id": ObjectId(role_id)}) if result is None: raise NoDocumentFound(404, "No role found with id : {}".format(role_id)) data_to_return.append(Utils.parse_document(result)) return data_to_return
def get_all_user(cls): connection = DB.get_connection() collection = connection.get_collection(cls.collection_name) data_to_return = [] all_user = collection.find({}).sort("created_at", -1) if all_user.count() == 0: raise NoDocumentFound(404, "No user found") for each_user in all_user: data_to_return.append(Utils.parse_document(each_user)) return data_to_return
def get_all_role(cls): data_to_return = [] connection = DB.get_connection() collection = connection.get_collection(cls.collection_name) all_roles = collection.find({}).sort("created_at", -1) if all_roles.count() is 0: raise NoDocumentFound(404, "No role found") for each_role in all_roles: data_to_return.append(Utils.parse_document(each_role)) return data_to_return
def get_permission_by_id(cls, permission_id): conn = DB.get_connection() collection = conn.get_collection(cls.collection_name) data_to_return = [] permission = collection.find_one({"_id": ObjectId(permission_id)}) if permission is None: raise NoDocumentFound( 404, "No permission found with id : {}".format(permission_id)) permission = Utils.parse_document(permission) data_to_return.append(permission) return data_to_return
def get_all_permission(cls): conn = DB.get_connection() collection = conn.get_collection(cls.collection_name) data_to_return = [] all_document = collection.find({}).sort("created_at", -1) if all_document.count() == 0: raise NoDocumentFound(404, "No permission found") for each_document in all_document: data_to_return.append(Utils.parse_document(each_document)) return data_to_return
def create_role(cls, name, permissions): connection = DB.get_connection() collection = connection.get_collection(cls.collection_name) existing_role = collection.find_one({"name": name}) if existing_role is not None: raise DuplicateDocument(409, "role with name : {} already exists".format(name)) document_to_insert = {"name": name, "permissions": permissions, "created_at": datetime.utcnow()} result = collection.insert_one(document_to_insert) last_inserted_id = str(result.inserted_id) return last_inserted_id
def create_user(cls, name, email, roles, phone_number=None): connection = DB.get_connection() collection = connection.get_collection(cls.collection_name) existing_user = collection.find_one({"email": email}) if existing_user is not None: raise DuplicateDocument(409, "User with email : {} already exits".format(email)) roles = map(lambda x: ObjectId(x), roles) document = {"name": name, "email": email, "roles": list(roles), "phone_number": phone_number, "created_at": datetime.utcnow()} result = collection.insert_one(document) return str(result.inserted_id)
def create_permission(cls, name, identifier): conn = DB.get_connection() collection = conn.get_collection(cls.collection_name) existing_permission = collection.find_one({"identifier": identifier}) if existing_permission is not None: raise DuplicateDocument( 409, "permission exists with identifier : {}".format(identifier)) document = { "name": name, "identifier": identifier, "created_at": datetime.utcnow() } result = collection.insert_one(document) inserted_id = result.inserted_id if inserted_id is not None: return str(inserted_id) return None
def login_handler(): login = request.form.get('login') password = request.form.get('password') if not all([login, password]): return '', 500 user_table_recs = DB.execute_select_query( 'SELECT name, login, password FROM User') user_rec = next(filter(lambda x: x[1] == login, user_table_recs)) if not user_rec: return '', 500 if user_rec[2] != password: return '', 500 response = make_response(redirect('/')) response.set_cookie('login', user_rec[1], samesite='Strict') return response
def test_get_connection_should_return_a_valid_db_connection(self): from src.db.db import DB actual_value = DB.get_connection() col_ref = actual_value.get_collection("dummy-collection") col_ref.insert_one({"key": "value"}) self.assertIsNotNone(actual_value)
def clear_collection(): db_connection = DB.get_connection() db_connection.drop_collection("permissions") db_connection.drop_collection("roles") db_connection.drop_collection("users")
def update_messages(self): self.messages = DB.execute_select_query( 'SELECT Message.id, User.id, name, timestamp, text ' 'FROM Message JOIN User ON author = User.id')
def delete_message(self, id): DB.execute_dml_query(f'DELETE FROM Message WHERE id = {id}')