Commit b8c96939 by Oláh István Gergely

dashboard: add trait with view

parent 2972e5cb
......@@ -815,8 +815,20 @@ class TraitForm(forms.ModelForm):
self.helper.form_show_labels = False
self.helper.layout = Layout(
Div(
'name',
css_class="col-sm-2",
Div(
Field('name', id="node-details-traits-input",
css_class="input-sm input-traits"),
Div(
HTML('<input type="submit" '
'class="btn btn-default btn-sm input-traits" '
'value="Add trait"/>',
),
css_class="input-group-btn",
),
css_class="input-group",
id="node-details-traits-form",
),
css_class="row",
),
)
......
{% extends "dashboard/base.html" %}
{% load i18n %}
{% load crispy_forms_tags %}
{% block content %}
<style>
.help-block {
display:none;
}
.row {
margin-bottom: 15px;
}
</style>
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="no-margin"><i class="icon-plus"></i> {% trans "Add Trait" %}</h3>
</div>
<div class="panel-body">
{% with form=form %}
{% include "display-form-errors.html" %}
{% endwith %}
{% crispy form %}
</div>
</div>
</div>
</div>
{% endblock %}
......@@ -21,15 +21,17 @@
<small>{% trans "No trait added!" %}</small>
{% endif %}
</div>
<form action="" method="POST">
{% csrf_token %}
<div class="input-group" id="node-details-traits-form">
<input type="text" class="form-control input-sm input-traits" name="new_trait" id="node-details-traits-input"/>
<div class="input-group-btn">
<input type="submit" class="btn btn-default btn-sm input-traits" value="{% trans "Add trait" %}"/>
</div>
</div>
</form>
{% load crispy_forms_tags %}
<style>
.row {
margin-bottom: 15px;
}
</style>
<form action="{% url "dashboard.views.node-addtrait" node.pk %}" method="POST">
{% csrf_token %}
{% crispy trait_form %}
</form>
</div><!-- id:node-details-traits -->
</div>
<div class="col-md-8">
......
......@@ -8,7 +8,7 @@ from .views import (
TemplateList, LeaseDetail, NodeCreate, LeaseCreate, TemplateCreate,
FavouriteView, NodeStatus, GroupList, TemplateDelete, LeaseDelete,
VmGraphView, TemplateAclUpdateView, GroupDetailView, GroupDelete,
GroupAclUpdateView, GroupUserDelete,
GroupAclUpdateView, GroupUserDelete, NodeAddTraitView,
)
urlpatterns = patterns(
......@@ -52,6 +52,8 @@ urlpatterns = patterns(
url(r'^node/list/$', NodeList.as_view(), name='dashboard.views.node-list'),
url(r'^node/(?P<pk>\d+)/$', NodeDetailView.as_view(),
name='dashboard.views.node-detail'),
url(r'^node/(?P<pk>\d+)/add-trait/$', NodeAddTraitView.as_view(),
name='dashboard.views.node-addtrait'),
url(r'^tx/$', TransferOwnershipConfirmView.as_view(),
name='dashboard.views.vm-transfer-ownership-confirm'),
url(r'^node/delete/(?P<pk>\d+)/$', NodeDelete.as_view(),
......
......@@ -29,11 +29,12 @@ from braces.views import LoginRequiredMixin, SuperuserRequiredMixin
from .forms import (
VmCreateForm, TemplateForm, LeaseForm, NodeForm, HostForm, DiskAddForm,
TraitForm,
)
from .tables import (VmListTable, NodeListTable, NodeVmListTable,
TemplateListTable, LeaseListTable, GroupListTable,)
from vm.models import (Instance, InstanceTemplate, InterfaceTemplate,
InstanceActivity, Node, instance_activity, Lease,
InstanceActivity, Node, Trait, instance_activity, Lease,
Interface, NodeActivity, )
from firewall.models import Vlan, Host, Rule
from storage.models import Disk
......@@ -439,8 +440,12 @@ class VmDetailView(CheckedDetailView):
class NodeDetailView(LoginRequiredMixin, SuperuserRequiredMixin, DetailView):
template_name = "dashboard/node-detail.html"
model = Node
form = None
form_class = TraitForm
def get_context_data(self, **kwargs):
def get_context_data(self, form=None, **kwargs):
if form is None:
form = self.form_class()
context = super(NodeDetailView, self).get_context_data(**kwargs)
instances = Instance.active.filter(node=self.object)
context['table'] = NodeVmListTable(instances)
......@@ -448,6 +453,7 @@ class NodeDetailView(LoginRequiredMixin, SuperuserRequiredMixin, DetailView):
node=self.object, parent=None
).order_by('-started').select_related()
context['activities'] = ia
context['trait_form'] = form
return context
def post(self, request, *args, **kwargs):
......@@ -455,8 +461,6 @@ class NodeDetailView(LoginRequiredMixin, SuperuserRequiredMixin, DetailView):
return self.__set_name(request)
if request.POST.get('new_status'):
return self.__set_status(request)
if request.POST.get('new_trait'):
return self.__add_trait(request)
if request.POST.get('to_remove'):
return self.__remove_trait(request)
......@@ -508,24 +512,6 @@ class NodeDetailView(LoginRequiredMixin, SuperuserRequiredMixin, DetailView):
return redirect(reverse_lazy("dashboard.views.node-detail",
kwargs={'pk': self.object.pk}))
def __add_trait(self, request):
new_trait_name = request.POST.get('new_trait')
self.object = self.get_object()
if len(new_trait_name) < 1:
message = u"Please input something!"
elif len(new_trait_name) > 20:
message = u"Trait name is too long!"
else:
self.object.traits.create(name=new_trait_name)
try:
messages.error(request, message)
except:
pass
return redirect(self.object.get_absolute_url())
def __remove_trait(self, request):
try:
to_remove = request.POST.get('to_remove')
......@@ -1230,6 +1216,40 @@ class NodeDelete(LoginRequiredMixin, SuperuserRequiredMixin, DeleteView):
return reverse_lazy('dashboard.index')
class NodeAddTraitView(SuperuserRequiredMixin, DetailView):
model = Node
template_name = "dashboard/node-add-trait.html"
def get_success_url(self):
next = self.request.GET.get('next')
if next:
return next
else:
return reverse_lazy("dashboard.views.node-detail",
kwargs={'pk': self.object.pk})
def get_context_data(self, **kwargs):
self.object = self.get_object()
context = super(NodeAddTraitView, self).get_context_data(**kwargs)
context['form'] = (TraitForm(self.request.POST) if self.request.POST
else TraitForm())
return context
def post(self, request, pk, *args, **kwargs):
context = self.get_context_data(**kwargs)
form = context['form']
if form.is_valid():
node = self.object
n = form.cleaned_data['name']
trait, created = Trait.objects.get_or_create(name=n)
node.traits.add(trait)
success_message = _("Trait successfully added to node.")
messages.success(request, success_message)
return redirect(self.get_success_url())
else:
return self.get(self, request, pk, *args, **kwargs)
class NodeStatus(LoginRequiredMixin, SuperuserRequiredMixin, DetailView):
template_name = "dashboard/confirm/node-status.html"
model = Node
......
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