make it possible to add variables to specific services (#5329)

This commit is contained in:
Frédéric Péters 2014-08-26 11:06:22 +02:00
parent 33f3e45d05
commit ed37e011b9
6 changed files with 58 additions and 7 deletions

View File

@ -4,7 +4,7 @@ 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
from .models import Authentic, Wcs, Passerelle, Variable
SECRET_CHARS = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'
EXCLUDED_FIELDS = ('slug', 'last_operational_check_timestamp',
@ -62,3 +62,18 @@ class PasserelleForm(BaseForm):
class Meta:
model = Passerelle
exclude = EXCLUDED_FIELDS
class VariableForm(forms.ModelForm):
class Meta:
model = Variable
exclude = ('service_type', 'service_pk')
def __init__(self, service=None, **kwargs):
self.service = service
super(VariableForm, self).__init__(**kwargs)
def save(self, commit=True):
if self.service:
self.instance.service = self.service
return super(VariableForm, self).save(commit=commit)

View File

@ -9,15 +9,20 @@ from django.core.exceptions import ValidationError
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from .utils import Zone, get_installed_services_dict
class Variable(models.Model):
name = models.CharField(max_length=100, verbose_name=_('name'),
unique=True)
name = models.CharField(max_length=100, verbose_name=_('name'))
value = models.TextField(verbose_name=_('value'),
blank=True,
help_text=_('start with [ or { for a JSON document'))
service_type = models.ForeignKey(ContentType, null=True)
service_pk = models.PositiveIntegerField(null=True)
service = generic.GenericForeignKey('service_type', 'service_pk')
last_update_timestamp = models.DateTimeField(auto_now=True, null=True)
@property
@ -48,6 +53,9 @@ class ServiceBase(models.Model):
last_operational_success_timestamp = models.DateTimeField(null=True)
last_update_timestamp = models.DateTimeField(auto_now=True, null=True)
variables = generic.GenericRelation(Variable,
content_type_field='service_type', object_id_field='service_pk')
def is_operational(self):
return (self.last_operational_success_timestamp is not None and
self.last_operational_success_timestamp == self.last_operational_check_timestamp)
@ -73,6 +81,7 @@ class ServiceBase(models.Model):
as_dict = dict([(x, y) for (x, y) in self.__dict__.items()
if type(y) in (int, str, unicode)])
as_dict['service-id'] = self.Extra.service_id
as_dict['variables'] = dict(((v.name, v.json) for v in self.variables.all()))
return as_dict

View File

@ -43,6 +43,7 @@
{% for service in installed_services %}
<div data-service-id="{{ service.Extra.service_id }}"
data-slug="{{ service.slug }}"
class="bo-block"
{% if service.wants_frequent_checks %}data-wants-check="true"{% endif %}>
<h3>{{ service.title }}</h3>
{% if not service.is_operational %}
@ -56,11 +57,21 @@
</p>
{% endif %}
{% endif %}
<p>
<form class="small" method="post" action=" {{ service|save_url }}" >
{% csrf_token %}
{{ service|as_update_form }}
<button class="enable-on-change" disabled="disabled">{% trans 'Save' %}</button>
<h4>{% trans "Custom variables" %}</h4>
{% for variable in service.variables.all %}
<p class="variable">
<label data-variable-id="{{ variable.id }}">{{ variable.name }}</label>
<input type="text" size="80" value="{{ variable.value }}" readonly>
<a rel="popup" class="update-variable" href="{% url 'update-variable' pk=variable.id %}" title="{% trans 'Update variable' %}">{% trans 'edit' %}</a>
<a rel="popup" class="icon-remove-sign" href="{% url 'delete-variable' pk=variable.id %}" title="{% trans 'Delete variable' %}"></a>
</p>
{% endfor %}
<a rel="popup" class="button" href="{% url 'new-variable-service' service=service.Extra.service_id slug=service.slug %}">{% trans 'Add new variable' %}</a>
</form>
</div>
{% endfor %}

View File

@ -14,6 +14,9 @@ urlpatterns = patterns('',
url(r'^new-(?P<service>\w+)$', ServiceCreateView.as_view(), name='create-service'),
url(r'^save-(?P<service>\w+)/(?P<slug>[\w-]+)$', ServiceUpdateView.as_view(), name='save-service'),
url(r'^delete-(?P<service>\w+)/(?P<slug>[\w-]+)$', ServiceDeleteView.as_view(), name='delete-service'),
url(r'^new-variable-(?P<service>\w+)/(?P<slug>[\w-]+)$',
VariableCreateView.as_view(), name='new-variable-service',),
url(r'^installed_services.json$', installed_services_json_view),
url(r'^operational_services.json$', operational_services_json_view),
)

View File

@ -30,7 +30,7 @@ def get_installed_services_dict():
'timestamp': calendar.timegm(timestamp.timetuple()),
'services': [x.as_dict() for x in get_installed_services()],
'variables': dict(((v.name, v.json)
for v in Variable.objects.all())),
for v in Variable.objects.filter(service_pk__isnull=True))),
}

View File

@ -23,7 +23,7 @@ class HomeView(TemplateView):
def get_context_data(self, **kwargs):
context = super(HomeView, self).get_context_data(**kwargs)
context['url_template'] = settings.SERVICE_URL_TEMPLATE
context['variables'] = Variable.objects.all()
context['variables'] = Variable.objects.filter(service_pk__isnull=True)
context['available_services'] = [
AvailableService(x) for x in AVAILABLE_SERVICES]
context['installed_services'] = utils.get_installed_services()
@ -33,11 +33,24 @@ class HomeView(TemplateView):
class VariableCreateView(CreateView):
model = Variable
success_url = reverse_lazy('environment-home')
form_class = forms.VariableForm
def get_form_kwargs(self):
kwargs = super(VariableCreateView, self).get_form_kwargs()
if 'service' in self.kwargs:
service_id = self.kwargs.pop('service')
service_slug = self.kwargs.pop('slug')
for service in AVAILABLE_SERVICES:
if service.Extra.service_id == service_id:
kwargs['service'] = service.objects.get(slug=service_slug)
break
return kwargs
class VariableUpdateView(UpdateView):
model = Variable
success_url = reverse_lazy('environment-home')
form_class = forms.VariableForm
class VariableDeleteView(DeleteView):
@ -146,6 +159,6 @@ def operational_services_json_view(request, **kwargs):
json.dump({
'services': [x.as_dict() for x in utils.get_operational_services()],
'variables': dict(((v.name, v.json)
for v in Variable.objects.all())),
for v in Variable.objects.filter(service_pk__isnull=True))),
}, response)
return response