Beispiel #1
0
def default_env(
    module_name,
    domain_name,
    admin_email,
    site_name=None,
    db_name=None,
    test=None,
    debug=None,
    debug_toolbar=False,
    high_res_images=False,
    image_credits=False,
    pull_host=None,
    pull_user=None,
    pull_sudo=None,
    pull_db_name=None,
    pull_shell=None,
    csrf=False,
    ssl=False,
    frontend_editing=False,
    email_file_log=False):
    """
    Setup default configuration, where module_name is the settings module that
    is calling this function, domain_name the name of the website or project.

    The default project configuration setup a PostgreSQL database with a name
    that is equal to the name of the project (e.g. domain_name). The base path
    is determined by the absolute path of the module that is named module_name.

    In order to pre-configure your project, add the following two lines to your
    settings.py file:

    from lib.settings import default_env
    env = default_env(__name__, 'test.co.uk')
    """
    # get settings module and normalise site name
    m = get_module_by_name(module_name)
    domain_name = domain_name.strip().lower()


    #
    # Determine if we are running under TEST
    #
    m.TEST = test if test is not None else 'test' in sys.argv


    # determine debug or production mode.
    # default is production mode unless we find the environment variable
    # DEV_MODE to be set to 1, in which case we assume DEBUG mode.
    m.DEBUG = debug if debug is not None else os.environ.get('DEV_MODE', '0') == '1' and not m.TEST
    m.MINIFY_RESOURCES = not m.DEBUG and not m.TEST


    #
    # Site
    #
    m.SITE_ID = 1
    m.DOMAIN_NAME = domain_name
    m.ALLOWED_HOSTS = ['*']
    m.TRACK_REVISION = True
    m.GENERATE_MINIFY_SRC = False
    m.DEBUG_DOMAIN_NAME = None


    #
    # Site name
    #
    m.CUBANE_SITE_NAME = site_name


    #
    # Database name
    #
    if db_name:
        m.DATABASE_NAME = db_name
    else:
        m.DATABASE_NAME = domain_name.replace('.', '_').replace('-', '_')


    #
    # Default path
    #
    m.CUBANE_PATH = os.path.realpath(
        os.path.join(
            os.path.dirname(__file__),
            '../'
        )
    )
    m.BASE_PATH = os.path.realpath(
        os.path.join(
            os.path.dirname(m.__file__),
            '../'
        )
    )


    #
    # Resource compressors
    #
    if m.TEST:
        # we use yuicompressor in test, because it is much faster
        m.MINIFY_CMD_JS  = 'java -jar %s --type js' % os.path.join(m.CUBANE_PATH, 'bin', 'yui', 'yuicompressor-2.4.8.jar')
        m.MINIFY_CMD_CSS = 'java -jar %s --type css' % os.path.join(m.CUBANE_PATH, 'bin', 'yui', 'yuicompressor-2.4.8.jar')
    else:
        m.MINIFY_CMD_JS  = 'java -jar %s' % os.path.join(m.CUBANE_PATH, 'bin', 'closure-compiler', 'compiler.jar')
        m.MINIFY_CMD_CSS = 'java -jar %s --type css' % os.path.join(m.CUBANE_PATH, 'bin', 'yui', 'yuicompressor-2.4.8.jar')


    #
    # Dashboard
    #
    m.CUBANE_DASHBOARD = False


    #
    # Default Operations
    #
    m.CUBANE_LISTING_DEFAULT_CLEAN = True
    m.CUBANE_LISTING_DEFAULT_MERGE = True


    #
    # Default CMS settings
    #
    m.CMS_RENDER_SLOT_CONTAINER = False
    m.CMS_TEMPLATES = (
        ('cubane/cms/default_template.html', 'Default Template'),
    )
    m.CMS_NAVIGATION = (
        ('header', 'Header'),
        ('footer', 'Footer'),
    )
    m.CMS_NAVIGATION_INCLUDE_CHILD_PAGES = False
    m.CMS_EXCERPT_LENGTH = 60
    m.CMS_NO_AUTO_EXCERPT = False
    m.CMS_SLOTNAMES = ['content']
    m.CMS_NAVIGATION_RELATED_FIELDS = ['image']
    m.CMS_TEST_SPF = True
    m.CMS_SOFTFAIL_SPF = False
    m.CMS_ADV_EDITOR_PLUGINS = False
    m.CMS_DEFAULT_SLOTNAME = 'content'
    m.CMS_BACKEND_SITEMAP = False
    m.CMS_META_TITLE_SEPARATOR = ' | '


    #
    # Default Google Analytics key in debug mode
    #
    m.DEBUG_GOOGLE_ANALYTICS = None


    #
    # Admin and managers
    #
    m.ADMINS = m.MANAGERS = (
        ('admin', admin_email),
    )

    m.INTERNAL_IPS = (
        '127.0.0.1',
    )


    #
    # Default session serializer (Extended JSON)
    #
    m.SESSION_SERIALIZER = 'cubane.serializers.ExtendedJSONSerializer'

    #
    # Backend permissions and ACLs
    #
    m.CUBANE_BACKEND_PERMISSIONS = False
    m.CUBANE_BACKEND_ACL = {}
    m.CUBANE_BACKEND_DEFAULT_ACL = {
        'create': Acl.ALL,
        'read': Acl.ALL,
        'update': Acl.ALL,
        'delete': Acl.ALL,
        'data_import': Acl.ALL,
        'data_export': Acl.ALL,
        'merge': Acl.ALL
    }


    #
    # Changelog entry lifespan
    #
    m.CUBANE_BACKEND_CHANGELOG_EXPIRY_DAYS = 180


    #
    # Cubane backend editor
    #
    m.CUBANE_BACKEND_EDITOR_PLUGINS = [
        '-advlist',
        '-autolink',
        '-charmap',
        '-print',
        '-searchreplace',
        '-visualblocks',
        '-code',
        '-fullscreen',
        '-insertdatetime',
        '-table',
        '-paste',
        '-nonbreaking',
        '-lists',
        '-cubanepreview',
        '-cubanelink'
    ]
    m.CUBANE_BACKEND_EDITOR_YOUTUBE = True


    #
    # Depreceated: Default backend presentation
    #
    m.BACKEND_THEME = {
        'product_name': 'inystem',
        'hide_innershed_logo': False,
        'color': {
            'r': 232,
            'g': 48,
            'b': 138
        }
    }


    #
    # Default backend button labels
    #
    m.CUBANE_BACKEND_DEFAULT_SAVE_BUTTON_LABEL = 'Save <small>And Close</small>'
    m.CUBANE_BACKEND_DEFAULT_SAVE_AND_CONTINUE_BUTTON_LABEL = 'Save <small>And Continue</small>'
    m.CUBANE_BACKEND_DEFAULT_BACK_BUTTON_LABEL = 'Back'


    #
    # Media options
    #
    m.CUBANE_BACKEND_MEDIA = True
    m.CUBANE_HASHED_MEDIA_URLS = False
    m.CUBANE_DEFAULT_MEDIA_PREVIEW_EXT = '.jpg'
    m.CUBANE_MEDIA_VERSIONS = True


    #
    # Video
    #
    m.CUBANE_VIDEO_TYPES = [
        'youtube'
    ]

    #
    # Timezone, Language and Formatting
    #
    m.TIME_ZONE = 'Europe/London'
    m.LANGUAGE_CODE = 'en-GB'
    m.USE_I18N = False
    m.DATE_FORMAT = 'd/m/Y'
    m.TIME_FORMAT = 'P'
    m.DATETIME_FORMAT = 'd/m/Y H:i'
    m.STR_DATE_FORMAT = '%d_%m_%Y'
    m.DATE_INPUT_FORMATS = (
        '%Y-%m-%d',   # 2006-10-25s
        '%d/%m/%Y',   # 25/10/2006
        '%d/%m/%y',   # 25/10/06
        '%d-%b-%y',   # 30-Dec-06
    )
    m.TIME_INPUT_FORMATS = (
        '%H:%M:%S',    # 14:30:55
        '%H:%M:%S.%f', # 14:30:55.123
        '%H:%M',       # 14:30
        '%I:%M %p'     # 2:30 PM
    )
    m.DATETIME_INPUT_FORMATS = (
        '%Y-%m-%dT%H:%M',        # '2010-10-10T10:15'
        '%d/%m/%Y %H:%M:%S',     # '25/10/2006 14:30:59'
        '%d/%m/%Y %H:%M:%S.%f',  # '25/10/2006 14:30:59.000200'
        '%d/%m/%Y %H:%M',        # '25/10/2006 14:30'
        '%d/%m/%Y',              # '25/10/2006'
        '%d/%m/%y %H:%M:%S',     # '25/10/06 14:30:59'
        '%d/%m/%y %H:%M:%S.%f',  # '25/10/06 14:30:59.000200'
        '%d/%m/%y %H:%M',        # '25/10/06 14:30'
        '%d/%m/%y',              # '25/10/06'
    )


    #
    # Default database setup
    #
    m.DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'NAME': m.DATABASE_NAME
        }
    }


    #
    # Speed up unit testing by switching database engine to sqlite3, unless
    # the env. specifies to run a full test and not speeding up unit testing.
    #
    m.TEST_FULL = False
    if m.TEST: # pragma: no cover
        m.TEST_FULL = os.environ.get('DEV_TEST_FULL', '0') == '1'
        if not m.TEST_FULL:
            m.DATABASES['default']['ENGINE'] = 'django.db.backends.sqlite3'


    #
    # Turn off django migrations if we are running under test.
    # See: http://stackoverflow.com/questions/25161425/disable-migrations-when-running-unit-tests-in-django-1-7
    #
    if m.TEST:
        m.MIGRATION_MODULES = {'myapp': None}


    #
    # Static and cache
    #
    m.CACHE_ENABLED = True
    m.CACHE_PUBLISH_ENABLED = True
    m.STATIC_URL = '/static/'
    m.PUBLIC_HTML_ROOT = os.path.abspath(os.path.join(m.BASE_PATH, '..', '..', 'public_html'))
    m.CACHE_ROOT = os.path.join(m.PUBLIC_HTML_ROOT, 'cache')
    m.STATIC_ROOT = os.path.join(m.PUBLIC_HTML_ROOT, 'static')
    m.STATICFILES_DIRS = (
        os.path.join(m.BASE_PATH, 'static'),
    )


    #
    # Media
    #
    if m.DEBUG or m.TEST:
        m.MEDIA_ROOT = os.path.abspath(os.path.join(m.BASE_PATH, 'media'))
    else:
        m.MEDIA_ROOT = os.path.abspath(os.path.join(m.BASE_PATH, '..', '..', 'public_html', 'media'))
    m.MEDIA_URL = 'media/'
    m.MEDIA_API_URL = 'media-api/'


    #
    # Media Download Url
    #
    m.MEDIA_DOWNLOAD_URL = 'download/'


    #
    # Fonts
    #
    m.CUBANE_FONT_ROOT = os.path.join(m.MEDIA_ROOT, 'fonts')
    m.CUBANE_FONT_BACKENDS = [
        'cubane.fonts.backends.GoogleFontsBackend',
    ]


    #
    # Responsive image sizes
    #
    m.DEFAULT_IMAGE_SIZE = 'x-large'
    m.CMS_EDITOR_DEFAULT_IMAGE_SIZE = 'x-large'
    m.IMAGE_SIZES = {
        'xx-small':  50,
        'x-small':  160,
        'small':    320,
        'medium':   640,
        'large':    900,
        'x-large': 1200
    }
    if high_res_images:
        m.DEFAULT_IMAGE_SIZE = 'xxx-large'
        m.CMS_EDITOR_DEFAULT_IMAGE_SIZE = 'xxx-large'
        m.IMAGE_SIZES.update({
            'xx-large':  1600,
            'xxx-large': 2400
        })

    m.DISABLE_DEVICE_RATIO = False


    #
    # Automatic image fitting
    #
    m.IMAGE_FITTING_ENABLED = False
    m.IMAGE_FITTING_SHAPES = []
    m.IMAGE_FITTING_COLOR   = 'white'


    #
    # Convert PNG files without transparency to JPG files
    #
    m.IMAGE_CONVERT_PNG_TO_JPG = True


    #
    # The default image compression for jpeg images
    #
    m.IMAGE_COMPRESSION_QUALITY = 82


    #
    # Image Optimization
    #
    m.IMAGE_OPTIMIZE = True
    m.IMAGE_JPEG_OPT_COMMAND = 'jpegtran -optimize -progressive -copy none -outfile %(dest)s %(source)s'
    m.IMAGE_PNG_OPT_COMMAND = 'optipng -o 0 -quiet -out %(dest)s %(source)s'


    #
    # Image PDF preview generation
    #
    m.IMAGE_PDF_PREVIEW_COMMAND = 'gs -sDEVICE=jpeg -o %(dest)s -dFirstPage=1 -dLastPage=1 -dJPEGQ=84 -r120 %(source)s'


    #
    # Maximal width considered to be saved as an original on the server
    #
    m.IMG_MAX_WIDTH = 2400


    #
    # Default image shape and image generator settings
    #
    m.DEFAULT_IMAGE_SHAPE = 'original'
    m.IMAGE_SHAPES = {}
    m.IMAGE_SHAPE_NAMES = {}
    m.IMAGE_ART_DIRECTION = {}


    #
    # Additional image information
    #
    m.IMAGE_CREDITS = image_credits
    m.IMAGE_EXTRA_TITLE = False
    m.IMAGE_CAPTION_LABEL = 'Caption'
    m.IMAGE_CAPTION_HELP_TEXT = 'Leave empty to fill automatically; Briefly describe the content of the image or document. This information is associated with the image or document and is analysed by search engines.'
    m.IMAGE_EXTRA_TITLE_LABEL = 'Description'
    m.IMAGE_EXTRA_TITLE_HELP_TEXT = 'If you wish to add an extra description to the image or document.'


    #
    # Devdeloper Information
    #
    m.CUBANE_PROVIDED_BY = {}


    #
    # Site Notification
    #
    m.CUBANE_SITE_NOTIFICATION = False


    #
    # Client Logo
    #
    m.CLIENT_LOGO = 'client-logo.png'


    #
    # Developer Logo
    #
    m.DEVELOPER_LOGO = 'developer-logo.png'


    #
    # Favicon Sizes used to generate the various favicons for different browsers
    #
    m.FAVICON_PATH = 'img'
    m.FAVICON_FILENAME = 'favicon.png'


    m.FAVICON_PNG_SIZES = [
        {'size': '16x16',   'filename': 'favicon-16x16.png'},
        {'size': '24x24',   'filename': 'favicon-24x24.png'},
        {'size': '32x32',   'filename': 'favicon-32x32.png'},
        {'size': '48x48',   'filename': 'favicon-48x48.png'},
        {'size': '57x57',   'filename': 'favicon-57x57.png'},
        {'size': '60x60',   'filename': 'favicon-60x60.png'},
        {'size': '64x64',   'filename': 'favicon-64x64.png'},
        {'size': '70x70',   'filename': 'favicon-70x70.png'},
        {'size': '72x72',   'filename': 'favicon-72x72.png'},
        {'size': '76x76',   'filename': 'favicon-76x76.png'},
        {'size': '96x96',   'filename': 'favicon-96x96.png'},
        {'size': '114x114', 'filename': 'favicon-114x114.png'},
        {'size': '120x120', 'filename': 'favicon-120x120.png'},
        {'size': '128x128', 'filename': 'favicon-128x128.png'},
        {'size': '144x144', 'filename': 'favicon-144x144.png'},
        {'size': '150x150', 'filename': 'favicon-150x150.png'},
        {'size': '152x152', 'filename': 'favicon-152x152.png'},
        {'size': '196x196', 'filename': 'favicon-196x196.png'},
        {'size': '310x150', 'filename': 'favicon-310x150.png'},
        {'size': '310x310', 'filename': 'favicon-310x310.png'}
    ]


    m.FAVICON_ICO_SIZES = [
        {'size': '16x16', 'filename': 'favicon-16x16.png'},
        {'size': '24x24', 'filename': 'favicon-24x24.png'},
        {'size': '32x32', 'filename': 'favicon-32x32.png'},
        {'size': '48x48', 'filename': 'favicon-48x48.png'},
        {'size': '64x64', 'filename': 'favicon-64x64.png'},
        {'size': '128x128', 'filename': 'favicon-128x128.png'}
    ]


    #
    # Default middleware
    #
    m.MIDDLEWARE_CLASSES = (
        'django.middleware.common.CommonMiddleware',
        'django.contrib.sessions.middleware.SessionMiddleware',
        'django.contrib.messages.middleware.MessageMiddleware',
        'django.contrib.auth.middleware.AuthenticationMiddleware',
        'cubane.middleware.SettingsMiddleware'
    )


    #
    # CSRF
    #
    if csrf:
        m.MIDDLEWARE_CLASSES = (
            'django.middleware.csrf.CsrfViewMiddleware',
        ) + m.MIDDLEWARE_CLASSES


    #
    # Templates
    #
    m.TEMPLATES = get_default_templates(m.BASE_PATH, m.DEBUG)


    #
    # Default apps
    #
    m.INSTALLED_APPS = (
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.sitemaps',
        'django.contrib.staticfiles'
    )


    #
    # Frontend editing
    #
    m.CUBANE_FRONTEND_EDITING = frontend_editing
    if frontend_editing:
        m.MIDDLEWARE_CLASSES += (
            'cubane.middleware.FrontendEditingMiddleware',
        )
        m.INSTALLED_APPS += (
            'cubane.frontend',
            'cubane.frontendloader',
        )


    #
    # Default locale
    #
    m.CUBANE_LOCALE = b'en_GB.UTF-8'


    #
    # Captcha
    #
    m.CAPTCHA = None
    m.RECAPTCHA_PUBLIC_KEY = ''
    m.RECAPTCHA_PRIVATE_KEY = ''
    m.CAPTCHA_PLACEHOLDER = ''


    #
    # Google Map API Key
    #
    m.CUBANE_GOOGLE_MAP_API_KEY = ''


    #
    # Google analytics
    #
    m.CUBANE_GOOGLE_ANALYTICS_ASYNC = False


    #
    # Mail
    #
    m.EMAIL_SUBJECT_PREFIX = '[%s] ' % domain_name
    if m.DEBUG:
        if email_file_log:
            m.EMAIL_BACKEND = 'cubane.backends.EmailEmlFileBackend'
            m.EMAIL_FILE_PATH = os.path.join(m.BASE_PATH, 'log')
        else:
            m.EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
    if m.TEST:
        m.EMAIL_BACKEND = 'django.core.mail.backends.locmem.EmailBackend'


    #
    # Login and Authentication
    #
    m.AUTHENTICATION_BACKENDS = (
        'django.contrib.auth.backends.ModelBackend',
        'cubane.backends.EmailAuthBackend',
    )


    #
    # Urls
    #
    m.APPEND_SLASH = True
    m.PREPEND_WWW = not m.DEBUG


    #
    # SSL - Determines whether the website should run ssl or not. This will
    # affect page redirects (which would be https:// rather than http:// but
    # will not neccessarily redirect from http:// to https:// automatically).
    #
    m.SSL = ssl
    if ssl:
        m.MIDDLEWARE_CLASSES += ('cubane.middleware.SSLResponseRedirectMiddleware',)


    #
    # Default logging setup
    #
    m.LOGGING = {
        'version': 1,
        'disable_existing_loggers': False,
        'filters': {
            'require_debug_false': {
                '()': 'django.utils.log.RequireDebugFalse'
            }
        },
        'handlers': {
            'mail_admins': {
                'level': 'ERROR',
                'filters': ['require_debug_false'],
                'class': 'django.utils.log.AdminEmailHandler'
            }
        },
        'loggers': {
            'django.request': {
                'handlers': ['mail_admins'],
                'level': 'ERROR',
                'propagate': True,
            },
        }
    }


    #
    # Default file upload handlers
    #
    m.FILE_UPLOAD_HANDLERS = (
        'django.core.files.uploadhandler.TemporaryFileUploadHandler',
    )


    #
    # Default resource targets
    #
    m.RESOURCES = {}
    m.CSS_MEDIA = ['screen', 'print']

    #
    # Default Require.js entry files
    # Array of config dict: [{'filepath':s, 'baseurl':s, 'module':s},...]
    # filepath: to id the file that needs to be compiled to a single file
    # baseURL: root directory to every module search (optional)
    # module: name of the module e.g. main (from main.js) (optional)
    #
    m.REQUIRE_JS = []


    #
    # Default configuration for backend listings
    #
    m.MAX_LISTING_COLUMNS = 6


    #
    # Default location for google map (backend)
    #
    m.DEFAULT_MAP_LOCATION = [52.6370209, 1.2996577]


    #
    # Default configuration for paginator
    #
    m.DEFAULT_PAGE_SIZE     = 10
    m.DEFAULT_MIN_PAGE_SIZE = 10
    m.DEFAULT_MAX_PAGE_SIZE = 100


    #
    # Shop defaults
    #
    m.SHOP_PREAUTH = False
    m.SHOP_CUSTOM_PROPERTIES = {}
    m.SHOP_BASKET_SESSION_VAR = 'ishop_basket'
    m.SHOP_BASKET_ALLOWED_PREFIX = ['ishop_basket']
    m.SHOP_BASKET_BACKEND_PREFIX = 'ishop_backend'
    m.SHOP_DEFAULT_DELIVERY_COUNTRY_ISO = 'GB'
    m.TRACKING_PROVIDERS = []
    m.SHOP_LOCALE = b'en_GB.UTF-8'
    m.SHOP_ENABLE_KIT_BUILDER = False
    m.SHOP_VARIETY_FILTER_ENABLED = True
    m.SHOP_MULTIPLE_CATEGORIES = False
    m.SHOP_CHANGE_CUSTOMER_PASSWORD_ENABLED = True
    m.SHOP_LOAD_VARIETY_PREVIEW = True


    #
    # Payment Gateways
    #
    m.GATEWAY_TEST      = 0
    m.GATEWAY_SAGEPAY   = 1
    m.GATEWAY_PAYPAL    = 2
    m.GATEWAY_STRIPE    = 3
    m.GATEWAY_DEKO      = 4
    m.GATEWAY_OMNIPORT  = 5
    m.GATEWAY_CHOICES = (
        (m.GATEWAY_TEST, 'Test Gateway'),
        (m.GATEWAY_SAGEPAY, 'Sagepay'),
        (m.GATEWAY_PAYPAL, 'PayPal'),
        (m.GATEWAY_STRIPE, 'Stripe'),
        (m.GATEWAY_DEKO, 'Deko'),
        (m.GATEWAY_OMNIPORT, 'OmniPort')
    )


    #
    # Default Payment gateways used
    #
    m.SHOP_LOAN_ENABLED = False
    m.SHOP_DEFAULT_PAYMENT_GATEWAY = m.GATEWAY_SAGEPAY
    m.SHOP_LOAN_PAYMENT_GATEWAY = m.GATEWAY_DEKO
    m.SHOP_PAYMENT_CONFIG = {}
    m.SHOP_TEST_MODE = m.DEBUG


    #
    # Allow pages to use hierarchy
    #
    m.PAGE_HIERARCHY = False


    #
    # Postcode Lookup
    #

    m.POSTCODE_GETADDRESS = 0
    m.POSTCODE_PROVIDERS = (
        (m.POSTCODE_GETADDRESS, 'GetAddress.io')
    )
    m.DEFAULT_POSTCODE_PROVIDER = m.POSTCODE_GETADDRESS
    m.POSTCODE_PROVIDER = m.DEFAULT_POSTCODE_PROVIDER
    m.POSTCODE_DEBUG = False


    #
    # Status Code handlers
    #
    m.HANDLER_404 = 'cubane.default_views.custom404'
    m.HANDLER_500 = 'cubane.default_views.custom500'


    # Pull from production server
    m.CUBANE_PULL = {
        'DATABASE': pull_db_name if pull_db_name else m.DATABASE_NAME,
        'HOST': pull_host if pull_host else m.DOMAIN_NAME,
        'USER': pull_user if pull_user else 'root',
        'DBDUMP': 'pg_dump',
        'SUDO': pull_sudo if pull_sudo else m.DOMAIN_NAME if not pull_user else None,
        'SHELL': pull_shell if pull_shell else 'sh',
        'SUDO_DEFINED': pull_user is None
    }


    #
    # Debug Toolbar (Debug only)
    #
    m.DEBUG_TOOLBAR = debug_toolbar
    if m.DEBUG and debug_toolbar:
        m.INSTALLED_APPS = m.INSTALLED_APPS + ('debug_toolbar',)
        m.MIDDLEWARE_CLASSES = ('debug_toolbar.middleware.DebugToolbarMiddleware',) + m.MIDDLEWARE_CLASSES
        m.DEBUG_TOOLBAR_PANELS = [
            'debug_toolbar.panels.versions.VersionsPanel',
            'debug_toolbar.panels.timer.TimerPanel',
            'debug_toolbar.panels.settings.SettingsPanel',
            'debug_toolbar.panels.headers.HeadersPanel',
            'debug_toolbar.panels.request.RequestPanel',
            'debug_toolbar.panels.sql.SQLPanel',
            'debug_toolbar.panels.staticfiles.StaticFilesPanel',
            'debug_toolbar.panels.cache.CachePanel',
            'debug_toolbar.panels.signals.SignalsPanel',
            'debug_toolbar.panels.logging.LoggingPanel',
            'debug_toolbar.panels.redirects.RedirectsPanel',
            #'debug_toolbar.panels.templates.TemplatesPanel'
        ]


    return SettingWrapper(m)
Beispiel #2
0
 def test_should_raise_if_module_does_not_exist(self):
     with self.assertRaises(ImportError):
         get_module_by_name('module-does-not-exist')
Beispiel #3
0
def setup_default_urls(module_name):
    """
    Setup default url patterns by adding additional url patterns to the given
    list of url patterns. Such additional patterns are concerned about
    serving media in DEBUG mode and adding default 404 and 500 handlers in
    PRODUCTION mode.
    """
    # get urls module
    urls = get_module_by_name(module_name)

    # create empty urlpatterns if not there yet
    if not hasattr(urls, 'urlpatterns'):
        urls.urlpatterns = []

    # django debug toolbar (debug only)
    if settings.DEBUG and settings.DEBUG_TOOLBAR:
        import debug_toolbar
        urls.urlpatterns += [
            url(r'^__debug__/', include(debug_toolbar.urls)),
        ]

    # 404 and 500 pages (PRODUCTION only)
    if not settings.DEBUG:
        urls.handler404 = settings.HANDLER_404
        urls.handler500 = settings.HANDLER_500
    else: # pragma: no cover
        urls.urlpatterns += [
            url(r'^404/$', get_callable(settings.HANDLER_404)),
            url(r'^500/$', get_callable(settings.HANDLER_500)),
        ]

    # enable postcode view if postcode in installed apps
    if 'cubane.postcode' in settings.INSTALLED_APPS:
        from cubane.postcode import views as postcode_views
        urls.urlpatterns += [
            url(r'^postcode-lookup/$', postcode_views.postcode_lookup, name='cubane.postcode_lookup')
        ]


    # serve media assets through django dev server (DEBUG only)
    if settings.DEBUG: # pragma: no cover
        # uploaded media files
        urls.urlpatterns += [
            url(
                r'^%s(?P<path>.*)$' % settings.MEDIA_URL,
                static.serve,
                {'document_root': settings.MEDIA_ROOT}
            )
        ]


    # serve media assets through frontend media api to allow for on-demand
    # media customisation and for downloading SVG icon sets
    if 'cubane.media' in settings.INSTALLED_APPS:
        from cubane.media import views as media_views

        # shapes
        urls.urlpatterns += [
            url(
                r'^%sshapes/(?P<shape>[-_\w]+)/(?P<size>[-_\w]+)/(?P<bucket>\d+)/(?P<pk>\d+)/(?P<filename>.*?)$' % settings.MEDIA_API_URL,
                media_views.media_api,
                name='cubane.media_api.shape'
            )
        ]

        # originals
        urls.urlpatterns += [
            url(
                r'^%soriginals/(?P<bucket>\d+)/(?P<pk>\d+)/(?P<filename>.*?)$' % settings.MEDIA_API_URL,
                media_views.media_api_original,
                name='cubane.media_api.original'
            )
        ]

        # by identifier alone
        urls.urlpatterns += [
            url(
                r'^%spk/(?P<pk>\d+)/$' % settings.MEDIA_API_URL,
                media_views.media_api_pk,
                name='cubane.media_api.pk'
            )
        ]

        # SVG icons sets (DEBUG only)
        if settings.DEBUG and 'cubane.svgicons' in settings.INSTALLED_APPS:
            from cubane.svgicons import views as svgicons_views
            urls.urlpatterns += [
                # individual icon
                url(
                    r'^%ssvgicons/(?P<target>[-_\w\d]+)/(?P<name>[-_\w\d]+)\.svg$' % settings.MEDIA_API_URL,
                    svgicons_views.media_api_svgicons
                ),

                # full icon set
                url(
                    r'^%ssvgicons/(?P<target>[-_\w\d]+)\.svg$' % settings.MEDIA_API_URL,
                    svgicons_views.media_api_svgicons,
                    kwargs={'name': None}
                )
            ]
Beispiel #4
0
 def test_should_return_mobule_if_exists(self):
     m = get_module_by_name('cubane')
     self.assertIsNotNone(m)
     self.assertEqual(m.__class__.__name__, 'module')