Giter Site home page Giter Site logo

fiee / django-userpreferences Goto Github PK

View Code? Open in Web Editor NEW

This project forked from narsil/django-userpreferences

4.0 2.0 1.0 100 KB

Store user preferences for other django apps (differences to original: less typos, i18n, no static files for Django <1.3)

Python 89.35% CSS 5.77% HTML 4.88%
python django preferences

django-userpreferences's Introduction

django-userpreferences (django-userpreferences)

Save arbitrary settings per user.

This pluggable Django app should integrate easily with other apps, also in existing projects.

Installation

Dependencies

Installing django-userpreferences

Install into your python path using pip:

pip install git+https://github.com/fiee/django-userpreferences.git

Add 'preferences' to your INSTALLED_APPS in settings.py:

INSTALLED_APPS = (
    ...
    'preferences',
)

Add '('preferences', include('preferences.urls'))' to your urls:

urlpatterns = [
    ....
    path('preferences', include('preferences.urls')),
]

Don't forget to run

./manage.py makemigrations preferences
./manage.py migrate

to create the preferences table.

Using django-userpreferences

Add a preferences.py file to your app test_app:

test_app/
 -- preferences.py
 -- models.py
 -- views.py

That looks like this:

PREFERENCES = (
    'mailing_period':(
        # ('Preference Display', 'value')
        ('Weekly', 'week'),  # first item is the default value
        ('Monthly', 'month'),
        ('Daily', 'day'),
    )
)

You can now access user preferences within your views:

>>> user.preferences['test_app']
{'mailing_period' : 'week'}

>>> user.preferences['test_app'] = { 'mailing_period' : 'month' }
>>> user.preferences.save()
>>> user.preferences['test_app']
{'mailing_period' : 'month'}

Note: Though it may have some properties of a dict, user.preferences is not a dict. It's a Model object; dict behaviour is a shortcut for user.preferences.preferences.

If you use the preferences urls, there’s an url to change preferences:

<a href="{% url preferences.views.change 'test_app' 'mailing_period' 'month' %}?return_url='/'>Receive monthly newsletter</a>

If the value in the database does not match any of the preferences in your preferences.py, the default value will be returned (this allows to disable preferences after people actually used them, without breaking your app).

Since we use pickle serialization, you can use only pickle-able settings. These include strings, integers, floats, booleans, tuples, lists, sets.

Only discrete sets of settings are allowed for now. Patches are welcome for preferences that accept user input.

Changing the default separator

django-userpreferences uses a separator between app name and preference name in forms. By default the separator is '/'. To override this, in the weird case you might be needing it in some variable name, you need to change it in your settings.py file:

PREFERENCES_SEPARATOR = '/'

Authors and License

Authors

License

GNU Lesser/Library Public License (LGPL)

django-picklefield is MIT-licensed. Django itself is BSD-licensed. Discuss.

django-userpreferences's People

Contributors

andrei-shabanski avatar fiee avatar gonzalodelgado avatar nattyg93 avatar senyor avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

gonzalodelgado

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.