# HG changeset patch # User BenoƮt Allard # Date 1412849815 -7200 # Node ID c00f20bd90bac4cd5268092af5c05965e454e77b # Parent 2c7786d7d14e15901566a67e35650f493c1c960e Allow deletion of (orphaned) Branches diff -r 2c7786d7d14e -r c00f20bd90ba farol/producttree.py --- 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//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/') @document_required @producttree_required diff -r 2c7786d7d14e -r c00f20bd90ba farol/templates/producttree/view.j2 --- 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() %} -

{{ branch._type}}: {{ branch._name }} (edit)

+
{{ branch._type}}: {{ branch._name }} (edit){% if branch.isOrphaned() %}{{ delete_button(url_for('.del_branch', path=branch.getPath() | join('/'))) }}{% endif %}
{% if branch._product %}

{{ branch._product._name }}

{% else %} diff -r 2c7786d7d14e -r c00f20bd90ba tests/testBranches.py --- 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('edit', 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) +