This release is a pre-release and may not be stable for production use.
django-switchuser
django-switchuser makes it easy for an administrator to switch to temporarily switch to another account by visiting /su.
Assumptions
Because django-switchuser was a quick project, it does make a couple of assumptions:
Your site uses a base.html template with a {% block content %} block. If this assumption does not hold: you will need to define your own su/login.html template, something like this:
{% extends "yourbase.html" %} {% block yourcontent %} {% include "su/login_form.html" %} {% endblock %}Any superuser is allowed to switch to any other user. If this assumption does not hold: you’ll need to submit a pull request… Sorry :(
Installation
pip install django-switchuser
Add a few things to settings.py (note: the SuStateMiddleware must appear after the AuthenticationMiddleware):
INSTALLED_APPS = ( ... "django_switchuser", ... ) MIDDLEWARE_CLASSES = ( ... "django.contrib.sessions.middleware.SessionMiddleware", "django.contrib.auth.middleware.AuthenticationMiddleware", "django_switchuser.middleware.SuStateMiddleware", ... ) TEMPLATE_CONTEXT_PROCESSORS = ( ... "django_switchuser.context_processors.su_state", ... )Add an entry to urls.py (note: you can use whatever URL you’d like; su/ is simply convenient):
urlpatterns += patterns("", ... url(r"^su/", include("django_switchuser.urls")), ... )(Optional) Add an entry to your base.html template (don’t use a base.html? See Assumptions!) which will show a convenient logout button:
<body> ... {% include "su/statusbar.html" %} </body>
Doing Your Own Thing
Doing your own thing is easy. The SuStateMiddleware and su_state context processors add a su_state attribute to the request and a su_state variable to the template rendering context. su_state is an instance of django_switchuser.state.SuState, and has the following attributes:
- SuState.is_active():
Returns True if the current user has been switched.
- SuState.auth_user:
The original user associated with the request. For example, if the user admin has switched to jane, then su_state.auth_user will be admin.
- SuState.active_user:
The user which has been switched to, or None if no user has been switched. For example, if the user admin has switched to jane, then su_state.active_user will be admin.
- SuState.can_su():
Returns True if the current user is allowed to switch.
- SuState.available_users():
Returns a QuerySet of User of the users which the current user is allowed to switch to.
- SuState.set_su_user_id(su_user_id):
Switches to the user with id su_user_id.
- SuState.clear_su():
Reverts back to the original user.
For example, if you don’t like the default switch user bar, you could add your own to your base.html:
{% if su_state.is_active %}
<a href="{% url su-logout %}">deactive {{ su.active_user.username }}</a>
{% elif su_state.can_su %}
<a href="{% url su-login %}">switch user</a>
{% endif %}
Metadata
Release files for django-switchuser 0.0.2-alpha
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django-switchuser-0.0.2-alpha.tar.gz | 5.3 kB | Without provenance | Details for django-switchuser-0.0.2-alpha.tar.gz |
Release files / django-switchuser-0.0.2-alpha.tar.gz
| Download URL | django-switchuser-0.0.2-alpha.tar.gz |
|---|---|
| Size | 5.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fe306aa132779b8e51e2baee26d7e21784ab7578419c9f08375ad2197ff89f20
|
|
BLAKE2b-256 checksum How to use checksums |
41ea541d91f67de9ea75122e3cfb7eb2581776d0c9dc5c3194885136e1ae29c2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for django-switchuser 0.0.2-alpha
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.