Commit 3de47528 by Kálmán Viktor

dashboard: clone templates view and form part

parent b2953fc4
......@@ -7,9 +7,11 @@ from django.contrib.auth.forms import (
from crispy_forms.helper import FormHelper
from crispy_forms.layout import (
Layout, Div, BaseInput, Field, HTML, Submit, Fieldset, TEMPLATE_PACK
Layout, Div, BaseInput, Field, HTML, Submit, Fieldset, TEMPLATE_PACK,
)
from crispy_forms.bootstrap import UneditableField as UF
from django.shortcuts import get_object_or_404
from crispy_forms.utils import render_field
from django import forms
......@@ -600,6 +602,56 @@ class TemplateForm(forms.ModelForm):
exclude = ('state', 'disks', )
class TemplateCloneForm(forms.ModelForm):
uneditable_fields = ['num_cores', 'priority', 'ram_size', 'max_ram_size',
'arch', 'access_method', 'boot_menu', 'raw_data',
'description', 'system',
'lease', 'tags']
def __init__(self, *args, **kwargs):
self.clone_from = kwargs.pop("clone_from")
self.user = kwargs.pop("user")
super(TemplateCloneForm, self).__init__(*args, **kwargs)
data = self.data.copy()
template = get_object_or_404(InstanceTemplate, pk=self.clone_from)
for f in self.uneditable_fields:
self.initial[f] = getattr(template, f)
value = getattr(template, f)
if hasattr(value, "pk"):
value = value.pk
data.update({'%s' % f: value})
data.update({'owner': self.user.pk})
self.data = data
def save(self, *args, **kwargs):
pass
@property
def helper(self):
helper = FormHelper()
ufs = [UF(uf) for uf in self.uneditable_fields]
helper.layout = Layout(
Fieldset(
_("Modify these"),
Field("name"),
),
Fieldset(
_("You can't modify these ..."),
*ufs
)
)
helper.add_input(Submit('submit', _('Clone template')))
return helper
class Meta:
model = InstanceTemplate
exclude = ('state', 'disks', )
class LeaseForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
......
{% extends "dashboard/base.html" %}
{% load i18n %}
{% load sizefieldtags %}
{% load crispy_forms_tags %}
{% block title-page %}{% trans "Clone template" %}{% endblock %}
{% 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-puzzle-piece"></i> {% trans "Clone template" %}</h3>
</div>
<div class="panel-body">
{% with form=form %}
{% include "display-form-errors.html" %}
{% endwith %}
{% crispy form %}
</div>
</div>
</div>
</div><!-- .row -->
<style>
fieldset {
margin-top: 40px;
}
fieldset legend {
font-weight: bold;
}
</style>
{% endblock %}
{% block extra_js %}
{% endblock %}
{% load i18n %}
<a href="{% url "dashboard.views.template-create" %}?parent={{ record.pk }}" id="template-list-clone-button" class="btn btn-default btn-xs" title="{% trans "Clone" %}">
<a href="{% url "dashboard.views.template-clone" pk=record.pk %}" id="template-list-clone-button" class="btn btn-default btn-xs" 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="{% trans "Edit" %}">
......
......@@ -11,7 +11,7 @@ from .views import (
TransferOwnershipConfirmView, TransferOwnershipView, vm_activity, VmCreate,
VmDelete, VmDetailView, VmDetailVncTokenView, VmGraphView, VmList,
VmMassDelete, VmMigrateView, VmRenewView, DiskRemoveView,
get_disk_download_status, TemplateChoose,
get_disk_download_status, TemplateChoose, TemplateClone
)
urlpatterns = patterns(
......@@ -36,6 +36,8 @@ urlpatterns = patterns(
name="dashboard.views.template-list"),
url(r"^template/delete/(?P<pk>\d+)/$", TemplateDelete.as_view(),
name="dashboard.views.template-delete"),
url(r"^template/clone/(?P<pk>\d+)/$", TemplateClone.as_view(),
name="dashboard.views.template-clone"),
url(r'^vm/(?P<pk>\d+)/remove_port/(?P<rule>\d+)/$', PortDelete.as_view(),
name='dashboard.views.remove-port'),
......
......@@ -37,7 +37,7 @@ from braces.views import (
from .forms import (
CircleAuthenticationForm, DiskAddForm, HostForm, LeaseForm, MyProfileForm,
NodeForm, TemplateForm, TraitForm, VmCustomizeForm,
NodeForm, TemplateForm, TraitForm, VmCustomizeForm, TemplateCloneForm
)
from .tables import (NodeListTable, NodeVmListTable,
TemplateListTable, LeaseListTable, GroupListTable,)
......@@ -762,6 +762,36 @@ class TemplateChoose(TemplateView):
return context
class TemplateClone(CreateView):
template_name = "dashboard/template-clone.html"
form_class = TemplateCloneForm
model = InstanceTemplate
def get_form_kwargs(self):
kwargs = super(TemplateClone, self).get_form_kwargs()
kwargs['clone_from'] = self.kwargs['pk']
kwargs['user'] = self.request.user
return kwargs
def get(self, *args, **kwargs):
if not self.request.user.has_perm('vm.create_template'):
raise PermissionDenied()
return super(TemplateClone, self).get(*args, **kwargs)
def post(self, request, *args, **kwargs):
if not self.request.user.has_perm('vm.create_template'):
raise PermissionDenied()
form = self.form_class(request.POST, clone_from=kwargs['pk'],
user=request.user)
if not form.is_valid():
return self.get(request, form, *args, **kwargs)
else:
# clone template
return redirect("/") # temp
class TemplateCreate(SuccessMessageMixin, CreateView):
model = InstanceTemplate
form_class = TemplateForm
......
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