Esempio n. 1
0
    async def prepare_db_request(self, data: dict) -> dict:
        """
        Prepare request to db. Replace Profile.user_id to int32 value from db.
        Args:
            data: dict with user data for Profile object.

        Returns:  dict with user profile.

        """
        profile = Profile(**data)
        profile.user_id = await get_next_sequence_value(
            self.request.app['db'], "user_id")
        return profile.to_dict()
Esempio n. 2
0
    async def post(self) -> Union[web.json_response, web.HTTPException]:
        """
        Take user credentials and check if user exist.
        If exist - return his profile in JSON. Else -> HTTPException
        Returns: User profile or Exception

        """
        request_data = await self.request.json()

        result = await self.find_user(request_data)

        if not result:
            raise web.HTTPNotFound(text="User not found")

        profile = Profile(**clean_data(result))

        if profile.check_password(request_data['password']):
            await profile.fetch_contacts(self.request.app['db'])

            return web.json_response(profile.to_dict(), status=200)

        raise web.HTTPNotAcceptable(text="Wrong password")