def delete(self, token, template_id): try: email = self.authorization.get_email(token) self.persistence_gateway.delete(email, template_id) except AuthenticationError: raise UserInputError('Authentication failed') except InvalidOperation: raise UserInputError('Operation not allowed')
def _assert_property_is_positive_integer(self, dictionary, property_name): if property_name not in dictionary.keys(): raise UserInputError(f"{property_name} is mandatory") val = dictionary[property_name] if type(val) != int: raise UserInputError(f"{property_name} has to be a integer") if val <= 0: raise UserInputError( f"{property_name} has to be a positive integer")
def get(self, template_id, token): try: email = self.authorization.get_email(token) x = self.persistence_gateway.get(template_id, email) return x except InvalidOperation: raise UserInputError('Operation not allowed') except AuthenticationError: raise UserInputError('Authentication failed')
def get_all(self, token): try: email = self.authorization.get_email(token) temp = self.persistence_gateway.get_all(email) return temp except InvalidOperation: raise UserInputError('Operation not allowed') except AuthenticationError: raise UserInputError('Authentication failed') except Exception: raise UserInputError('Wrong Credentials invalid token')
def insert(self, token, template_name, subject, body): try: email = self.authorization.get_email(token) x = self.persistence_gateway.insert(email, template_name, subject, body) return x except InvalidOperation: raise UserInputError('Wrong Credentials') except AuthenticationError: raise UserInputError('Authentication failed') except Exception: raise UserInputError('Miscellaneous')
def login(self, email, password): try: user = self.user_repository.get_user(email) salt = user["salt"] hashed_password_db = user["password_hash"] hashed_password = self.hash_password(password, salt) if hashed_password_db == hashed_password: return self.authorization.get_token(email) else: raise UserInputError('Wrong Credentials') except InvalidOperation: raise UserInputError('Wrong Credentials')
def _assert_property_is_valid_datetime_string(self, r, property_name): if property_name not in r.keys(): raise UserInputError(f"{property_name} is mandatory") if type(r[property_name]) != str: raise UserInputError( f"{property_name} needs to be in string format ex.2034-06-01 01:10:20" ) try: if datetime.now() > datetime.fromisoformat(r[property_name]): raise UserInputError(f"{property_name} cannot be in the past") except ValueError: raise UserInputError( f"{property_name} needs to be in string format ex.2034-06-01 01:10:20" )
def _assert_property_is_valid_string(self, dictionary, property_name, max_length): if property_name not in dictionary.keys(): raise UserInputError(f"{property_name} is mandatory") val = dictionary[property_name] if type(val) != str: raise UserInputError(f"{property_name} has to be a string") if val.isspace(): raise UserInputError(f"{property_name} is mandatory") if len(val) > max_length: raise UserInputError( f"{property_name}'s max length is {max_length}") if len(val) == 0: raise UserInputError(f"{property_name} is mandatory")
def register(self, email, password, first_name, last_name): try: salt = uuid.uuid4().hex hashed_password = self.hash_password(password, salt) self.user_repository.create_user(first_name, last_name, email, hashed_password, salt) except UnableToInsertDueToDuplicateKeyError: raise UserInputError('ID already exist')
def add_audio_file(self, audio_file_id, creation_request): if type(audio_file_id) is not int: raise UserInputError("Audio file Id has to be an integer") self._assert_creation_parameters_are_correct(creation_request) filtered_creation_request = self.__filter_audio_file(creation_request) self.persistence_gateway.add(self._get_collection(), audio_file_id, filtered_creation_request)
def create_audio_file_handler(self, audio_file_type): audio_file_type = audio_file_type.lower() if audio_file_type == "audiobook": return AudioBookHandler(self.persistence_gateway) if audio_file_type == "song": return SongHandler(self.persistence_gateway) if audio_file_type == "podcast": return PodcastHandler(self.persistence_gateway) else: raise UserInputError('The audio file type is not understood')
def _assert_creation_parameters_are_correct(self, r): self._assert_property_is_valid_string(r, 'name', 100) self._assert_property_is_positive_integer(r, 'duration') self._assert_property_is_valid_datetime_string(r, 'uploaded_time') self._assert_property_is_valid_string(r, 'host', 100) if 'participants' in r.keys(): if type(r['participants']) != list: raise UserInputError("participants has to be a list") if len(r['participants']) > 10: raise UserInputError( "participants can have maximum of 10 members") for member in r['participants']: if type(member) is not str: raise UserInputError( "Each member of the participant has to be string") for member in r['participants']: if len(member) > 100: raise UserInputError( "maximum characters allowed for all participants in 100" )
def update_audio_file(self, audio_file_type, audio_file_id, creation_request): try: audio_file_handler = self.create_audio_file_handler(audio_file_type) audio_file_handler.update_audio_file(audio_file_id, creation_request) except ItemNotFound: raise UserInputError('Item does not exists')
def delete_file(self, audio_file_type, audio_file_id): audio_file_handler = self.create_audio_file_handler(audio_file_type) try: audio_file_handler.delete_audio_file(audio_file_id) except ItemNotFound: raise UserInputError("Item to be deleted is not found")
def get_file(self, audio_file_type, audio_file_id): audio_file_handler = self.create_audio_file_handler(audio_file_type) try: return audio_file_handler.get_audio_file(audio_file_id) except ItemNotFound: raise UserInputError("The requested item was not found")
def add_audio_file(self, audio_file_type, audio_file_id, creation_request): try: audio_file_handler = self.create_audio_file_handler(audio_file_type) audio_file_handler.add_audio_file(audio_file_id, creation_request) except UnableToInsertDueToDuplicateKeyError: raise UserInputError('ID already exist')