Code Monkey home page Code Monkey logo

django-guest-user's Introduction

Code Lint Python Tests Documentation

django-guest-user

Allow visitors to interact with your site like a temporary user ("guest") without requiring registration.

Anonymous visitors who request a decorated page get a real temporary user object assigned and are logged in automatically. They can use the site like a normal user until they decide to convert to a real user account to save their data.

Inspired by and as an alternative for django-lazysignup and rewritten for Django 3.2+ and Python 3.8+.

Documentation

Find the complete documentation on Read the Docs.

Quickstart

  1. Install the django-guest-user package from PyPI

  2. Add guest_user to your INSTALLED_APPS and migrate your database

  3. Add guest_user.backends.GuestBackend to your AUTHENTICATION_BACKENDS

  4. Include guest_user.urls in your URLs

  5. Decorate your views with @allow_guest_user:

    from guest_user.decorators import allow_guest_user
    
    @allow_guest_user
    def my_view(request):
        assert request.user.is_authenticated
        return render(request, "my_view.html")

A more detailed guide is available in the installation documentation.

Contributing

All contributions are welcome! Please read the contributing guidelines in this repostory.

Development Status

This project is under active development. Thanks to previous work the core functionality is well-established and this package builds on top of it.

This project was created because the original project has been in an inactive state without major updates in a long time. The code base was rewritten with only modern versions of Python and Django in mind.

django-guest-user's People

Contributors

blag avatar julianwachholz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

django-guest-user's Issues

How to log in instead of registering a new user?

Hi @julianwachholz,

Thanks a lot for working on this package, it's very useful!

I had a question I hoped you could help me answer: Is there a way to allow the user to log in, instead of creating a new user during the conversion? So that if a user hadn't logged in before putting items in the cart, he can still keep them

Thank you.

Best,
Dylan

missing migration ?

Hello,

After installing django-guest-user, if I run the makemigrations command of django manage.py, it create a migration with :

# Generated by Django 4.2.2 on 2023-06-27 14:00

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('guest_user', '0001_initial'),
    ]

    operations = [
        migrations.AlterField(
            model_name='guest',
            name='id',
            field=models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
        ),
    ]

Is it normal ?

Using django-guest-user on the home page

I'm using django-guest-user on my home page to allow visitors to get some use out of the website before forcing them to register as users. The problem is (and I can't figure a way around it yet) is dealing with user Login after a user visits the homepage. Since I only @allow_guest_user on the homepage, I assumed that if you clicked to login, it would stop the guest user experience, but I'm assuming that isn't exactly how it works.

Are there any tricks or workarounds to allowing logins for someone considered a "guest user" who is actually another user logged out who is visiting the site?

Thanks again for the package - this is solving a huge use case for me!

Why am I told to add django.contrib.sessions to INSTALLED_APPS?

Hello, I'm trying to apply this package in my project, but I'm stuck here
When I visit an API with @allow_guest_user above the get method:

class GuestUserLogin(GenericAPIView):
    permission_classes = [permissions.AllowAny]

    @allow_guest_user
    def get(self, request):
        username = request.user.username
        return JsonResponse({'detail': f'Guest user login success!{username}'})

and if I visit this API by router path('guestlogin/', GuestUserLogin.as_view()),, it returns an error:

Internal Server Error: /guestlogin/
Traceback (most recent call last):
  File "F:\learn\cubewall\venv\Lib\site-packages\asgiref\sync.py", line 534, in thread_handler
    raise exc_info[1]
  File "F:\learn\cubewall\venv\Lib\site-packages\django\core\handlers\exception.py", line 42, in inner
    response = await get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\learn\cubewall\venv\Lib\site-packages\asgiref\sync.py", line 534, in thread_handler
    raise exc_info[1]
  File "F:\learn\cubewall\venv\Lib\site-packages\django\core\handlers\base.py", line 253, in _get_response_async
    response = await wrapped_callback(
               ^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\learn\cubewall\venv\Lib\site-packages\asgiref\sync.py", line 479, in __call__
    ret: _R = await loop.run_in_executor(
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\concurrent\futures\thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\learn\cubewall\venv\Lib\site-packages\asgiref\sync.py", line 538, in thread_handler
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "F:\learn\cubewall\venv\Lib\site-packages\django\views\decorators\csrf.py", line 56, in wrapper_view
    return view_func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\learn\cubewall\venv\Lib\site-packages\django\views\generic\base.py", line 104, in view
    return self.dispatch(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\learn\cubewall\venv\Lib\site-packages\rest_framework\views.py", line 509, in dispatch
    response = self.handle_exception(exc)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\learn\cubewall\venv\Lib\site-packages\rest_framework\views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "F:\learn\cubewall\venv\Lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
    raise exc
  File "F:\learn\cubewall\venv\Lib\site-packages\rest_framework\views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "F:\learn\cubewall\venv\Lib\site-packages\guest_user\decorators.py", line 28, in wrapper
    maybe_create_guest_user(request)
  File "F:\learn\cubewall\venv\Lib\site-packages\guest_user\functions.py", line 21, in maybe_create_guest_user
    assert hasattr(
AssertionError: Please add 'django.contrib.sessions' to INSTALLED_APPS.

However, 'django.contrib.sessions' do exist in INSTALLED_APPS:

INSTALLED_APPS = [
    'daphne',
    'wall',
    'simpleui',
    'rest_framework',
    'guest_user',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

I tried to move it to the top or the bottom of INSTALLED_APPS but neither worked.
If I remove the decorator @allow_guest_user, it worked well(with a real user logged in)

My Django version: 4.2.4
Python version: 3.11.1

RFC on a changing GuestManager.convert to update in-place

Hi @julianwachholz,

First off, thank you very much for maintaining this package for new versions of Django!

I've opened this ticket to propose a relatively simple change (or addition) of the GuestManager in the package that I think would enable greater functionality for devs wanting to create guest users.

Background

Business Use Case

I started using this package to enable guest users functionality on a site, with the goal that:

  • ephemeral guest users would be able to modify the db (ie CRUD their own objects like a regular user)
  • after conversion all of the state in related models would be maintained

After reading the documentation for django-guest-user I didn't see any flags that stated this usage wasn't possible, but after installing the package and trying to use it, to the best of my understanding, the use case above is not supported. I think this has also been raised previously in #3.

EDIT:

  • This use case is supported; I had a misconfigured form and had misread the guest creation workflow when debugging.

Thanks very much for your time!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.