Beispiel #1
0
def create_account(email, password_raw, account_type, auth_id_pre='local:',
                   _dto=True):
    """Creates a new account. Sends an email confirmation.

    :param email: (str) Email address.
    :param password_raw: (str) Raw password.
    :param auth_id_pre: (str) Auth ID prefix.
    :return: The newly created account.
            (dto.accounts.AccountDto) if _dto is True
            (kinds.accounts.Account) if _dto is False
    """
    asserts.type_of(email, basestring)
    asserts.type_of(password_raw, basestring)
    asserts.type_of(account_type, AccountType)

    email = email.lower()
    auth_id = auth_id_pre + email

    account = _create_new_account(email, password_raw, auth_id)
    _account_setup(account_type, account)

    # Async email.
    services.email.send_email_verification(account.id)
    if not _dto:
        return account
    return AccountDto.from_account_ndb(account)
Beispiel #2
0
def account_update(actor_id, account_dto, _dto=True):
    """Updates account information.

    :param actor_id: (int) ID of the account.
    :param account_dto: (dto.accounts.AccountDto) DTO with updated information.
    :return: (dto.accounts.AccountDto) if _dto is True.
             (kinds.accounts.Account) if _dto is False.
    """
    asserts.type_of(account_dto, AccountDto)

    ac = account_by_id(actor_id, _dto=False)
    acd = account_dto

    def has_changed(pdto, pndb):
        return pdto is not None and pdto != pndb

    if ac.id != acd.id:
        raise exp.PermissionExp()
    if has_changed(acd.first, ac.first):
        ac.first = acd.first
    if has_changed(acd.last, ac.last):
        ac.last = acd.last
    if has_changed(acd.phone_number, ac.phone_number):
        ac.phone_number = acd.phone_number
    if has_changed(acd.account_type, ac.account_type):
        ac.account_type = acd.account_type
        # First-time caregiver.
        if ac.account_type == AccountType.Caregiver and ac.caregiver_id is None:
            caregiver = Caregiver(account_id=ac.id)
            caregiver.put()
            ac.caregiver_id = caregiver.id
    ac.put()
    if _dto:
        return AccountDto.from_account_ndb(ac)
    return ac
Beispiel #3
0
 def user_model(self):
     """Gets the user object for the user from the datastore."""
     user_model, timestamp = self.auth.store.user_model.get_by_auth_token(
         self.user['user_id'],
         self.user['token']) if self.user else (None, None)
     if user_model:
         return AccountDto.from_account_ndb(user_model)
Beispiel #4
0
    def to_account_dto(cls, account_api):
        """Translates the given AccountApiModel to an AccountDto.

        :param account_api: (api.accounts.AccountApiModel)
        :return: (dto.accounts.AccountDto)
        """
        asserts.type_of(account_api, AccountApiModel)

        account_dto = AccountDto()
        map_props(account_dto, account_api, AccountDto._props)
        return account_dto
Beispiel #5
0
def login(email, password_raw, auth_id_pre='local:'):
    """Checks if given credentials are valid.

    :param email: (str) Email address.
    :param password_raw: (str) Raw password.
    :param auth_id_pre: (str) Auth ID prefix.
    :return: (dto.accounts.AccountDto) or (None)
    """
    asserts.type_of(email, basestring)
    asserts.type_of(password_raw, basestring)

    email = email.lower()
    auth_id = auth_id_pre + email
    account = Account.get_by_auth_password(auth_id, password_raw)
    return AccountDto.from_account_ndb(account)
Beispiel #6
0
def account_by_email(email, _dto=True, _throw=True):
    """Returns the account associated with the given email address.

    :param email: (int) ID of the account.
    :return: (dto.accounts.AccountDto) if _dto is True.
             (kinds.accounts.Account) if _dto is False.
             (None) if account does not exist.
    """
    asserts.type_of(email, basestring)

    account = Account.query(Account.email == email.lower()).get()
    if account is None and _throw:
        logging.warning('account not found. email={}'.format(email))
        raise exp.NotFoundExp('Account not found.')
    if not _dto:
        return account
    return AccountDto.from_account_ndb(account)
Beispiel #7
0
def account_by_id(account_id, _dto=True, _throw=True):
    """Returns the account associated with the given account_id.

    :param account_id: (int) ID of the account.
    :return: (dto.accounts.AccountDto) if _dto is True.
             (kinds.accounts.Account) if _dto is False.
             (None) if account does not exist.
    :raise: (exp.NotFoundExp) if `_throw` is True and account is not found.
    """
    asserts.valid_id_type(account_id)

    account = Account.get_by_id(account_id)
    if account is None and _throw:
        logging.warning('account not found. id={}'.format(account_id))
        raise exp.NotFoundExp('Account not found.')
    if not _dto:
        return account
    return AccountDto.from_account_ndb(account)
Beispiel #8
0
def verify_email(email, token, _dto=True):
    """Verifies the given email verification token.

    :param email: (str) Email address.
    :param token: (str) Verification token.
    :return: Updated account.
    """
    asserts.type_of(email, basestring)
    asserts.type_of(token, basestring)

    account = account_by_email(email, _dto=False)
    if account.verification_token != token:
        raise exp.BadRequestExp('Invalid verification token.')
    if not account.email_verified:
        account.email_verified = True
        account.put()
    if _dto:
        return AccountDto.from_account_ndb(account)
    return account