Skip to content

Instantly share code, notes, and snippets.

@easydevmixin
Created June 22, 2015 10:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save easydevmixin/d0571e7f7a822ff88c98 to your computer and use it in GitHub Desktop.
Save easydevmixin/d0571e7f7a822ff88c98 to your computer and use it in GitHub Desktop.
Django Project Layout
# Python compiled classes
__pycache__/
*.pyc
# SQLite databases
*.sqlite
*.sqlite3
*.db
# Project specific
src/static/
src/media/
# Unit test and coverage
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover
# Translations
*.mo
*.pot
# Mac OS
.DS_*
# Editors and IDEs (for PyCharm, SublimeText and Netbeans)
.idea/
*.sublime-*
nbproject/private/
build/
nbbuild/
dist/
nbdist/
nbactions.xml
nb-configuration.xml
.nb-gradle/
# Others
*.~
# -*- coding: utf-8 -*-
# This is an __init__.py example file placed into the settings directory
# It will try to import the module local.py and if not found it will
# exit with an error message.
import sys
try:
from .local import *
except ImportError:
print('local.py file does not exist.')
print('Go to settings directory and copy local.py.example to local.py')
sys.exit(-1)
"""
Django settings for easydevmixin project.
Generated by 'django-admin startproject' using Django 1.8.2.
For more information on this file, see
https://docs.djangoproject.com/en/1.8/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.8/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import json
import os
import sys
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
ADMINS = (
('Ramon Maria Gallart', 'rgallart@easydevmixin.com'),
)
MANAGERS = ADMINS
def get_secret(the_secret, debugging):
"""
As a security measure, all secrets will be kept in a json file named
secrets.json. This file will not be managed by the version control
system, but will be available in our documentation repository or
as an attached file. The main goal of this is that this file should
not be viewable by no one except us or our team.
"""
try:
secrets_file = os.path.join(BASE_DIR, 'settings', 'secrets.json')
myjson = json.load(open(secrets_file))
if debugging:
return myjson['development'][the_secret]
else:
return myjson['production'][the_secret]
except Exception as e:
print("Something weird happened while retrieving a secret: {}".format(e))
sys.exit(-1)
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/
# Application definition
INSTALLED_APPS = (
# DJANGO APPS
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# 3RD-PARTY APPS. PLACE HERE EXTERNAL LIBRARIES
# PROJECT APPS. PLACE HERE THE APPS CREATED
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
ROOT_URLCONF = 'easydevmixin.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'easydevmixin.wsgi.application'
# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
MEDIA_URL = "/media/"
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
##########################################################
# Set up logging directory
##########################################################
# set the directory where you want to write log files
# this dir will be created if it does not exist
# Django will die if it cannot be created
# This is an example of an absolute path:
# logsdir = os.path.realpath('/home/vagrant/logs')
# This uses a dir 'logs' in your home directory (/home/vagrant in the example)
logsdir = os.path.realpath(os.path.join(os.getenv('HOME'), 'logs'))
try:
os.stat(logsdir)
except OSError:
print("mkdir %s..." % logsdir)
try:
os.mkdir(logsdir)
except OSError as e:
print("OSError({0}): {1}".format(e.errno, e.strerror))
print("Could not mkdir %s" % logsdir)
sys.exit(1)
from .base import * # We import everything we have previously set up on base.py
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
# Notice here the use of the function get_secret to get information
# from the secrets.json file which shouldn't be on our VCS
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': get_secret('DB_NAME', DEBUG),
'USER': get_secret('DB_USER', DEBUG),
'PASSWORD': get_secret('DB_PASSWORD', DEBUG),
}
}
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = get_secret('SECRET_KEY', DEBUG)
#########################################################
# Activate django-debug-toolbar if it is installed
#########################################################
try:
import debug_toolbar
MIDDLEWARE_CLASSES += ('debug_toolbar.middleware.DebugToolbarMiddleware',)
INSTALLED_APPS += ('debug_toolbar', )
DEBUG_TOOLBAR_CONFIG = {
'SHOW_TOOLBAR_CALLBACK': 'easydevmixin.settings.debug_toolbar_stuff.show_toolbar',
}
except ImportError:
pass
#########################################################
# Activate django-extensions if exist
#########################################################
try:
import django_extensions
INSTALLED_APPS += ('django_extensions', )
except ImportError:
pass
# LOGGING. An example of how to set up a basic logging facility
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'verbose': {
'format': "[%(asctime)s] [%(levelname)s] [%(name)s] [%(lineno)s] %(message)s",
'datefmt': "%d/%m/%Y %H:%M:%S"
},
'simple': {
'format': '%(levelname)s %(message)s'
},
},
'handlers': {
'mail_admins': {
'level': 'ERROR',
'class': 'django.utils.log.AdminEmailHandler'
},
'console': {
'class': 'logging.StreamHandler',
'formatter': 'simple',
},
'example_rotating_file': {
'class': 'logging.handlers.RotatingFileHandler',
'formatter': 'verbose',
'filename': os.path.join(logsdir, 'assets.log'),
'maxBytes': 1024 * 1024 * 10,
'backupCount': 10,
},
},
'loggers': {
'django.request': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': True,
},
'example': {
'handlers': ['example_rotating_file'],
'level': 'DEBUG',
},
}
}
from .development import *
from .base import *
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
TEMPLATE_DEBUG = False
# CHANGE THE ALLOWED_HOSTS LIST TO FIT YOUR NEEDS
ALLOWED_HOSTS = ['www.easydevmixin.com', 'www.easydevmixin.cat']
# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': get_secret('DB_NAME', DEBUG),
'USER': get_secret('DB_USER', DEBUG),
'PASSWORD': get_secret('DB_PASSWORD', DEBUG),
}
}
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = get_secret('SECRET_KEY', DEBUG)
# LOGGING
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'verbose': {
'format': "[%(asctime)s] [%(levelname)s] [%(name)s] [%(lineno)s] %(message)s",
'datefmt': "%d/%m/%Y %H:%M:%S"
},
'simple': {
'format': '%(levelname)s %(message)s'
},
},
'handlers': {
'mail_admins': {
'level': 'ERROR',
'class': 'django.utils.log.AdminEmailHandler'
},
'console': {
'class': 'logging.StreamHandler',
'formatter': 'simple',
},
'assets_rotating_file': {
'class': 'logging.handlers.RotatingFileHandler',
'formatter': 'verbose',
'filename': os.path.join(logsdir, 'assets.log'),
'maxBytes': 1024 * 1024 * 10,
'backupCount': 10,
},
'template_loader_rotating_file': {
'class': 'logging.handlers.RotatingFileHandler',
'formatter': 'verbose',
'filename': os.path.join(logsdir, 'template_loader.log'),
'maxBytes': 1024 * 1024 * 10,
'backupCount': 10,
},
},
'loggers': {
'django.request': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': True,
},
'assets': {
'handlers': ['assets_rotating_file'],
'level': 'INFO',
},
'template_loader': {
'handlers': ['template_loader_rotating_file'],
'level': 'INFO',
},
}
}
{
"development": {
"SECRET_KEY": "the-development-django-secret-key",
"DB_NAME": "the-development-database-name",
"DB_USER": "the-development-database-username",
"DB_PASSWORD": "the-development-database-password",
},
"production": {
"SECRET_KEY": "the-production-django-secret-key",
"DB_NAME": "the-production-database-name",
"DB_USER": "the-production-database-username",
"DB_PASSWORD": "the-production-database-password",
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment