Commit 07b66c82 by Kálmán Viktor

dashboard: create template

parent 3b4b53ee
......@@ -301,15 +301,24 @@ class TemplateForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(TemplateForm, self).__init__(*args, **kwargs)
self.fields['disks'] = forms.ModelMultipleChoiceField(queryset=DISKS)
mn = self.instance.interface_set.filter(
managed=True).values_list("vlan", flat=True)
un = self.instance.interface_set.filter(
managed=False).values_list("vlan", flat=True)
self.initial['managed_networks'] = mn
self.initial['unmanaged_networks'] = un
if self.instance.pk:
mn = self.instance.interface_set.filter(
managed=True).values_list("vlan", flat=True)
un = self.instance.interface_set.filter(
managed=False).values_list("vlan", flat=True)
self.initial['managed_networks'] = mn
self.initial['unmanaged_networks'] = un
def save(self, commit=True):
data = self.cleaned_data
self.instance.max_ram_size = data.get('ram_size')
instance = super(TemplateForm, self).save(commit=False)
if commit:
instance.save()
self.instance.disks = data['disks'] # TODO why do I need this
# create and/or delete InterfaceTemplates
managed = InterfaceTemplate.objects.filter(
managed=True, template=self.instance).values_list("vlan",
......@@ -332,17 +341,15 @@ class TemplateForm(forms.ModelForm):
InterfaceTemplate.objects.filter(
managed=False, template=self.instance).exclude(
vlan__in=data['unmanaged_networks']).delete()
self.instance.disks = data['disks'] # TODO why do I need this
self.instance.max_ram_size = data.get('ram_size')
instance = super(TemplateForm, self).save(commit=False)
if commit:
instance.save()
return instance
@property
def helper(self):
helper = FormHelper()
if not self.instance.pk:
self.instance.priority = 20
self.instance.ram_size = 512
self.instance.num_cores = 2
helper.layout = Layout(
Field("name"),
Fieldset(
......@@ -386,7 +393,7 @@ class TemplateForm(forms.ModelForm):
),
css_class="row",
),
Field('max_ram_size', type="hidden"),
Field('max_ram_size', type="hidden", value="0"),
Field('arch'),
),
Fieldset(
......
{% extends "dashboard/base.html" %}
{% load i18n %}
{% load crispy_forms_tags %}
{% block content %}
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">
<a class="pull-right btn btn-default btn-xs" href="{% url "dashboard.views.template-list" %}">{% trans "Back" %}</a>
<h3 class="no-margin"><i class="icon-desktop"></i> {% trans "Create template" %}</h3>
</div>
<div class="panel-body">
{% with form=form %}
{% include "display-form-errors.html" %}
{% endwith %}
{% crispy form %}
</div>
</div>
</div>
</div>
<style>
fieldset {
margin-top: 40px;
}
fieldset legend {
font-weight: bold;
}
</style>
<script>
$(function() {
$("#hint_id_num_cores, #hint_id_priority, #hint_id_ram_size").hide();
});
</script>
{% endblock %}
......@@ -7,9 +7,12 @@
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">
<a href="#" class="pull-right btn btn-success btn-xs">
<a href="{% url "dashboard.views.template-create" %}" class="pull-right btn btn-success btn-xs">
<i class="icon-plus"></i> new template
</a>
<a href="{% url "dashboard.views.lease-create" %}" class="pull-right btn btn-success btn-xs" style="margin-right: 10px;">
<i class="icon-plus"></i> new lease
</a>
<h3 class="no-margin"><i class="icon-desktop"></i> {% trans "Templates" %}</h3>
</div>
<div class="panel-body">
......
......@@ -15,6 +15,8 @@ urlpatterns = patterns(
name="dashboard.views.lease-detail"),
url(r'^lease/create/$', LeaseCreate.as_view(),
name="dashboard.views.lease-create"),
url(r'^template/create/$', TemplateCreate.as_view(),
name="dashboard.views.template-create"),
url(r'^template/(?P<pk>\d+)/$', TemplateDetail.as_view(),
name='dashboard.views.template-detail'),
url(r"^template/list/$", TemplateList.as_view(),
......
......@@ -351,6 +351,16 @@ class AclUpdateView(View, SingleObjectMixin):
value, unicode(request.user))
class TemplateCreate(SuccessMessageMixin, CreateView):
model = InstanceTemplate
form_class = TemplateForm
template_name = "dashboard/template-create.html"
success_message = _("Successfully created a new template!")
def get_success_url(self):
return reverse_lazy("dashboard.views.template-list")
class TemplateDetail(SuccessMessageMixin, UpdateView):
model = InstanceTemplate
template_name = "dashboard/template-edit.html"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment