django-lazysignup

repository·master·Indexed 19 days ago

https://github.com/danfairs/django-lazysignup

A Django package that enables a 'lazy signup' workflow, allowing anonymous users to interact with a site using temporary accounts before officially registering. It provides decorators like @allow_lazy_user, @require_lazy_user, and @require_nonlazy_user to manage access, as well as utilities to detect lazy users in templates and code. The library includes a conversion process to transition temporary accounts to permanent ones and a management command to remove expired users based on SESSION_COOKIE_AGE.

Tokens
3.2K
Snippets
16
Records
20
Agent score
65%

What's inside django-lazysignup

  1. Overview of django-lazysignup

    master
    django-lazysignup is a Django package that enables a 'lazy signup' workflow. It allows anonymous or unauthenticated users to interact with a site using a temporary user account, providing an experience similar to authenticated users without requiring immediate registration. Users can later convert these temporary accounts into permanent, real user accounts at any time.
  2. Install dependencies and requirements for development

    master

    To set up the development environment, clone the repository and install the package in editable mode with all optional dependencies using the [all] extra.

    git clone https://github.com/danfairs/django-lazysignup
    cd django-lazysignup
    
    # Install dependencies and requirements
    pip install -e .[all]
  3. Configure django-lazysignup in Django settings

    master

    After installation, you must perform two configuration steps in your Django settings file:

    1. Add 'lazysignup' to your INSTALLED_APPS.
    2. Add 'lazysignup.backends.LazySignupBackend' to your AUTHENTICATION_BACKENDS. It is recommended to keep the default django.contrib.auth.backends.ModelBackend as well.
    INSTALLED_APPS = [
        # ...
        'django.contrib.auth',
        'lazysignup',
        # ...
    ]
    
    AUTHENTICATION_BACKENDS = (
        'django.contrib.auth.backends.ModelBackend',
        'lazysignup.backends.LazySignupBackend',
    )
  4. Customize the user conversion process

    master

    The convert view allows temporary users to become real users by providing a username and password. You can customize this process by providing your own form class via the LAZYSIGNUP_CUSTOM_USER_CREATION_FORM setting.

    Custom Form Requirements

    Your custom form must satisfy the following:

    1. Initialization: It must accept the generated User object via an instance keyword argument.
    2. Persistence: It must implement a save() method to convert the user.
    3. Credentials: It must implement a get_credentials() method that returns a dictionary suitable for django.contrib.auth.authenticate() (typically containing username and password keys).

    Configuration

    Set the path to your form class in your Django settings:

    LAZYSIGNUP_CUSTOM_USER_CREATION_FORM = 'myproject.apps.myapp.forms.MyForm'
  5. Configure a custom User model for lazysignup

    master

    By default, lazysignup uses the model defined in your Django AUTH_USER_MODEL setting. If you are using a custom user model that is different from your AUTH_USER_MODEL, you must explicitly specify it using the LAZYSIGNUP_USER_MODEL setting. This setting expects a standard dotted Django model path.

    Requirements for Custom User Models: To work correctly with lazysignup, your custom user model's manager must satisfy the following:

    1. It must have a create_user method with the same signature and semantics as django.contrib.auth.models.UserManager.
    2. It must support a get() method on the manager.
    3. It must support lookups by both primary key and username.

    If your model subclasses Django's AbstractUser, you can typically use the standard UserManager to satisfy these requirements.

    # In your settings.py
    LAZYSIGNUP_USER_MODEL = 'myapp.CustomUser'
  6. Remove expired users via management command

    master

    To prevent performance degradation caused by a buildup of unconverted user accounts, run the remove_expired_users management command. This command identifies and removes user accounts whose associated sessions have expired based on settings.SESSION_COOKIE_AGE.

    In production environments, it is recommended to schedule this command using cron or a similar task scheduler.

    python manage.py remove_expired_users
  7. Remove expired users via Django Admin

    master

    You can manually remove expired users through the Django Admin interface by following these steps:

    1. Navigate to the LazyUser instances list.
    2. Select the users you wish to remove.
    3. From the action dropdown, select: Delete selected lazy users and unconverted users older than settings.SESSION_COOKIE_AGE.
    4. Click Go.

    Warning: This action calls user.delete() on each user, which triggers a cascade delete. Any related data associated with these users will also be removed from the system. If you need to preserve specific data, you must implement a custom cleanup job.

  8. Configure local database for testing

    master

    The test suite can be run against different database backends. Set the DB environment variable to select the target database after performing the following setup steps:

    PostgreSQL

    Create a user and database named lazysignup:

    psql -c "CREATE USER lazysignup with login createdb password 'lazysignup';"
    psql -c "CREATE DATABASE lazysignup with OWNER lazysignup;"
    export DB="local-postgres"

    MySQL

    Create the database and user with appropriate privileges:

    mysql -e "CREATE DATABASE lazysignup CHARACTER SET utf8;"
    mysql -e "CREATE USER 'lazysignup'@'localhost' IDENTIFIED BY 'lazysignup';"
    mysql -e "GRANT ALL PRIVILEGES ON lazysignup.* to 'lazysignup'@'localhost';"
    mysql -e "FLUSH PRIVILEGES;"
    export DB="local-mysql"

    SQLite

    For a lightweight local test, use SQLite:

    export DB="sqlite"
    # PostgreSQL
    export DB="local-postgres"
    
    # MySQL
    export DB="local-mysql"
    
    # SQLite
    export DB="sqlite"
  9. Run tests and check coverage

    master

    Run the test suite using coverage to ensure code quality. The project requires a minimum coverage threshold of 98%.

    To run standard tests:

    coverage run manage.py test
    coverage report --fail-under=98

    To run tests with custom user settings:

    coverage run manage.py test --settings=custom_user_tests.settings
    coverage report --fail-under=98