Commit c529f9c6 by Kálmán Viktor

dashboard: clone templates from parents

parent 020ee0a8
......@@ -299,16 +299,39 @@ class TemplateForm(forms.ModelForm):
queryset=VLANS, required=False)
def __init__(self, *args, **kwargs):
parent = kwargs.pop("parent", None)
super(TemplateForm, self).__init__(*args, **kwargs)
self.fields['disks'] = forms.ModelMultipleChoiceField(queryset=DISKS)
if self.instance.pk:
mn = self.instance.interface_set.filter(
if parent is not None:
template = InstanceTemplate.objects.get(pk=parent)
parent = template.__dict__
fields = ["system", "name", "num_cores", "boot_menu", "ram_size",
"priority", "state", "access_method", "raw_data",
"arch", "description"]
for f in fields:
self.initial[f] = parent[f]
self.initial['lease'] = parent['lease_id']
self.initial['disks'] = template.disks.all()
self.initial['parent'] = template
self.initial['name'] = "Clone of %s" % self.initial['name']
self.for_networks = template
else:
self.for_networks = self.instance
if self.instance.pk or parent is not None:
mn = self.for_networks.interface_set.filter(
managed=True).values_list("vlan", flat=True)
un = self.instance.interface_set.filter(
un = self.for_networks.interface_set.filter(
managed=False).values_list("vlan", flat=True)
self.initial['managed_networks'] = mn
self.initial['unmanaged_networks'] = un
if not self.instance.pk and len(self.errors) < 1:
self.instance.priority = 20
self.instance.ram_size = 512
self.instance.num_cores = 2
def save(self, commit=True):
data = self.cleaned_data
self.instance.max_ram_size = data.get('ram_size')
......@@ -346,10 +369,6 @@ class TemplateForm(forms.ModelForm):
@property
def helper(self):
helper = FormHelper()
if not self.instance.pk and len(self.errors) < 1:
self.instance.priority = 20
self.instance.ram_size = 512
self.instance.num_cores = 2
helper.layout = Layout(
Field("name"),
Fieldset(
......@@ -403,7 +422,7 @@ class TemplateForm(forms.ModelForm):
Field('raw_data'),
Field('req_traits'),
Field('description'),
Field("parent"),
Field("parent", type="hidden"),
Field("system"),
Field("state"),
),
......
......@@ -10,9 +10,6 @@
<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">
......@@ -22,15 +19,10 @@
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="panel panel-default">
<div class="panel-heading">
<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>
......
{% load i18n %}
<a href="{% url "dashboard.views.template-create" %}?parent={{ record.pk }}" id="template-list-clone-button" class="btn btn-default btn-xs" title data-original-title="{% trans "Clone" %}">
<i class="icon-copy"></i>
</a>
<a href="{% url "dashboard.views.template-detail" pk=record.pk%}" id="template-list-edit-button" class="btn btn-default btn-xs" title data-original-title="{% trans "Edit" %}">
<i class="icon-edit"></i>
</a>
......
......@@ -361,6 +361,15 @@ class TemplateCreate(SuccessMessageMixin, CreateView):
template_name = "dashboard/template-create.html"
success_message = _("Successfully created a new template!")
def get(self, *args, **kwargs):
self.parent = self.request.GET.get("parent")
return super(TemplateCreate, self).get(*args, **kwargs)
def get_form_kwargs(self):
kwargs = super(TemplateCreate, self).get_form_kwargs()
kwargs['parent'] = getattr(self, "parent", None)
return kwargs
def get_success_url(self):
return reverse_lazy("dashboard.views.template-list")
......
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