Skip to main content

Conveniently store reference to request user on thread/db level.

Project description

Quickstart

Install django-currentuser:

pip install django-currentuser

Note: if there is a new Django version released that the library hasn’t been upgraded to support yet, e.g.:

The conflict is caused by:

The user requested django==5.1 django-currentuser 0.8.0 depends on Django<5.1 and >=4.2

you can try to install it with the unsupported/untested Django version by using the DJANGO_CURRENTUSER_USE_UNSUPPORTED_DJANGO environment variable

DJANGO_CURRENTUSER_USE_UNSUPPORTED_DJANGO=1 pip install django-currentuser

Ade it to the middleware classes in your settings.py:

MIDDLEWARE = (
    ...,
    'django_currentuser.middleware.ThreadLocalUserMiddleware',
)

Then use it in a project:

from django_currentuser.middleware import (
    get_current_user, get_current_authenticated_user)

# As model field:
from django_currentuser.db.models import CurrentUserField
class Foo(models.Model):
    created_by = CurrentUserField()
    updated_by = CurrentUserField(on_update=True)

Differences to django-cuser

Both libraries serve the same purpose, but be aware of these differences (as of django-cuser v.2017.3.16):

  • django-currentuser’s CurrentUserField stores the reference to the request user at initialization of the model instance and still allows you to overwrite the value before saving. django-cuser sets the value in the pre_save handler of the field just before writing it to the database. Intermediate changes will be ignored.

  • django-cuser deletes the user reference from the thread after finishing a response and it will therefore no longer be available for testing purposes.

Supported Versions

Note on [semver](https://semver.org/): While the fact that Django support stopped for version X, doesn’t have to mean it’s a breaking change for this lib, as it would be just a backward-compatible metadata patch, no code change. However, at some point support needs to be dropped - so for simplicity, the project follows what Django supports. If someone needs to use different, unsupported version, DJANGO_CURRENTUSER_USE_UNSUPPORTED_DJANGO allows for it.

Release Notes

  • 0.9.0

    • by @bartvanandel

    • add support for Python 3.13

    • add support for Django 5.2

    • drop support for Python 3.8

    • drop support for Django 5.0

  • 0.8.0

    • add support for Django 5.1

    • drop support for Django 3.2

    • introduce DJANGO_CURRENTUSER_USE_UNSUPPORTED_DJANGO environment variable to make upgrades easier

  • 0.7.0

    • add support for Django 5.0

    • add support for Python 3.12

    • drop support for Django 4.0 and 4.1

  • 0.6.1

    • remove project transfer warning from README in order not to scare people away from the project

  • 0.6.0

    • add support for Django 4.0, 4.1, and 4.2

    • add support for Python 3.11

    • drop support for Python 3.6 and 3.7

  • 0.5.3 - add support for Django 3.2 and Python 3.9

  • 0.5.2 - Fixed Django deprecation warning about using ugettext_lazy()

  • 0.5.1 - add support for Django 3.1 and Python 3.8

  • 0.5.0 - add support for update on save (thank you @felubra) - no longer build on Python 3.5, deprecated

  • 0.4.3 - add support for Django 3.0

  • 0.4.2 - Minor fix for supported Django and Python versions

  • 0.4.0 - update supported versions

    • drop support for Python 3.4

    • drop support for Django 2.0

    • add support for Python 3.7

    • add support for Django 2.2

    • update tox3travis.py to not loose deployment feature

  • 0.3.4 - Use public Travis for packaging to remove dependency on outdated build system

  • 0.3.3 - drop Python 3.7 support due to build process problems

  • 0.3.1 - attempt to add Python 3.7 support

  • 0.3.0 - update supported versions according to https://www.djangoproject.com/download/#supported-versions and https://devguide.python.org/#status-of-python-branches

    • drop support for Python 3.2

  • 0.2.3 - support custom user model, drop Django 1.10 support

  • 0.2.2 - support Django 2.0

  • 0.2.1 - version fixes #9

    • support Django 1.11.x and not just 1.11.0

  • 0.2.0 - New middleclass format

    • Adapt to new object based middle class format of Django 1.10+

    • Drop support for deprecated Django versions 1.8 and 1.9

  • 0.1.1 - minor release

    • suppress warning for passed kwargs as long as they match the defaults (avoids them being printed during running tests when fields are cloned)

  • 0.1.0 - initial release

    • provides middleware + methods to set + retrieve reference of currently logged in user from thread

    • provides CurrentUserField that by default stores the currently logged in user

    • supports Django 1.10, 1.11 on python 2.7, 3.4, 3.5, and 3.6 - as per the official django docs

Contributing

As an open source project, we welcome contributions.

The code lives on github.

Reporting issues/improvements

Please open an issue on github or provide a pull request whether for code or for the documentation.

For non-trivial changes, we kindly ask you to open an issue, as it might be rejected. However, if the diff of a pull request better illustrates the point, feel free to make it a pull request anyway.

Pull Requests

  • for code changes

    • it must have tests covering the change. You might be asked to cover missing scenarios

    • the latest flake8 will be run and shouldn’t produce any warning

    • if the change is significant enough, documentation has to be provided

To trigger the packaging, run make release on the master branch with a changed version number.

Setting up all Python versions

sudo apt-get -y install software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
for version in 3.9 3.10 3.11 3.12 3.13; do
  py=python$version
  if ! which ${py}; then
    sudo apt-get -y install ${py} ${py}-dev
  fi
done
sudo add-apt-repository --remove ppa:deadsnakes/ppa
sudo apt-get update

Code of Conduct

As it is a Django extension, it follows Django’s own Code of Conduct. As there is no mailing list yet, please use github issues

Contributors

Current maintainer: @zsoldosp Initial development & maintenance: @PaesslerAG

For contributors, see github contributors.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_currentuser-0.9.0.tar.gz (13.7 kB view details)

Uploaded Source

Built Distribution

django_currentuser-0.9.0-py2.py3-none-any.whl (7.8 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django_currentuser-0.9.0.tar.gz.

File metadata

  • Download URL: django_currentuser-0.9.0.tar.gz
  • Upload date:
  • Size: 13.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for django_currentuser-0.9.0.tar.gz
Algorithm Hash digest
SHA256 3805fe2ef2a85456c6b791417e0bf782e62c8d8fa021c280d96624b1e942e504
MD5 1489d06f67806d7a7147579aebb44671
BLAKE2b-256 5b1ecf607b4c5474fa98e060c81dab74f3170691fc84d68e1366207cd43085c6

See more details on using hashes here.

File details

Details for the file django_currentuser-0.9.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_currentuser-0.9.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 df03a9eb7cb39983def680c1cd22e605f16adf8d315a06e706ddd97d5f7e2fbb
MD5 9f59875e5febfd80b62781f360b18c74
BLAKE2b-256 2ee27bc0aba295a76e5ffa30fa5244d69b7a03ac2dc358a8f74f2511741189da

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page