Giter VIP home page Giter VIP logo

django-pandora's Introduction

django-pandora

This is Pandora's box. Don't open it if you cannot live with the consequences.

django-pandora is a utility library for django that lets you easily store values in a thread-local. This makes it possible to introduce global variables that are thread safe and don't interfere with other requests to the server.

However a global state is a common anti-pattern in software development that you usually want to avoid. But at the end of the day it might be the easiest way to solve some of your problems.

The most common use case for django-pandora might be to store the current request in Pandora's box to make it available in places of your code that doesn't get the request passed in as argument.

Installation

  1. pip install django-pandora
  2. Add 'pandora.middleware.RequestMiddleware' to your MIDDLEWARE_CLASSES setting. (optional)

Usage

The pandora.box object implements a dict like interface which you can use to store thread local values. It's really as simple as using a dictionary:

>>> from pandora import box
>>> box['foo'] = 'Hello world!'
>>> box['foo']
'Hello world!'
>>> 'foo' in box
True
>>> 'bar' in box
False
>>> box['bar']
Traceback (most recent call last):
  ...
KeyError: 'bar'
>>> box.get('bar', None)
None

As mentioned above, you might want to use the box to store the current request object. Pandora ships with a custom middleware for this purpose. Just append 'pandora.middleware.RequestMiddleware' to your MIDDLEWARE_CLASSES setting. You can then access the request with box['request'].

There is also another middleware that stores the current user object in box['user']. To use this, add 'pandora.middleware.UserMiddleware' to your MIDDLEWARE_CLASSES setting, but make sure that it is listed after 'django.contrib.auth.middleware.AuthenticationMiddleware'.

Tips

  • You don't need to import the box object into your module if you think the name is too generic and might clash with some of your local names. Just use

    >>> import pandora
    >>> pandora.box['request']
    ...
    
  • Even if opening Pandora's box might not seem too evil at the first glance, please try to avoid it and pass the request object or any other dependency around where possible. Having a global state makes things more difficult for everyone - especially to reliably test your code.

  • Read the Wikipedia article about Pandora's box.

django-pandora's People

Contributors

gregmuellegger avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.