def register_account(uow: UnitOfWork, email: str, password: str, username: str) -> Account: whitelisted_emails = getenv('WHITELISTED_EMAILS') if whitelisted_emails is not None and email not in whitelisted_emails.split( ','): raise AccountRegistrationError('Email not whitelisted') with uow: if uow.account.get(Account, email=email) is not None: raise AccountRegistrationError( f"Account with the email '{email}' already exists") if uow.user.get(User, username=username): raise AccountRegistrationError( f"Account with the username '{username}' already exists") password_hash = Authentication().generate_password_hash(password) user = User(username) account = Account(email, password_hash, user) account_record = uow.account.add(account) uow.commit() account_record = uow.account.get(Account, email=email) verification_token = account.generate_verification_token account.send_registration_email(verification_token) return account_record
def create_article(uow: UnitOfWork, user_id: int, title: str, body: str) -> Article: with uow: user_record = uow.user.get(User, id=user_id) if user_record is None: raise UserError('User does not exist') new_article = Article(title, body) new_article.convert_body_to_html user_record.articles.append(new_article) uow.commit() article_record = uow.articles.get(Article) return article_record
def create_project(uow: UnitOfWork, user_id: int, title: str, body: str) -> Project: with uow: user_record = uow.user.get(User, id=user_id) if user_record is None: raise UserError('User does not exist') new_project = Project(title, body) new_project.convert_body_to_html user_record.projects.append(new_project) uow.commit() project_record = uow.projects.get(Project) return project_record
def verify_account(uow: UnitOfWork, token: str) -> Account: registration_details = Account.validate_verification_token(token) email = registration_details['email'] with uow: account_record = uow.account.get(Account, email=email) if account_record is None: raise AccountError( f"No registered account with the email '{email}'") if account_record.verified: raise AccountVerificationError('Account already verified') setattr(account_record, 'verified', True) uow.commit() account_record = uow.account.get(Account, email=email) return account_record