changeset 80:c00f20bd90ba

Allow deletion of (orphaned) Branches
author Benoît Allard <benoit.allard@greenbone.net>
date Thu, 09 Oct 2014 12:16:55 +0200
parents 2c7786d7d14e
children 65c874b2c78e
files farol/producttree.py farol/templates/producttree/view.j2 tests/testBranches.py
diffstat 3 files changed, 30 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/farol/producttree.py	Thu Oct 09 12:16:16 2014 +0200
+++ b/farol/producttree.py	Thu Oct 09 12:16:55 2014 +0200
@@ -107,6 +107,25 @@
     ptree.addBranch(branch)
     return redirect(url_for('.view'))
 
+@producttree.route('/branch/<path:path>/del', methods=['POST'])
+@document_required
+@producttree_required
+def del_branch(path):
+    path = [int(p) for p in path.split('/')]
+    cvrf = get_current()
+    ptree = cvrf._producttree
+    try: branch = cvrf._producttree.getBranch(path)
+    except (ValueError, IndexError):
+        flash('Cannot find Branch', 'danger')
+        abort(404)
+    if branch._childs:
+        flash('Cannot delete a branch with childs', 'danger')
+        abort(403)
+    branch.unlink()
+    del branch
+    return redirect(url_for('.view'))
+
+
 @producttree.route('/product/<productid>')
 @document_required
 @producttree_required
--- a/farol/templates/producttree/view.j2	Thu Oct 09 12:16:16 2014 +0200
+++ b/farol/templates/producttree/view.j2	Thu Oct 09 12:16:55 2014 +0200
@@ -24,7 +24,7 @@
 -#}
 
 {% extends "base.j2" %}
-{% from "macros.j2" import panel, modal %}
+{% from "macros.j2" import panel, modal, delete_button %}
 {% block title %}Product Tree{% endblock %}
 
 {% set active = 'product' %}
@@ -50,7 +50,7 @@
 {% call panel(heading="Branches", badge=producttree._branches | length, title=4, extended=True) %}
   {% for branch in producttree._branches recursive %}
     {% call panel() %}
-      <p>{{ branch._type}}: <em>{{ branch._name }}</em> (<a href="{{ url_for('.edit_branch', path=branch.getPath() | join('/')) }}">edit</a>)</p>
+      <div>{{ branch._type}}: <em>{{ branch._name }}</em> (<a href="{{ url_for('.edit_branch', path=branch.getPath() | join('/')) }}">edit</a>){% if branch.isOrphaned() %}{{ delete_button(url_for('.del_branch', path=branch.getPath() | join('/'))) }}{% endif %}</div>
       {% if branch._product %}
         <p><strong><a href="{{ url_for('.view_product', productid=branch._product._productid) }}">{{ branch._product._name }}</a></strong></p>
       {% else %}
--- a/tests/testBranches.py	Thu Oct 09 12:16:16 2014 +0200
+++ b/tests/testBranches.py	Thu Oct 09 12:16:55 2014 +0200
@@ -54,3 +54,12 @@
         rv = self.app.post('/producttree/branch/0/0/edit', data=dict(type="Vendor", name="Other", parent=""), follow_redirects=True)
         self._assertIn('<a href="/producttree/branch/1/edit">edit</a>', rv)
 
+    def testDeleteBranch(self):
+        rv = self.createDoc('Title', 'Type')
+        self.assertEqual(rv.status_code, 200)
+        rv = self.app.post('/producttree/create')
+        rv = self.app.get('/producttree/branch/add')
+        rv = self.app.post('/producttree/branch/add', data=dict(type="Vendor", name="SecPod", parent=""))
+        rv = self.app.post('/producttree/branch/0/del', follow_redirects=True)
+        self.assertEqual(rv.status_code, 200)
+

http://farol.wald.intevation.org