add global variables (#5027)

This commit is contained in:
Thomas NOËL 2014-06-25 17:55:18 +02:00
parent a845ff0da3
commit 792ff4d4a3
7 changed files with 134 additions and 6 deletions

View File

@ -1,13 +1,36 @@
import datetime
import urllib2
import json
from django.db import models
from django.utils.timezone import now
from django.utils.translation import ugettext_lazy as _
from django.core.exceptions import ValidationError
from .utils import Zone
class Variable(models.Model):
name = models.CharField(max_length=100, verbose_name=_('name'),
unique=True)
value = models.TextField(verbose_name=_('value'),
blank=True,
help_text=_('start with [ or { for a JSON document'))
@property
def json(self):
if self.value and self.value[0] in '{[':
return json.loads(self.value)
return self.value
def clean(self):
if self.value and self.value[0] in '{[':
try:
json.loads(self.value)
except ValueError:
raise ValidationError('invalid JSON document')
class ServiceBase(models.Model):
class Meta:
abstract = True

View File

@ -15,6 +15,19 @@
<button disabled="disabled">{% trans 'Save' %}</button>
</form>
<h2>{% trans 'Variables' %}</h2>
<form class="small">
{% for variable in variables %}
<p>
<label class="update-variable" data-variable-id="{{ variable.id }}">{{ variable.name }}</label>
<input type="text" size="80" value="{{ variable.value }}" readonly>
<a href="delete-variable/{{ variable.id }}" class="delete-variable" title="{% trans 'Delete variable' %}"></a>
</p>
{% endfor %}
<button id="new-variable">{% trans 'Add new variable' %}</button>
</form>
<h2>{% trans 'Services' %}</h2>
<p>
@ -77,6 +90,35 @@ jQuery.fn.extend({
});
$(function() {
$('#new-variable').click(function() {
$.get('new-variable', function(data) {
var form = $(data).find('form');
$(form).attr('action', 'new-variable');
$(form).dialog({modal: true, title: '{% trans "Add new variable" %}', width: 'auto'});
});
event.preventDefault();
});
$('.update-variable').click(function() {
var variable_id = $(this).data('variable-id');
$.get('update-variable/' + variable_id, function(data) {
var form = $(data).find('form');
$(form).attr('action', 'update-variable/' + variable_id);
$(form).dialog({modal: true, title: '{% trans "Update variable" %}', width: 'auto'});
});
event.preventDefault();
});
$('a.delete-variable').click(function() {
var url = $(this).attr('href');
$.get($(this).attr('href'), function(data) {
var form = $(data).find('form');
$(form).dialog({modal: true, title: '{% trans "Variable deletion" %}', width: 'auto'});f
});
event.preventDefault();
});
$('#new-service').change(function() {
var service_id = $(this).val();
$.get('new-' + service_id, function(data) {

View File

@ -0,0 +1,12 @@
{% extends "hobo/base.html" %}
{% load i18n %}
{% block content %}
<form action="{% url 'delete-variable' object.id %}" method="post">
{% csrf_token %}
{% blocktrans with title=object.name %}
Are you sure you want to delete "{{ title }}"?
{% endblocktrans %}
<button class="enable-on-change">{% trans 'Delete' %}</button>
</form>
{% endblock %}

View File

@ -0,0 +1,21 @@
{% extends "hobo/base.html" %}
{% load i18n %}
{% block appbar %}
<h2>Variable</h2>
<span><a href="./">{% trans 'Back to settings' %}</a></span>
{% endblock %}
{% block content %}
<form method="post" enctype="multipart/form-data">
<div id="form-content">
{% csrf_token %}
{{ form.as_p }}
</div>
{% block buttons %}
<button class="enable-on-change">{% trans 'Save' %}</button>
{% endblock %}
</form>
{% endblock %}

View File

@ -4,6 +4,11 @@ from .views import *
urlpatterns = patterns('',
url(r'^$', HomeView.as_view(), name='environment-home'),
url(r'^new-variable$', VariableCreateView.as_view()),
url(r'^update-variable/(?P<pk>\w+)$', VariableUpdateView.as_view(),
name='update-variable'),
url(r'^delete-variable/(?P<pk>\w+)$', VariableDeleteView.as_view(),
name='delete-variable'),
url(r'^check_operational/(?P<service>\w+)/(?P<slug>[\w-]+)$',
operational_check_view, name='operational-check'),
url(r'^new-(?P<service>\w+)$', ServiceCreateView.as_view()),

View File

@ -7,7 +7,7 @@ from django.http import HttpResponse
from django.views.generic.base import TemplateView
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from .models import Authentic, Wcs, AVAILABLE_SERVICES
from .models import Variable, Authentic, Wcs, AVAILABLE_SERVICES
from . import forms, utils
@ -23,12 +23,28 @@ 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['available_services'] = [
AvailableService(x) for x in AVAILABLE_SERVICES]
context['installed_services'] = utils.get_installed_services()
return context
class VariableCreateView(CreateView):
model = Variable
success_url = reverse_lazy('environment-home')
class VariableUpdateView(UpdateView):
model = Variable
success_url = reverse_lazy('environment-home')
class VariableDeleteView(DeleteView):
model = Variable
success_url = reverse_lazy('environment-home')
class ServiceCreateView(CreateView):
success_url = reverse_lazy('environment-home')
@ -118,11 +134,19 @@ def operational_check_view(request, service, slug, **kwargs):
def installed_services_json_view(request, **kwargs):
response = HttpResponse(content_type='application/json')
json.dump([x.as_dict() for x in utils.get_installed_services()], response)
json.dump({
'services': [x.as_dict() for x in utils.get_installed_services()],
'variables': dict(((v.name, v.json)
for v in Variable.objects.all())),
}, response)
return response
def operational_services_json_view(request, **kwargs):
response = HttpResponse(content_type='application/json')
json.dump([x.as_dict() for x in utils.get_operational_services()], response)
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())),
}, response)
return response

View File

@ -15,12 +15,13 @@ p.being-deployed {
padding-left: 2em;
}
a.delete-service {
a.delete-service, a.delete-variable {
float: right;
text-decoration: none;
}
a.delete-service:after {
a.delete-service:after, a.delete-variable:after {
font: normal 1.5em FontAwesome;
content:"\f057";
}
}