Commit 07b66c82 by Kálmán Viktor

dashboard: create template

parent 3b4b53ee
...@@ -301,6 +301,7 @@ class TemplateForm(forms.ModelForm): ...@@ -301,6 +301,7 @@ class TemplateForm(forms.ModelForm):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(TemplateForm, self).__init__(*args, **kwargs) super(TemplateForm, self).__init__(*args, **kwargs)
self.fields['disks'] = forms.ModelMultipleChoiceField(queryset=DISKS) self.fields['disks'] = forms.ModelMultipleChoiceField(queryset=DISKS)
if self.instance.pk:
mn = self.instance.interface_set.filter( mn = self.instance.interface_set.filter(
managed=True).values_list("vlan", flat=True) managed=True).values_list("vlan", flat=True)
un = self.instance.interface_set.filter( un = self.instance.interface_set.filter(
...@@ -310,6 +311,14 @@ class TemplateForm(forms.ModelForm): ...@@ -310,6 +311,14 @@ class TemplateForm(forms.ModelForm):
def save(self, commit=True): def save(self, commit=True):
data = self.cleaned_data 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 # create and/or delete InterfaceTemplates
managed = InterfaceTemplate.objects.filter( managed = InterfaceTemplate.objects.filter(
managed=True, template=self.instance).values_list("vlan", managed=True, template=self.instance).values_list("vlan",
...@@ -332,17 +341,15 @@ class TemplateForm(forms.ModelForm): ...@@ -332,17 +341,15 @@ class TemplateForm(forms.ModelForm):
InterfaceTemplate.objects.filter( InterfaceTemplate.objects.filter(
managed=False, template=self.instance).exclude( managed=False, template=self.instance).exclude(
vlan__in=data['unmanaged_networks']).delete() 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 return instance
@property @property
def helper(self): def helper(self):
helper = FormHelper() helper = FormHelper()
if not self.instance.pk:
self.instance.priority = 20
self.instance.ram_size = 512
self.instance.num_cores = 2
helper.layout = Layout( helper.layout = Layout(
Field("name"), Field("name"),
Fieldset( Fieldset(
...@@ -386,7 +393,7 @@ class TemplateForm(forms.ModelForm): ...@@ -386,7 +393,7 @@ class TemplateForm(forms.ModelForm):
), ),
css_class="row", css_class="row",
), ),
Field('max_ram_size', type="hidden"), Field('max_ram_size', type="hidden", value="0"),
Field('arch'), Field('arch'),
), ),
Fieldset( 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 @@ ...@@ -7,9 +7,12 @@
<div class="col-md-12"> <div class="col-md-12">
<div class="panel panel-default"> <div class="panel panel-default">
<div class="panel-heading"> <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 <i class="icon-plus"></i> new template
</a> </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> <h3 class="no-margin"><i class="icon-desktop"></i> {% trans "Templates" %}</h3>
</div> </div>
<div class="panel-body"> <div class="panel-body">
......
...@@ -15,6 +15,8 @@ urlpatterns = patterns( ...@@ -15,6 +15,8 @@ urlpatterns = patterns(
name="dashboard.views.lease-detail"), name="dashboard.views.lease-detail"),
url(r'^lease/create/$', LeaseCreate.as_view(), url(r'^lease/create/$', LeaseCreate.as_view(),
name="dashboard.views.lease-create"), 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(), url(r'^template/(?P<pk>\d+)/$', TemplateDetail.as_view(),
name='dashboard.views.template-detail'), name='dashboard.views.template-detail'),
url(r"^template/list/$", TemplateList.as_view(), url(r"^template/list/$", TemplateList.as_view(),
......
...@@ -351,6 +351,16 @@ class AclUpdateView(View, SingleObjectMixin): ...@@ -351,6 +351,16 @@ class AclUpdateView(View, SingleObjectMixin):
value, unicode(request.user)) 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): class TemplateDetail(SuccessMessageMixin, UpdateView):
model = InstanceTemplate model = InstanceTemplate
template_name = "dashboard/template-edit.html" 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