async def get_by_id(
            self, account_main_id: int
    ) -> Tuple[Optional[AccountMain], Optional[Error]]:
        async with self.pool.acquire() as conn:
            row = await conn.fetchrow(
                f"""
                    SELECT 
                        id AS {ACCOUNT_MAIN_ID},
                        created_at AS {ACCOUNT_MAIN_CREATED_AT},
                        edited_at AS {ACCOUNT_MAIN_EDITED_AT},
                        email AS {ACCOUNT_MAIN_EMAIL},
                        hash_password AS {ACCOUNT_MAIN_HASH_PASSWORD},
                        is_active AS {ACCOUNT_MAIN_IS_ACTIVE},
                        is_confirmed AS {ACCOUNT_MAIN_IS_CONFIRMED},
                        balance AS {ACCOUNT_MAIN_BALANCE}
                    FROM 
                        account_main
                    WHERE 
                        id = $1
                """, account_main_id)
            if not row:
                return None, None

            return AccountMainDeserializer.deserialize(
                row, DES_ACCOUNT_MAIN_FROM_DB_FULL), None
 def _from_db_full(auth_code: Record) -> AuthCode:
     account_main_dict = filter_keys_by_substr(auth_code, ACCOUNT_MAIN)
     return AuthCode(id=auth_code.get(AUTH_CODE_ID),
                     created_at=auth_code.get(AUTH_CODE_CREATED_AT),
                     edited_at=auth_code.get(AUTH_CODE_EDITED_AT),
                     account_main=AccountMainDeserializer.deserialize(
                         account_main_dict, DES_ACCOUNT_MAIN_FROM_DB_FULL),
                     code=auth_code.get(AUTH_CODE_CODE))
def test_des_from_dict():

    account_main_dict = {
        "email": "*****@*****.**",
        "password": "******"
    }

    account_main = AccountMainDeserializer.deserialize(account_main_dict, DES_FROM_DICT)
    assert account_main.email == account_main_dict['email']
    assert account_main.password == account_main_dict['password']
Esempio n. 4
0
async def auth_basic(request: Request):
    mistakes = RegisterAuthSchema().validate(request.json)
    if mistakes:
        return get_response_with_validation_errors(mistakes)

    account_main = AccountMainDeserializer.deserialize(request.json,
                                                       DES_FROM_DICT)
    account_main, err = await AuthService.auth_basic(account_main)
    if err:
        return err.get_response_with_error()

    return get_response_auth(account_main)
 async def get_by_session_id(
         self,
         session_id: int) -> Tuple[Optional[AccountMain], Optional[Error]]:
     async with self.pool.acquire() as conn:
         row = await conn.fetchrow(
             f"""
                 SELECT
                     account_session.account_main_id AS {ACCOUNT_MAIN_ID}
                 FROM
                     account_session
                 WHERE id = $1
             """, session_id)
         if not row:
             return None, None
         return AccountMainDeserializer.deserialize(
             row, DES_ACCOUNT_MAIN_FROM_DB_FULL), None
    def _deserialize_from_db_full(place_main: Union[dict, Record]) -> PlaceMain:
        account_main = filter_keys_by_substr(place_main, ACCOUNT_MAIN)
        main_language = filter_keys_by_substr(place_main, LANGUAGE)
        photo = filter_keys_by_substr(place_main, PHOTO)
        main_currency = filter_keys_by_substr(place_main, CURRENCY)

        return PlaceMain(
            id=place_main.get(PLACE_MAIN_ID),
            created_at=place_main.get(PLACE_MAIN_CREATED_AT),
            edited_at=place_main.get(PLACE_MAIN_EDITED_AT),
            main_language=LanguageDeserializer.deserialize(main_language, DES_LANGUAGE_FROM_DB_FULL),
            account_main=AccountMainDeserializer.deserialize(account_main, DES_ACCOUNT_MAIN_FROM_DB_FULL),
            name=place_main.get(PLACE_MAIN_NAME),
            login=place_main.get(PLACE_MAIN_LOGIN),
            photo=PhotoDeserializer.deserialize(photo, DES_PHOTO_FROM_DB_FULL),
            description=place_main.get(PLACE_MAIN_DESCRIPTION),
            main_currency=CurrencyDeserializer.deserialize(main_currency, DES_CURRENCY_FROM_DB_FULL),
            is_draft=place_main.get(PLACE_MAIN_IS_DRAFT),
            is_published=place_main.get(PLACE_MAIN_IS_PUBLISHED)
        )
    async def get_by_email_hash_password(
        self, account_main: AccountMain
    ) -> Tuple[Optional[AccountMain], Optional[Error]]:
        async with self.pool.acquire() as conn:
            row = await conn.fetchrow(
                f"""
                SELECT 
                    id AS {ACCOUNT_MAIN_ID},
                    email AS {ACCOUNT_MAIN_EMAIL},
                    hash_password AS {ACCOUNT_MAIN_HASH_PASSWORD},
                    is_active AS {ACCOUNT_MAIN_IS_ACTIVE},
                    is_confirmed AS {ACCOUNT_MAIN_IS_CONFIRMED}
                FROM 
                    account_main
                WHERE 
                    email = $1 AND hash_password = $2
            """, account_main.email, account_main.hash_password)

            if row:
                return AccountMainDeserializer.deserialize(
                    row, DES_ACCOUNT_MAIN_FROM_DB_FULL), None
            return None, None
def test_des_from_db_full():
    account_main_record = {
        ACCOUNT_MAIN_ID: 12,
        ACCOUNT_MAIN_CREATED_AT: datetime.now(),
        ACCOUNT_MAIN_EDITED_AT: datetime.now(),
        ACCOUNT_MAIN_EMAIL: '*****@*****.**',
        ACCOUNT_MAIN_HASH_PASSWORD: '******',
        ACCOUNT_MAIN_BALANCE: 321,
        ACCOUNT_MAIN_IS_CONFIRMED: False,
        ACCOUNT_MAIN_IS_ACTIVE: True
    }

    account_main = AccountMainDeserializer.deserialize(account_main_record, DES_ACCOUNT_MAIN_FROM_DB_FULL)

    assert account_main_record[ACCOUNT_MAIN_ID] == account_main.id
    assert account_main_record[ACCOUNT_MAIN_CREATED_AT] == account_main.created_at
    assert account_main_record[ACCOUNT_MAIN_EDITED_AT] == account_main.edited_at
    assert account_main_record[ACCOUNT_MAIN_EMAIL] == account_main.email
    assert account_main_record[ACCOUNT_MAIN_HASH_PASSWORD] == account_main.hash_password
    assert account_main_record[ACCOUNT_MAIN_BALANCE] == account_main.balance
    assert account_main_record[ACCOUNT_MAIN_IS_CONFIRMED] == account_main.is_confirmed
    assert account_main_record[ACCOUNT_MAIN_IS_ACTIVE] == account_main.is_active