Esempio n. 1
0
    async def authenticate(self, request, email=None, password=None):
        user = None
        UserModel = get_user_model()
        simpl_client = GamesAPIClient(url=settings.SIMPL_GAMES_URL,
                                      auth=(email, password))
        async with simpl_client as api_session:
            try:
                simpl_user = await api_session.users.get(email=email)
                runusers = \
                    await api_session.runusers.filter(user=simpl_user.id,
                                                      game_slug=settings.GAME_SLUG)
                if len(runusers) == 0:
                    return None
            except GamesAPIClient.NotAuthenticatedError:
                return None

            defaults = payload_to_attrs(simpl_user.payload)
            user, _ = UserModel._default_manager.get_or_create(
                email=email, defaults=defaults)
            return user if self.user_can_authenticate(user) else None
from django.conf import settings

from simpl_client import GamesAPIClient

from {{ cookiecutter.project_slug }}.asyncio import coro

CALLBACK_URL = getattr(settings, 'CALLBACK_URL',
                       'http://{hostname}:{port}/api/callback')


def get_callback_url():
    return CALLBACK_URL.format(hostname=os.environ.get('HOSTNAME', ''),
                               port=os.environ.get('PORT', ''))


simpl_client = GamesAPIClient(url=settings.SIMPL_GAMES_URL,
                              auth=settings.SIMPL_GAMES_AUTH)


@coro
async def subscribe(prefix, callback=None):
    if callback is None:
        callback = get_callback_url()
    try:
        async with simpl_client as api_session:
            subscription = await api_session.hooks.create({
                'event': '{}.*'.format(prefix),
                'url': callback,
            })
            return subscription
    except GamesAPIClient.HTTPError as e:
        if e.response.status_code == 400: