add the possibility of service templates (#4580)

It is possible to define templates using the new SERVICE_TEMPLATES
settings variable, it expects a dictionary mapping service identifiers
to a list of (template name, template title) tuples.

Note: Template names are opaque identifiers, it's up to the deployment
agents to assign some meaning to them.

Example:

    SERVICE_TEMPLATES = {
        'wcs': [('export-auquo-light.wcs', u'Au quotidien light'),
                ('export-auquo.wcs', u'Au quotidien'),
                ('export-demo.wcs', u'Au quotidien Demo')],
    }
This commit is contained in:
Frédéric Péters 2014-08-25 15:47:06 +02:00
parent 8404eef58b
commit 33f3e45d05
3 changed files with 45 additions and 0 deletions

View File

@ -1,6 +1,8 @@
from django import forms
from django.conf import settings
from django.template.defaultfilters import slugify
from django.utils.crypto import get_random_string
from django.utils.translation import ugettext_lazy as _
from .models import Authentic, Wcs, Passerelle
@ -9,11 +11,38 @@ EXCLUDED_FIELDS = ('slug', 'last_operational_check_timestamp',
'last_operational_success_timestamp', 'secret_key')
class BaseForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
choices = self.get_template_choices()
super(BaseForm, self).__init__(*args, **kwargs)
if len(choices) < 2 or self.instance.id:
del self.fields['template_name']
else:
self.fields['template_name'].choices = choices
self.fields['template_name'].widget = forms.Select(choices=choices)
# the template name cannot change once the object exists, display the
# choice that was selected as an additional, disabled, <select> widget.
if self.instance.id and len(choices) > 1:
self.fields['template_name_readonly'] = forms.fields.CharField(
label=_('Template'), required=False,
initial=self.instance.template_name)
self.fields['template_name_readonly'].widget = forms.Select(choices=choices)
self.fields['template_name_readonly'].widget.attrs['disabled'] = 'disabled'
def get_template_choices(self):
if not settings.SERVICE_TEMPLATES:
return []
service_id = self.Meta.model.Extra.service_id
return settings.SERVICE_TEMPLATES.get(service_id, [])
def save(self, commit=True):
if not self.instance.slug:
self.instance.slug = slugify(self.instance.title)
if not self.instance.secret_key:
self.instance.secret_key = get_random_string(50, SECRET_CHARS)
choices = self.get_template_choices()
if not self.instance.id and len(choices) == 1:
self.instance.template_name = choices[0][0]
return super(BaseForm, self).save(commit=commit)

View File

@ -42,6 +42,7 @@ class ServiceBase(models.Model):
slug = models.SlugField()
base_url = models.CharField(_('Base URL'), max_length=200)
secret_key = models.CharField(_('Secret Key'), max_length=60)
template_name = models.CharField(_('Template'), max_length=60, blank=True)
last_operational_check_timestamp = models.DateTimeField(null=True)
last_operational_success_timestamp = models.DateTimeField(null=True)

View File

@ -105,6 +105,21 @@ LOCALE_PATHS = (
SERVICE_URL_TEMPLATE = 'https://${app}.example.net'
# SERVICE_TEMPLATES: possible "flavours" for the various service types.
# This variable expects a dictionary mapping service identifiers
# to a list of (template name, template title) tuples.
#
# Note: Template names are opaque identifiers, it's up to the deployment
# agents to assign some meaning to them.
#
# Example:
# SERVICE_TEMPLATES = {
# 'wcs': [('export-auquo-light.wcs', u'Au quotidien light'),
# ('export-auquo.wcs', u'Au quotidien'),
# ('export-demo.wcs', u'Au quotidien Demo')],
# }
SERVICE_TEMPLATES = None
AGENT_HOST_PATTERNS = None
AGENT_WCS_APP_DIR = '/var/lib/wcs'
AGENT_WCS_COMMAND = '/usr/sbin/wcsctl check-hobos --site-url ${wcs_url}'