Package: raphendyr-django-essentials
Few essential additions for working with Django web framework.
In other words, stuff I have always copied to every project I have been working with.Setting managementModuleconfcontains tools to hwlp working with djangosettings.py.
Most important parts are includinglocal_settings.pyand creatingSECRET_KEYinsecret_key.py.
In addition, environment variables starting withDJANGO_will be included (parsed as json) to overwrite settings (useful for heroku for example).In end of yoursettings.pyadd following:fromr_django_essentials.confimportupdate_settingsupdate_settings(__name__)That is basically same as:fromr_django_essentials.confimport*# second argument is optional for all except firts. Default is shown here.update_settings_from_module(__name__,'local_settings')update_secret_from_file(__name__,'secret_key')update_settings_from_environment(__name__,'DJANGO_')update_installed_apps(__name__,'required_apps')update_context_processors_from_apps(__name__,'context_processors')use_cache_template_loader_in_production(__name__)Consult theconf.pyfor more details.Secret key autogenerationEvery installation of your application should always use it’s own randomly generatedSECRET_KEY.
To help with this task you can use this feature to automatically create new key if one is not provided by the settings.For this feature to work, yoursettings.pyneeds to do one of the following:# Include SECRET_KEY from secret_key.py into settingsfromr_django_essentials.confimportupdate_secret_from_fileupdate_secret_from_file(__name__,'secret_key')# update_settings will call above function as part of it's workfromr_django_essentials.confimportupdate_settingsupdate_settings(__name__)This will load the option if the file exists and will create new file if one doesn’t exist.Remember to addmyproject/secret_key.pyto.gitignoreas you shouldn’t ever haveSECRET_KEYin your version control.Django app dependenciesDjango doesn’t support depending from another Django app, but that is still sometimes really useful.
To keep power for the web app maintainer, don’t abuse this system my including complex django apps.
This feature is best used to include template and asset libraries (distributed ass django apps).For this feature to work, yoursettings.pyneeds to do one of the following:# Just expand the apps dependenciesfromr_django_essentials.confimportexpand_required_appsINSTALLED_APPS=expand_required_apps(INSTALLED_APPS)# Do same as above, but manipulate settings modulefromr_django_essentials.confimportupdate_installed_appsupdate_installed_apps(__name__)# update_settings will call above function as part of it's workfromr_django_essentials.confimportupdate_settingsupdate_settings(__name__)Now thatsettings.pyis calling correct functions, you need to add something like following to your appsAppConfig:# myapp/apps.pyfromdjango.appsimportAppConfigclassMyAppConfig(AppConfig):name='myapp'verbose_name='My example app'required_apps=['django.contrib.staticfiles','django.contrib.humanize',]Those apps will be added toINSTALLED_APPSby one of the above settings snippets.Required context processorsSimilar to app dependencies, this feature populates template enginescontext_processorslist fromAppConfig.For this feature to work, yoursettings.pyneeds to do one of the following:# Just update TEMPLATES list of dictionariesfromr_django_essentials.confimportadd_required_context_processorsadd_required_context_processors(TEMPLATES,INSTALLED_APPS)# Do same as above, but manipulate settings modulefromr_django_essentials.confimportupdate_context_processors_from_appsupdate_context_processors_from_apps(__name__)# update_settings will call above function as part of it's workfromr_django_essentials.confimportupdate_settingsupdate_settings(__name__)In your appsAppConfigyou would have something like this:# myapp/apps.pyfromdjango.appsimportAppConfigclassMyAppConfig(AppConfig):name='myapp'verbose_name='My example app'context_processors='myapp.context_processors.myapp_context_processor'# orcontext_processors=('myapp.context_processors.myapp_context_processor1','myapp.context_processors.myapp_context_processor2',)Above will add context processors into django template engines options.
If you need to add context processors for different backend,
then use dictionary with backend as a key and list of processors as value.Deprecated warningNeed to mark function deprecated without good deprecation system in place?fromr_django_essentials.deprecationimportdeprecated@deprecated("my_function is deprecated, use my_new_function instead")defmy_function(argument):returnmy_new_function(argument)Colorized log formatterYou would like to color code different log sources with different colors?In yoursettings.py:OGGING={'version':1,'disable_existing_loggers':False,'formatters':{'colored':{'()':'r_django_essentials.logging.SourceColorizeFormatter','format':'[%(asctime)s:%(levelname)8s%(name)s]%(message)s','colors':{'django.db.backends':{'fg':'cyan'},'myapp':{'fg':'red'},},},},'handlers':{'debug_console':{'level':'DEBUG','class':'logging.StreamHandler','stream':'ext://sys.stdout','formatter':'colored',},},'loggers':{'':{'level':'DEBUG','handlers':['debug_console'],'propagate':True},},}Enum for choices in modelsEasily create enumerations that works well with choices field in django.In yourmodels.py:fromdjango.dbimportmodelsfromdjango.utils.translationimportugettext_lazyas_fromr_django_essentials.fieldsimportEnumclassMyProcess(models.Model):STATUS=Enum(('OK',0,_('Process is ok')),('PROBLEM',1,_('Process has problem')),('ERROR',2,_('Process is in error state')),)status=models.PositiveSmallIntegerField(default=STATUS.OK,choices=STATUS.choices,verbose_name=_("Process status"),)@propertydefstatus_text(self):returnself.STATUS[self.status]