def create_new_user(username, password, signup_ip, name=None, email=None): """ Creates a new user with the specified username and password. :param username: Requested username of the new user :param password: Password for the new user :param signup_ip: IP address from which the user signed up :param name: Name of the user (optional) :param email: Email of the user (optional) :return: Newly created User object :raises InvalidEmailException: If an invalid email is passed :raises UsernameNotAvailableException: If the username is not available """ # Input validation if not is_username_available(username): raise UsernameNotAvailableException( 'The username {username} is not available'.format( username=username)) if email and not is_email_address_valid(email): raise InvalidEmailException( '{email_addr} is not a valid email address'.format( email_addr=email)) new_user = models.User( signup_ip=signup_ip, username=username, password_hash=util.cryptography.secure_hash(password), name=name, email=email, ) session.add(new_user) session.commit() return new_user
def create_new_paste(contents, user_id=None, expiry_time=None, title=None, language=None, password=None, is_api_post=False): """ Create a new paste. :param user_id: User ID of the paste poster :param contents: Contents of the paste :param expiry_time: Unix time at which the paste should expire (optional, default to no expiry) :param title: Title of the paste (optional) :param language: Language of the paste (optional, defaults to plain text) :param password: Password of the paste (optional) :param is_api_post: True to indicate that the post was posted externally via the API interface (optional) :return: An instance of models.Paste representing the newly added paste. """ new_paste = models.Paste( user_id=user_id, contents=contents, expiry_time=int(expiry_time) if expiry_time is not None else None, title=title if title else 'Untitled', language=language or 'text', password_hash=util.cryptography.secure_hash(password) if password is not None else None, is_api_post=is_api_post, ) session.add(new_paste) session.commit() return new_paste
def create_new_attachment(paste_id, file_name, file_size, mime_type, file_data): """ Create a new database entry for an attachment with the given file_name, associated with a particular paste ID. :param paste_id: Paste ID to associate with this attachment :param file_name: Raw name of the file :param file_size: Size of the file in bytes :param mime_type: MIME type of the file :param file_data: Binary, base64-encoded file data :return: An instance of models.Attachment describing this attachment entry :raises PasteDoesNotExistException: If the associated paste does not exist """ # Add an entry into the database describing this file new_attachment = models.Attachment( paste_id=paste_id, file_name=secure_filename(file_name), file_size=file_size, mime_type=mime_type, ) _store_attachment_file(paste_id, file_data, new_attachment) session.add(new_attachment) session.commit() return new_attachment
def create_new_user(username, password, signup_ip, name=None, email=None): """ Creates a new user with the specified username and password. :param username: Requested username of the new user :param password: Password for the new user :param signup_ip: IP address from which the user signed up :param name: Name of the user (optional) :param email: Email of the user (optional) :return: Newly created User object :raises InvalidEmailException: If an invalid email is passed :raises UsernameNotAvailableException: If the username is not available """ # Input validation if not is_username_available(username): raise UsernameNotAvailableException('The username {username} is not available'.format(username=username)) if email and not is_email_address_valid(email): raise InvalidEmailException('{email_addr} is not a valid email address'.format(email_addr=email)) new_user = models.User( signup_ip=signup_ip, username=username, password_hash=util.cryptography.secure_hash(password), name=name, email=email, ) session.add(new_user) session.commit() return new_user
def create_new_attachment(paste_id, file_name, file_size, mime_type, file_data): """ Create a new database entry for an attachment with the given file_name, associated with a particular paste ID. :param paste_id: Paste ID to associate with this attachment :param file_name: Raw name of the file :param file_size: Size of the file in bytes :param mime_type: MIME type of the file :param file_data: Binary, base64-encoded file data :return: An instance of models.Attachment describing this attachment entry :raises PasteDoesNotExistException: If the associated paste does not exist """ # Add an entry into the database describing this file new_attachment = models.Attachment( paste_id=paste_id, file_name=secure_filename(file_name), file_size=file_size, mime_type=mime_type ) _store_attachment_file(paste_id, file_data, new_attachment.hash_name) session.add(new_attachment) session.commit() return new_attachment