# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright (c) 2013 Mirantis Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from django import template

import logging

from django.utils.translation import ugettext_lazy as _

from horizon import tables

LOG = logging.getLogger(__name__)


class CreateNodegroupTemplate(tables.LinkAction):
    name = "create"
    verbose_name = _("Create Node group Template")
    url = "horizon:savanna:nodegroup_templates:create-nodegroup-template"
    classes = ("ajax-modal", "btn-create", "create-nodegrouptemplate-btn")


class ConfigureNodegroupTemplate(tables.LinkAction):
    name = "configure"
    verbose_name = _("Configure Node group Template")
    url = "horizon:savanna:nodegroup_templates:configure-nodegroup-template"
    classes = ("ajax-modal", "btn-create", "configure-nodegrouptemplate-btn")


def render_processes(nodegroup_template):
    template_name = 'nodegroup_templates/_processes_list.html'
    context = {"processes": nodegroup_template.node_processes}
    return template.loader.render_to_string(template_name, context)


class NodegroupTemplatesTable(tables.DataTable):
    name = tables.Column("name",
                         verbose_name=_("Node group template name"),
                         link=("horizon:savanna:nodegroup_templates:details"))
    plugin_name = tables.Column("plugin_name",
                                verbose_name=_("Plugin name"))
    hadoop_version = tables.Column("hadoop_version",
                                   verbose_name=_("Hadoop version"))
    node_processes = tables.Column(render_processes,
                                   verbose_name=_("Node processes"))

    class Meta:
        name = "nodegroup_templates"
        verbose_name = _("Node group Templates")
        table_actions = (CreateNodegroupTemplate, ConfigureNodegroupTemplate)
        row_actions = ()

@@ -748,16 +748,73 @@
 t tables
+%0Afrom savannadashboard.api import client as savannaclient
 %0A%0ALOG = 
@@ -1402,16 +1402,553 @@
 btn%22)%0A%0A%0A
+class DeleteTemplate(tables.BatchAction):%0A    name = %22delete_nodegroup_template%22%0A    verbose_name = _(%22Delete%22)%0A    classes = (%22btn-terminate%22, %22btn-danger%22)%0A%0A    action_present = _(%22Delete%22)%0A    action_past = _(%22Deleted%22)%0A    data_type_singular = _(%22Node group Template%22)%0A    data_type_plural = _(%22Node group Templates%22)%0A%0A    def allowed(self, request, template):%0A        return True%0A%0A    def action(self, request, template_id):%0A        savanna = savannaclient.Client(request)%0A        savanna.node_group_templates.delete(template_id)%0A%0A%0A
 def rend
@@ -2913,16 +2913,41 @@
 emplate,
+%0A                        
  Configu
@@ -2965,16 +2965,57 @@
 Template
+,%0A                         DeleteTemplate
 )%0A      
@@ -3031,10 +3031,25 @@
 ions = (
+DeleteTemplate,
 )%0A

Delete nodegroup template button added.