Everybody know about long spagetty-style settings file for django :-)
I tried to find any cool settings loader, but have no luck.I created this one myself.
Ok, we forgetting about `settings.py` and creating module settings (folder named settings with file `__init__.py`).
This `__init__.py` file have preloader for modules placed in settings folder and `../settings_local.py` (if exists) at the end. settings_local is awesome tool, when you use any VCS like git and have settings in vcs, but for example you have different database connection settings. You can change this settings in settings_local.
Settings splitter have variable moduleweights. This variable declares weights for selected modules to allow loader sort modules by priority and use already defined settings in each other loaded module. You can define your custom modules and weights there.
Ok, now few examples.
settings/env.py
import os
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/
DEBUG = not 'http/ask.helldude.ru/' in os.path.realpath(__file__)
settings/assets.py
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/
import os
import sys
settings = sys.modules['project.settings']
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(settings.BASE_DIR, 'static')
STATICFILES_DIRS = (os.path.join(settings.BASE_DIR, "project/static"),)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'compressor.finders.CompressorFinder',
)
settings_local.py:
import sys
settings = sys.modules['project.settings']
print 'I WAS LOADED KHA KHA KHA'
if settings.DEBUG:
print 'In debug mode'
You can see amazing 'hack' there :-)
I use already defined settings via sys.modules['project.settings'] (where project is common folder for my project applications).
I hope you like this small lifehack for django settings!
rudude.
I'm using Django's FlatPages, but I want to be able to restrict admin access to Users based on a FlatPage url. For example, User John Doe should be able to edit any FlatPage objects whose URL begins with `/johndoe/` (such as `/johndoe/about/` or `/johndoe/projects/whatever/`).
For this to work, John Doe would already need the appropriate admin permissions for FlatPage (such as can_add and can_change).
I have set this up as a separate *flatpage_addons* app. It consists of the **Permission** model, which maps a starting URL to one or more django Users. It consists of the minimal necessary admin code so Permissions can be created using the admin.
The bulk of this code consists of the *ifhasflatpagepermission* template tag as well as the *flatpage_result_list* inclusion tag. The former works much like django's existing *if*, *else*, *endif* tags while the latter is modified from the django admin's *result_list* inclusion tag.
This may not be the most elegant solution to my problem, but so far it works for me. Any comments or suggestions are welcome!
- urls
- url
- permission
- permissions
- flatpage
- flatpages