Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Django 3.0.5 compatibility problem #66

Open
silvioBi opened this issue Apr 12, 2020 · 14 comments
Open

Django 3.0.5 compatibility problem #66

silvioBi opened this issue Apr 12, 2020 · 14 comments

Comments

@silvioBi
Copy link

Hello I'm using Django 3.0.5 and django.utils.decorators.available_attrs is not supported anymore but it is used in lazysignup/decorators.py. I have substituted it with WRAPPER_ASSIGNMENTS as suggested in the docs and this it works fine:

from functools import wraps

from django.conf import settings
from django.contrib.auth import SESSION_KEY
from django.contrib.auth import authenticate
from django.contrib.auth import get_user
from django.contrib.auth import login
from django.shortcuts import redirect
from functools import WRAPPER_ASSIGNMENTS

from lazysignup.constants import USER_AGENT_BLACKLIST
from lazysignup.utils import is_lazy_user

ALLOW_LAZY_REGISTRY = {}


def allow_lazy_user(func):
    def wrapped(request, *args, **kwargs):
        assert hasattr(request, 'session'), ("You need to have the session "
                                             "app installed")
        if getattr(settings, 'LAZYSIGNUP_ENABLE', True):
            # If the user agent is one we ignore, bail early
            ignore = False
            request_user_agent = request.META.get('HTTP_USER_AGENT', '')
            for user_agent in USER_AGENT_BLACKLIST:
                if user_agent.search(request_user_agent):
                    ignore = True
                    break

            # If there's already a key in the session for a valid user, then
            # we don't need to do anything. If the user isn't valid, then
            # get_user will return an anonymous user
            if get_user(request).is_anonymous and not ignore:
                # If not, then we have to create a user, and log them in.
                from lazysignup.models import LazyUser
                user, username = LazyUser.objects.create_lazy_user()
                request.user = None
                user = authenticate(username=username)
                assert user, ("Lazy user creation and authentication "
                              "failed. Have you got "
                              "lazysignup.backends.LazySignupBackend in "
                              "AUTHENTICATION_BACKENDS?")
                # Set the user id in the session here to prevent the login
                # call cycling the session key.
                request.session[SESSION_KEY] = user.id
                login(request, user)
        return func(request, *args, **kwargs)

    return wraps(func)(wrapped)


def require_lazy_user(*redirect_args, **redirect_kwargs):
    def decorator(func):
        @wraps(func, assigned=WRAPPER_ASSIGNMENTS)
        def inner(request, *args, **kwargs):
            if is_lazy_user(request.user):
                return func(request, *args, **kwargs)
            else:
                return redirect(*redirect_args, **redirect_kwargs)
        return inner
    return decorator


def require_nonlazy_user(*redirect_args, **redirect_kwargs):
    def decorator(func):
        @wraps(func, assigned=WRAPPER_ASSIGNMENTS)
        def inner(request, *args, **kwargs):
            if not is_lazy_user(request.user):
                return func(request, *args, **kwargs)
            else:
                return redirect(*redirect_args, **redirect_kwargs)
        return inner
    return decorator

Sorry if I might have missed something or not used the right format but be patient this is the first issue that I open.
Hope it helped :)

@danfairs
Copy link
Owner

Sorry for not responding sooner, github mails go into a bit of a black hole for me :(

I've reproduced this locally and will push a fix over the next day or two. Thanks!

@silvioBi
Copy link
Author

silvioBi commented May 1, 2020

No problema, glad it helped :)

@silvioBi silvioBi closed this as completed May 1, 2020
@danfairs
Copy link
Owner

danfairs commented May 5, 2020

Reopening because I haven’t fixed it yet :)

@danfairs danfairs reopened this May 5, 2020
agoloborodko added a commit to agoloborodko/django-lazysignup that referenced this issue Aug 9, 2020
@NerdPraise
Copy link

This issue still haven't been fixed

tugcanolgun added a commit to tugcanolgun/django-lazysignup that referenced this issue Mar 23, 2021
The issue and a solution was raised by silvioBi on 2020-04-12
danfairs#66
@PolyMeilex
Copy link

Any progress on this? There are some forks that claim to work with Django 3.x, but I would rather use upstream

@MattFanto
Copy link

Would be nice if you can merge the related PR #67

Django 2.2 LTS support will end soon

@johnalbert-dot-py
Copy link

I hope this one will be fixed. BTW Thank You :)

@MattFanto
Copy link

It's merged but the in Pypi is not update, unfortunately, the PR was missing the version bump.
In any case you can also install it like this as a temporary workaround

django-lazysignup = {git = "https://github.com/danfairs/django-lazysignup", rev = "42470be"}

@Jazzthedog
Copy link

@MattFanto I tried to install it based on your command and it was a no go. Is that command able to be used in requirements.txt?
For example if this fixes the issue, would i have to manually change again lets say in 'heruko'? Thanks again.

@MattFanto
Copy link

@Jazzthedog depends on what you are using (poetry pipenv, ...) the pip command should be something like

pip install git+https://github.com/danfairs/django-lazysignup@42470be

@Jazzthedog
Copy link

Thanks Matt, I fixed the problem manually before you replied.. :)

The fact that i fixed it manually, when i update my requirements.txt, it will still be wrong? And, I can assume later when i publish this project to Heroku for example, i will have to go back manually correct it again :(

How do you 'pin' to a particular package commit# in requirements.txt?

@julianwachholz
Copy link

Hi everyone! Unfortunately I haven't been able to reach Dan about this but a couple weeks ago I decided to create a spin-off package built from the ground up with modern Django and Python versions in mind.

If you're still waiting for Django 3.0+ support, I invite you to check out django-guest-user
I also added CBV mixins and an Allauth integration, among other things.

@blag
Copy link

blag commented Dec 24, 2021

Awesome, thanks @julianwachholz, I'll switch to that.

@winny-
Copy link

winny- commented Feb 14, 2023

I reached out to Dan on twitter, his company's twitter, and his linkedin. Hopefully something happens =)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants