Skip to main content

Posts

Showing posts from March, 2011

Django Pattern For Model Permissions

I think I've found an interesting way to set up a permissions system in Django.  This system allows you to specify granular permissions on specific object instances and is template friendly. Setup The Permissions Infrastructure First, you create an "abstract" Permissions class that models will use as a permissions base class.  This base permissions class sets up simple caching for the permissions and creates some common methods to be used by all permissions objects.  The current_user represents the logged in that you want to check permissions for a particular object. Notice the use of threadlocals here.  If current_user is not supplied in the constructor, the class will try to lookup the current_user from a variable by the threadlocals middleware (see below).  Be warned, however, some people don't seem to like the threadlocals approach .  To be honest, I'm still trying to evaluate the pluses and minuses, but it sure makes certain things in the templates easi

Django Environment Quick Setup

aptitude install python-virtualenv   # for debian-based systems virtualenv --no-site-packages myproject cd myproject/ source ./bin/activate pip install django  south  django-extensions pip install flup  psycopg2 mkdir -p ./etc/ ./var/log/ ./app/django/ cd ./app/django/ django-admin.py startproject mymain

Django Project Setup Conveniences

Whenever I start a new Django project, there are several common steps that I to help standardize my setups. In settings.py ... I define a PROJECT_ROOT variable which contains the path to the project directory.  This is useful for various settings in the setup.py: import sys, os PROJECT_ROOT = os.path.dirname(__file__) I set my MEDIA_ROOT to be a path relative to the PROJECT_ROOT.   Of course, I will need to create this directory on the filesystem.  MEDIA_ROOT = os.path.join(PROJECT_ROOT, '..','htdocs','media').replace('\\','/') + '/' And I set a relative TEMPLATE_DIRS path.  I will need to make this directory as well. TEMPLATE_DIRS = (     os.path.join(os.path.dirname(__file__), 'templates').replace('\\','/'), ) Sometimes I add extra directories to the path so I can store specific packages within the project directory. (I might use this if I want to keep all related modules together). sys.path

Introduction

This blog will document my adventures in Django, the Python web framework.  It is a direct fork of my Linux Info  blog, but will be specifically utilized for documenting my Django activity.  Though my aim is to post Django code and examples that are helpful, there will most certainly be better/different ways to approach various challenges.  Any comments are much appreciated. Joe