django-lazysignup icon indicating copy to clipboard operation
django-lazysignup copied to clipboard

Django 3.0.5 compatibility problem

Open silvioBi opened this issue 4 years ago • 14 comments

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 :)

silvioBi avatar Apr 12 '20 11:04 silvioBi

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!

danfairs avatar Apr 30 '20 21:04 danfairs

No problema, glad it helped :)

silvioBi avatar May 01 '20 08:05 silvioBi

Reopening because I haven’t fixed it yet :)

danfairs avatar May 05 '20 18:05 danfairs

This issue still haven't been fixed

NerdPraise avatar Dec 21 '20 04:12 NerdPraise

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

PolyMeilex avatar Jul 13 '21 21:07 PolyMeilex

Would be nice if you can merge the related PR https://github.com/danfairs/django-lazysignup/pull/67

Django 2.2 LTS support will end soon

MattFanto avatar Aug 22 '21 13:08 MattFanto

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

johnalbert-dot-py avatar Oct 12 '21 16:10 johnalbert-dot-py

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"}

MattFanto avatar Oct 12 '21 17:10 MattFanto

@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.

Jazzthedog avatar Nov 29 '21 01:11 Jazzthedog

@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

MattFanto avatar Nov 29 '21 15:11 MattFanto

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?

Jazzthedog avatar Nov 30 '21 23:11 Jazzthedog

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.

julianwachholz avatar Dec 06 '21 15:12 julianwachholz

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

blag avatar Dec 24 '21 09:12 blag

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

winny- avatar Feb 14 '23 00:02 winny-