|
| 1 | +# django-guest-user |
| 2 | + |
| 3 | +A Django app that allows visitors to interact with your site as a guest user |
| 4 | +without requiring registration. |
| 5 | + |
| 6 | +Largely inspired by [django-lazysignup](https://github.com/danfairs/django-lazysignup) and rewritten for Django 3 and Python 3.6 and up. |
| 7 | + |
| 8 | +## Installation |
| 9 | + |
| 10 | +Install the package with your favorite package manager from PyPI: |
| 11 | + |
| 12 | +``` |
| 13 | +pip install django-guest-user |
| 14 | +``` |
| 15 | + |
| 16 | +Add the app to your `INSTALLED_APPS` and `AUTHENTICATION_BACKENDS`: |
| 17 | + |
| 18 | +```python |
| 19 | +INSTALLED_APPS = [ |
| 20 | + ... |
| 21 | + "django_guest_user", |
| 22 | +] |
| 23 | + |
| 24 | +AUTHENTICATION_BACKENDS = [ |
| 25 | + "django.contrib.auth.backends.ModelBackend", |
| 26 | + "guest_user.backends.GuestBackend", |
| 27 | +] |
| 28 | +``` |
| 29 | + |
| 30 | +Add the patterns to your URL config: |
| 31 | + |
| 32 | +```python |
| 33 | +urlpatterns = [ |
| 34 | + ... |
| 35 | + path("convert/", include("guest_user.urls")), |
| 36 | +] |
| 37 | +``` |
| 38 | + |
| 39 | +Don't forget to run migrations: |
| 40 | + |
| 41 | +``` |
| 42 | +python manage.py migrate |
| 43 | +``` |
| 44 | + |
| 45 | +## How to use |
| 46 | + |
| 47 | +Guest users are not created for every unauthenticated request. |
| 48 | +Instead, use the `@allow_guest_user` decorator on a view to enable |
| 49 | +that view to be accessed by a temporary guest user. |
| 50 | + |
| 51 | +```python |
| 52 | +from guest_user.decorators import allow_guest_user |
| 53 | + |
| 54 | +@allow_guest_user |
| 55 | +def my_view(request): |
| 56 | + # Will always be either a registered a guest user. |
| 57 | + username = request.user.username |
| 58 | + return HttpResponse(f"Hello, {username}!") |
| 59 | +``` |
| 60 | + |
| 61 | +## API |
| 62 | + |
| 63 | +### `@guest_user.decorators.allow_guest_user` |
| 64 | + |
| 65 | +View decorator that will create a temporary guest user in the event |
| 66 | +that the decorated view is accessed by an unauthenticated visitor. |
| 67 | + |
| 68 | +Takes no arguments. |
| 69 | + |
| 70 | +### `@guest_user.decorators.guest_user_required(redirect_field_name="next", login_url=None)` |
| 71 | + |
| 72 | +View decorator that redirects to a given URL if the accessing user is |
| 73 | +anonymous or already authenticated. |
| 74 | + |
| 75 | +Arguments: |
| 76 | + |
| 77 | +- `redirect_field_name`: URL query parameter to use to link back in the case of a redirect to the login url. Defaults to `django.contrib.auth.REDIRECT_FIELD_NAME` ("next"). |
| 78 | +- `login_url`: URL to redirect to if the user is not authenticated. Defaults to the `LOGIN_URL` setting. |
| 79 | + |
| 80 | +### `@guest_user.decorators.regular_user_required(redirect_field_name="next", login_url=None)` |
| 81 | + |
| 82 | +Decorator that will not allow guest users to access the view. |
| 83 | +Will redirect to the conversion page to allow a guest user to fully register. |
| 84 | + |
| 85 | +Arguments: |
| 86 | + |
| 87 | +- `redirect_field_name`: URL query parameter to use to link back in the case of a redirect to the login url. Defaults to `django.contrib.auth.REDIRECT_FIELD_NAME` ("next"). |
| 88 | +- `login_url`: URL to redirect to if the user is a guest. Defaults to `"guest_user_convert"`. |
| 89 | + |
| 90 | +### `guest_user.functions.get_guest_model()` |
| 91 | + |
| 92 | +The guest user model is swappable. This function will return the currently configured model class. |
| 93 | + |
| 94 | +### `guest_user.functions.is_guest_user(user)` |
| 95 | + |
| 96 | +Check wether the given user instance is a temporary guest. |
| 97 | + |
| 98 | +### `guest_user.signals.converted` |
| 99 | + |
| 100 | +Signal that is dispatched when a guest user is converted to a regular user. |
| 101 | + |
| 102 | +### Template tag `is_guest_user` |
| 103 | + |
| 104 | +A filter to use in templates to check if the user object is a guest. |
| 105 | + |
| 106 | +``` |
| 107 | +{% load guest_user %} |
| 108 | +
|
| 109 | +{% if user|is_guest_user %} |
| 110 | + Hello guest. |
| 111 | +{% endif %} |
| 112 | +``` |
| 113 | + |
| 114 | +## Settings |
| 115 | + |
| 116 | +Various settings are provided to allow customization of the guest user behavior. |
| 117 | + |
| 118 | +### `GUEST_USER_ENABLED` |
| 119 | + |
| 120 | +`bool`. If `False`, the `@allow_guest_user` decorator will not create guest users. |
| 121 | +Defaults to `True`. |
| 122 | + |
| 123 | +### `GUEST_USER_MODEL` |
| 124 | + |
| 125 | +`str`. The swappable model identifier to use as the guest model. |
| 126 | +Defaults to `"guest_user.Guest"`. |
| 127 | + |
| 128 | +### `GUEST_USER_NAME_GENERATOR` |
| 129 | + |
| 130 | +`str`. Import path to a function that will generate a username for a guest user. |
| 131 | +Defaults to `"guest_user.functions.generate_uuid_username"`. |
| 132 | + |
| 133 | +Included with the package are two alternatives: |
| 134 | + |
| 135 | +`"guest_user.functions.generate_numbered_username"`: Will create a random four digit |
| 136 | +number prefixed by `GUEST_USER_NAME_PREFIX`. |
| 137 | + |
| 138 | +`"guest_user.functions.generate_friendly_username"`: Creates a friendly and easy to remember username by combining an adjective, noun and number. Requires `random_username` to be installed. |
| 139 | + |
| 140 | +### `GUEST_USER_NAME_PREFIX` |
| 141 | + |
| 142 | +`str`. A prefix to use with the `generate_numbered_username` generator. |
| 143 | +Defaults to `"Guest"`. |
| 144 | + |
| 145 | +### `GUEST_USER_CONVERT_FORM` |
| 146 | + |
| 147 | +`str`. Import path for the guest conversion form. |
| 148 | +Must implement `get_credentials` to be passed to Django's `authenticate` function. |
| 149 | +Defaults to `"guest_user.forms.UserCreationForm"`. |
| 150 | + |
| 151 | +### `GUEST_USER_CONVERT_PREFILL_USERNAME` |
| 152 | + |
| 153 | +`bool`. Set the generated username as initial value on the conversion form. |
| 154 | +Defaults to `False`. |
| 155 | + |
| 156 | +### `GUEST_USER_CONVERT_URL` |
| 157 | + |
| 158 | +`str`. URL name for the convert view. |
| 159 | +Defaults to `"guest_user_convert"`. |
| 160 | + |
| 161 | +### `GUEST_USER_CONVERT_REDIRECT_URL` |
| 162 | + |
| 163 | +`str`. URL name to redirect to after conversion, unless a redirect parameter was provided. |
| 164 | +Defaults to `"guest_user_convert_success"`. |
| 165 | + |
| 166 | +### `GUEST_USER_BLOCKED_USER_AGENTS` |
| 167 | + |
| 168 | +`list[str]`. Web crawlers and other user agents to block from becoming guest users. |
| 169 | +The list will be combined into a regular expression. |
| 170 | +Default includes a number of well known bots and spiders. |
| 171 | + |
| 172 | +## Status |
| 173 | + |
| 174 | +This project is currently untested. But thanks to [previous work](https://github.com/danfairs/django-lazysignup) it is largely functional. |
| 175 | + |
| 176 | +I decided to rewrite the project since the original project hasn't seen any |
| 177 | +larger updates for a few years now and the code base was written a long time ago. |
0 commit comments