A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://django-guest-user.readthedocs.io/en/latest/api.html below:

Website Navigation


API — django-guest-user main documentation

API Decorators
@allow_guest_user[source]

Allow anonymous users to access the view by creating a guest user.

Usage example:

from guest_user.decorators import allow_guest_user

@allow_guest_user
def hello_world(request):
    return HttpResponse(f"Hello {request.user.username}!")
@guest_user_required(function=None, anonymous_url=None, registered_url=None)[source]

Current user must be a temporary guest.

Since being a guest user is not a state that a registered user can ever revert back to, there is no “next” URL handling in this decorator.

Parameters:

Usage example:

from guest_user.decorators import guest_user_required

@guest_user_required(anonymous_url="/login/", registered_url="/dashboard/")
def only_for_guests(request):
    pass
@regular_user_required(function=None, login_url=None, convert_url=None, redirect_field_name='next')[source]

Current user must not be a temporary guest.

The redirected URL will get a “next” parameter added to the URL in order to redirect the user back to the page they were trying to access.

Parameters:
  • login_url – Redirect target for anonymous users. Defaults to django:ref/settings:``login_url``.

  • convert_url – Redirect target for guest users. Defaults to GUEST_USER_CONVERT_URL.

  • redirect_field_name – URL parameter used to redirect to the origin page. Defaults to django.contrib.auth.REDIRECT_FIELD_NAME (= “next”).

Usage example:

from guest_user.decorators import regular_user_required

@regular_user_required
def permanent_users_only(request):
    # This view will redirect anonymous users to the login page
    # and guest users to the convert page, respectively.
    # A redirect URL parameter will be added to users can jump right back here.
    pass
Mixins
class AllowGuestUserMixin[source]

Allow anonymous users to access the view by creating a guest user.

This mixin does not require overriding any attributes.

Example usage:

from guest_user.mixins import AllowGuestUserMixin

class HelloWorldView(AllowGuestUserMixin, View):
    def get(self, request):
        return HttpResponse(f"Hello {request.user.username}!")
class GuestUserRequiredMixin[source]

Current user must be a temporary guest.

Since being a guest user is not a state that a registered user can ever revert back to, there is no “next” URL handling in this mixin.

Example usage:

from guest_user.mixins import GuestUserRequiredMixin

class OnlyGuestView(GuestUserRequiredMixin, View):
    anonymous_url = "/login/"
    registered_url = "/dashboard/"
anonymous_url: str = None

Redirect target for anonymous users. Defaults to GUEST_USER_REQUIRED_ANON_URL.

registered_url: str = None

Redirect target for registered users. Defaults to GUEST_USER_REQUIRED_USER_URL.

class RegularUserRequiredMixin[source]

Current user must not be a temporary guest.

Anonymous users will be redirected to login_url. Guest users will be redirected to the convert_url.

The redirected URL will get a “next” parameter added to the URL in order to redirect the user back to the page they were trying to access.

Example usage:

from guest_user.mixins import RegularUserRequiredMixin

class RealUsersOnlyView(RegularUserRequiredMixin, View):
    login_url = "/login/"
    convert_url = "/convert/?from=RealUsersOnlyView"
login_url: str = None

Redirect target for anonymous users. Defaults to django:ref/settings:``login_url``.

convert_url: str = None

Redirect target for guest users. Defaults to GUEST_USER_CONVERT_URL.

redirect_field_name: str = 'next'

URL parameter used to redirect to the origin page. Defaults to django.contrib.auth.REDIRECT_FIELD_NAME (= “next”).

Template Tags

The django-guest-user template tags are registered in the guest_user namespace.

To use them, load them in your template:

is_guest_user(user) bool[source]

Template filter to check if the passed object is a guest user.

Usage

{% if user|is_guest_user %}
  Hello guest.
{% endif %}
Functions

Several helper functions are provided for advanced usage.

maybe_create_guest_user(request)[source]

Create a guest user and log them in.

This function will create and authenticate a new guest user should the visitor not be authenticated already and their user agent isn’t on the block list.

get_guest_model()[source]

Return the configured Guest model.

is_guest_user(user) bool[source]

Check if the given user instance is a temporary guest.

generate_uuid_username() str[source]

Generate a random username based on UUID.

generate_numbered_username() str[source]

Generate a random username based on a prefix and a random number.

generate_friendly_username() str[source]

Generate a random username with adjective and nouns put together.

Requires random-username to be installed.

Signals
guest_created = <django.dispatch.dispatcher.Signal object>

A visitor accessed a view that created a guest user.

Parameters:
  • user – The new guest user.

  • request – The request that created the guest user.

converted = <django.dispatch.dispatcher.Signal object>

A guest user converted to a regular registered user.

Parameters:

user – The now registered user.

Models
class Guest(*args, **kwargs)[source]

A temporary guest user.

Users linked to a Guest instance are considered temporary guests and will be deleted by cleanup jobs after their expiration.

The age of a guest user is determined by the created_at field.

This model is swappable with the GUEST_USER_MODEL setting. Custom Guest models should use the GuestManager or a custom manager that implements the same custom methods.

class GuestManager(*args, **kwargs)[source]

Manager for Guest objects.

create_guest_user(request=None, username: str | None = None) User[source]

Create a guest user.

Returns the underlying User object.

Parameters:
  • request – The current request object.

  • username – The preferred username for the user, may be None.

convert(form: ModelForm) User[source]

Convert a guest user to a regular one.

The form passed in is expected to be a ModelForm instance, bound to the user to be converted.

Raises:

TypeError if the user is not a temporary guest.

Parameters:

form – The model form used to create the permanent user.

Returns:

The converted User object.

delete_expired()[source]

Delete all expired guest users.

Forms
class UserCreationForm(*args, **kwargs)[source]

A modelform that creates a standard Django user.

Custom implementations must implement get_credentials().

get_credentials() dict[source]

Get the credentials required to log the user in after conversion.

The credentials are passed to Django’s authenticate().

Returns:

Login credentials. This is usually a dict with “username” and “password”.

property media

Return all media required to render the widgets on this form.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4