def login(self, login: str, password: str) -> Dict[str, str]: """Proccess user data and return it otherwise error Parameters ---------- login : str login of an user """ user_data: User = self.auth_repository.get_user_data(login) if (user_data != None): password_is_correct = verify_password(user_data["password"], password) if password_is_correct: return { "id": str(user_data["_id"]), "login": user_data["login"] } else: raise ResponseException("Password is incorrect") else: raise ResponseException("Invalid login")
def create_canvas(self, canvas_data: CanvasDataToCreate, user_id: str) -> str: canvas_template = canvas_repository.get_canvas_template( canvas_data.type) if canvas_template is None: raise ResponseException("Incorrect canvas type") result = self.canvas_collection.insert_one({ "ownerId": user_id, "title": canvas_data.title, "type": canvas_data.type, "date": datetime.now(), "rows": canvas_template["rows"], "columns": canvas_template["columns"], "data": canvas_template["data"] }) return { "code": 0, "message": { "created_canvas_id": str(result.inserted_id) } }
def delete_canvas(self, user_id: str, canvas_id: str) -> ServerResponse: try: result = self.canvas_collection.delete_one({ "ownerId": user_id, "_id": ObjectId(canvas_id) }) return {"code": 0, "message": {"deleted_canvas_id": canvas_id}} except Exception: raise ResponseException(Exception)
def registration(self, user_data: User) -> ServerResponse: """User registration Parameters ---------- user_data : User Dictionary with an user data (login, email: Optional, password) """ email, login, password = destruct_dict(user_data) if login == "": raise ResponseException("Login is empty") if password == "": raise ResponseException("Password is empty") existed_user_data: User = self.auth_repository.get_user_data(login) user_data.password = hash_password(user_data.password) registration_date = datetime.datetime.now( pytz.timezone('Europe/Moscow')) if (existed_user_data != None): return ResponseException("Such user already exists") inserted_id = self.user_collection.insert_one({ "email": user_data.email, "login": user_data.login, "password": user_data.password, "registration_date": registration_date }).inserted_id if inserted_id != None: return {"code": 0, "message": "Success"} else: raise ResponseException("No such user exists")
def update_canvas(self, data: CanvasDataToUpdate, user_id: str) -> ServerResponse: try: result = self.canvas_collection.update_one( { "ownerId": user_id, "_id": ObjectId(data.canvas_id) }, {"$set": { "title": data.title, "data": data.data }}) return {"code": 0, "message": "Success"} except Exception: raise ResponseException(Exception)
def check_data_for_spam(data: CanvasDataToSend): total_cells = len(data.data) total_spam = 0 for item in data.data: content = item['content'] if (len(content) == 0): raise ResponseException('Fill all fields') res = find_spam(content) if (res == 'spam'): total_spam += 1 if (total_spam < total_cells / 2): return False else: return True
def create_canvas_template( self, canvasTemplateData: CanvasTemplateToCreate) -> ServerResponse: canvas_type = canvasTemplateData.type if not canvas_type: raise ResponseException("Type is not set") if not canvasTemplateData.rows: raise ResponseException("Rows are not set") if not canvasTemplateData.columns: raise ResponseException("Columns are not set") if not canvasTemplateData.data: raise ResponseException("Data are not set") isExist = self.canvas_template_collection.find_one( {"type": canvas_type}) != None if (isExist): raise ResponseException("Such Canvas type is already exist") result = self.canvas_template_collection.insert_one({ "ownerId": None, "title": None, "type": canvas_type, "date": datetime.now(), "rows": canvasTemplateData.rows, "columns": canvasTemplateData.columns, "data": canvasTemplateData.data }) return {"code": 0, "message": "Success"}
def check_for_admin(access_token: str): user = get_current_user(access_token) if (user["login"] != "admin"): raise ResponseException( "You have to be an admin to create a new canvas template")
def delete_canvas_template(self, canvas_type: str) -> ServerResponse: try: self.canvas_template_collection.delete_one({"type": canvas_type}) return {"code": 0, "message": "Success"} except Exception: raise ResponseException("Couldn't delete canvas template")