Building SASS with Django and Airavata

By Emmanuelle Delescolle

Who am I?

  • An average developper
  • Who loves working with Django
  • A woman who codes

Before getting started

Everyone knows the 3 most diffult things in STEM are

  • Naming things
  • Off-by-one errors

What steps are necessary/usefull to build SASS with Django?

Why?

Definition

  • Same application
  • Same code-base
  • Same database

  • At least some level of customization

Things Airavata takes care of:

  • ALLOWED_HOSTS
  • Per site url caching
  • Site-specific urls
  • Site-specific templates
  • Site-specific static files
  • CBV Mixin

ALLOWED_HOSTS

ALLOWED_HOSTS = ['*']

Unsecure


Django allowed sites

Doesn't handle domain aliases


Manually adding each domain to ALLOWED_HOSTS

Requires an application restart
Error-prone

ALLOWED_HOSTS

## settings.py
from airavata.utils import AllowedSites
ALLOWED_HOSTS = AllowedSites()

Per site url-caching

When

DEBUG = False

urls.py is only evaluated once!

Site-specific... everything

Sites modules

Site-specific urls

Django-host


Currently not compatible with Airavata

Site-specific urls

##urls.py
from airavata import urls

urlpatterns = urls.UrlPatterns([
    # Place your patterns here
    ...
    url(...),
])


##sites/mydomain_com/urls.py
urlpatterns += [
    url(
        r'^' + settings.STATIC_URL[1:] + r'(?P<path>.*)$',
        serve,
        {'document_root': settings.STATIC_ROOT}
    ),
]

Site-specific templates

##settings.py
TEMPLATES = [
    {
        ...
        ## Make sure APP_DIRS is set to False
        'APP_DIRS': False,
        'OPTIONS': {
            ...
            ## add a loaders option
            'loaders': (
                'airavata.template_loader.Loader',
                # 'django.template.loaders.filesystem.Loader',
                'django.template.loaders.app_directories.Loader'
            ),
        },
    },
]

Site-specific static files

##settings.py
STATICFILES_FINDERS = (
    "airavata.staticfiles_finder.SiteFinder",
    # "django.contrib.staticfiles.finders.FileSystemFinder",
    "django.contrib.staticfiles.finders.AppDirectoriesFinder",
)

CBV Mixin

from django.views.generic import DetailView, ListView
from airavata.views import SiteFilteredViewMixin

from .models import MyModel


class MyModelListView(SiteFilteredViewMixin, ListView):

  model = MyModel


class MyModelDetailView(SiteFilteredViewMixin, DetailView):

  model = MyModel
  site_field = 'base_site'

More?

Demo

Questions

Building SASS with Django and Airavata

By Emma

Building SASS with Django and Airavata

PyConUK 2015 talk

  • 1,979