Example #1
0
def auth(request, provider):
    redirect_uri = reverse("social:complete", args=(provider, ))
    request.social_strategy = DjangoStrategy(DjangoStorage, request)
    try:
        backend_cls = get_backend(BACKENDS, provider)
        backend_obj = backend_cls(request.social_strategy, redirect_uri)
    except MissingBackend:
        raise Http404('Backend not found')

    return do_auth(backend_obj, redirect_name=REDIRECT_FIELD_NAME)
Example #2
0
def disconnect(request, provider, association_id=None):
    request.social_strategy = DjangoStrategy(DjangoStorage, request)
    try:
        backend_cls = get_backend(BACKENDS, provider)
        backend_obj = backend_cls(request.social_strategy, "")
    except MissingBackend:
        raise Http404('Backend not found')

    return do_disconnect(backend_obj,
                         request.user,
                         association_id,
                         redirect_name=REDIRECT_FIELD_NAME)
Example #3
0
 def setUp(self):
     super().setUp()
     Backend = module_member("social_core.backends.github.GithubOAuth2")
     self.strategy = DjangoStrategy(DjangoStorage)
     self.backend = Backend(self.strategy, redirect_uri="/complete/github")
     self.login_redirect_url = "/"
Example #4
0
def process_join_answer(message):
    invite_token = message.text
    try:
        company = Company.objects.get(invite_token=invite_token)
    except Company.DoesNotExist:
        bot.reply_to(
            message,
            _("Sorry, couldn't find lunch group with the given token. :("))
        state_registry.del_state(message.from_user.id)
        return

    # Authenticate user, create if need to
    from_user = message.from_user
    try:
        social_auth = UserSocialAuth.objects.get(provider='telegram',
                                                 uid=from_user.id)
    except UserSocialAuth.DoesNotExist:
        storage = DjangoStorage()
        strategy = DjangoStrategy(storage)
        backend = TelegramAuth(strategy)

        username = get_username(strategy, from_user.__dict__,
                                backend)['username']
        with transaction.atomic():
            user = create_user(strategy,
                               from_user.__dict__,
                               backend,
                               username=username)['user']
            user.first_name = from_user.first_name or ''
            user.last_name = from_user.last_name or ''
            user.has_telegram = True
            user.telegram_chat_id = message.chat.id
            user.save()
            UserSocialAuth.objects.get_or_create(provider='telegram',
                                                 uid=from_user.id,
                                                 defaults={
                                                     'extra_data':
                                                     from_user.__dict__,
                                                     'user': user,
                                                 })
    else:
        user = social_auth.user

    if not user.is_active:
        bot.send_message(message.chat.id,
                         _('Sorry, your account has been deactivated.'))
        return

    # Create employee and add him to the group
    employee, created = Employee.objects.get_or_create(company=company,
                                                       user=user)
    if created:
        msg = _(
            "You've successfully joined lunch group «{}». "
            "You may manage your participation with /offline and /online commands."
        ).format(company.name)
    else:
        msg = _(
            "You've joined lunch group «{}» already. "
            "You may manage your participation with /offline and /online commands."
        ).format(company.name)
    bot.send_message(message.chat.id, msg)