Mercurial > farol > farol
comparison farol/producttree.py @ 0:4a9f23230eba
Initial Release
author | Benoît Allard <benoit.allard@greenbone.net> |
---|---|
date | Wed, 24 Sep 2014 10:07:49 +0200 |
parents | |
children | dbfe89e3c6fe |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4a9f23230eba |
---|---|
1 # -*- encoding: utf-8 -*- | |
2 # Description: | |
3 # Web stuff about the ProductTree | |
4 # | |
5 # Authors: | |
6 # BenoƮt Allard <benoit.allard@greenbone.net> | |
7 # | |
8 # Copyright: | |
9 # Copyright (C) 2014 Greenbone Networks GmbH | |
10 # | |
11 # This program is free software; you can redistribute it and/or | |
12 # modify it under the terms of the GNU General Public License | |
13 # as published by the Free Software Foundation; either version 2 | |
14 # of the License, or (at your option) any later version. | |
15 # | |
16 # This program is distributed in the hope that it will be useful, | |
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 # GNU General Public License for more details. | |
20 # | |
21 # You should have received a copy of the GNU General Public License | |
22 # along with this program; if not, write to the Free Software | |
23 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
24 | |
25 """\ | |
26 Web-stuff about the producttree | |
27 """ | |
28 | |
29 from functools import wraps | |
30 | |
31 from flask import Blueprint, render_template, redirect, url_for, abort, request, flash | |
32 | |
33 from farolluz.cvrf import (CVRFProductBranch, CVRFFullProductName, | |
34 CVRFRelationship, CVRFGroup) | |
35 from .session import document_required, get_current | |
36 | |
37 producttree = Blueprint('producttree', __name__) | |
38 | |
39 def producttree_required(f): | |
40 @wraps(f) | |
41 def decorated_function(*args, **kwargs): | |
42 if get_current()._producttree is None: | |
43 flash('Operation invalid without producttree', 'warning') | |
44 return redirect(url_for('document')) | |
45 return f(*args, **kwargs) | |
46 return decorated_function | |
47 | |
48 @producttree.route('/create', methods=['POST']) | |
49 @document_required | |
50 def create(): | |
51 get_current().createProductTree() | |
52 return redirect(url_for('document')) | |
53 | |
54 | |
55 @producttree.route('/branch/<path:path>/edit', methods=['GET', 'POST']) | |
56 @document_required | |
57 @producttree_required | |
58 def edit_branch(path): | |
59 path = [int(p) for p in path.split('/')] | |
60 cvrf = get_current() | |
61 ptree = cvrf._producttree | |
62 try: | |
63 branch = cvrf._producttree.getBranch(path) | |
64 except ValueError: | |
65 abort(404) | |
66 if request.method != 'POST': | |
67 branches = [('', '')] + [(b.getName(), b.getPath(True)) for b in ptree.getNotTerminalBranches(branch)] | |
68 return render_template('producttree/edit_branch.j2', branch=branch, branches=branches, types=branch.TYPES) | |
69 | |
70 pbranch = ptree | |
71 if request.form['parent']: | |
72 pbranch = ptree.getBranch([int(p) for p in request.form['parent'].split('/')]) | |
73 | |
74 if pbranch is not branch.getParent(): | |
75 # We have to 're-link' the element ... | |
76 # 1. unlink | |
77 branch.unlink() | |
78 # 2. set the new parent | |
79 branch._parentbranch = pbranch | |
80 # 3. add to the Tree (again) | |
81 ptree.addBranch(branch) | |
82 | |
83 branch._type = request.form['type'] | |
84 branch._name = request.form['name'] | |
85 return redirect(url_for('document')) | |
86 | |
87 @producttree.route('/branch/add', methods=['GET', 'POST']) | |
88 @document_required | |
89 @producttree_required | |
90 def add_branch(): | |
91 cvrf = get_current() | |
92 ptree = cvrf._producttree | |
93 if request.method != 'POST': | |
94 branches = [('', '')] + [(b.getName(), b.getPath(True)) for b in ptree.getNotTerminalBranches()] | |
95 return render_template('producttree/edit_branch.j2', branch=CVRFProductBranch('', '', ptree), action='Add', branches=branches, types=CVRFProductBranch.TYPES) | |
96 | |
97 pbranch = ptree | |
98 if request.form['parent']: | |
99 pbranch = ptree.getBranch([int(p) for p in request.form['parent'].split('/')]) | |
100 branch = CVRFProductBranch(request.form['type'], request.form['name'], pbranch) | |
101 ptree.addBranch(branch) | |
102 return redirect(url_for('document')) | |
103 | |
104 @producttree.route('/product/<productid>') | |
105 @document_required | |
106 @producttree_required | |
107 def view_product(productid): | |
108 cvrf = get_current() | |
109 try: | |
110 product = cvrf.getProductForID(productid) | |
111 except IndexError: | |
112 abort(404) | |
113 return render_template('producttree/view_product.j2', product=product, cvrf=cvrf) | |
114 | |
115 @producttree.route('/product/<productid>/edit', methods=['GET', 'POST']) | |
116 @document_required | |
117 @producttree_required | |
118 def edit_product(productid): | |
119 cvrf = get_current() | |
120 ptree = cvrf._producttree | |
121 try: | |
122 product = cvrf.getProductForID(productid) | |
123 except KeyError: | |
124 abort(404) | |
125 | |
126 if request.method != 'POST': | |
127 current_rel = '' | |
128 if product.isRelationship(): | |
129 current_rel = str(ptree._relationships.index(product.getCurrentRelationship())) | |
130 leaves = [('', '')] + [(b.getName(), b.getPath(True)) for b in ptree.getOrphanedBranches(product)] | |
131 rels = [('', '')] + [(ptree.getNameOfRelationship(r), str(i)) for i, r in ptree.getOrphanedRelationships(product)] | |
132 return render_template('producttree/edit_product.j2', product=product, action='Update', orphaned_leaves=leaves, orphaned_relationships=rels, current_rel=current_rel) | |
133 | |
134 if request.form['parent_branch'] and request.form['parent_relationship']: | |
135 flash('Cannot set a parent branch and parent relationship', 'danger') | |
136 return redirect(url_for('.edit_product', productid=productid)) | |
137 | |
138 oldp = product._parent | |
139 if request.form['parent_branch']: | |
140 pbranch = ptree.getBranch([int(p) for p in request.form['parent_branch'].split('/')]) | |
141 if pbranch is not oldp: | |
142 # Gonna be funny, needs re-link | |
143 product.unlink() | |
144 product._parent = pbranch | |
145 ptree.addProduct(product) | |
146 elif request.form['parent_relationship']: | |
147 prel = ptree._relationships[int(request.form['parent_relationship'])] | |
148 if prel is not oldp: | |
149 product.unlink() | |
150 product._parent = prel | |
151 ptree.addProduct(product) | |
152 else: | |
153 if ptree is not oldp: | |
154 product.unlink() | |
155 product._parent = ptree | |
156 ptree.addProduct(product) | |
157 | |
158 product._productid = request.form['productid'] | |
159 product._name = request.form['name'] | |
160 product._cpe = request.form['cpe'] or None | |
161 return redirect(url_for('document')) | |
162 | |
163 @producttree.route('/product/add', methods=['GET', 'POST']) | |
164 @document_required | |
165 @producttree_required | |
166 def add_product(): | |
167 cvrf = get_current() | |
168 ptree = cvrf._producttree | |
169 | |
170 if request.method != 'POST': | |
171 product=CVRFFullProductName('', '', ptree) | |
172 leaves = [('', '')] + [(b.getName(), b.getPath(True)) for b in ptree.getOrphanedBranches()] | |
173 rels = [('', '')] + [(ptree.getNameOfRelationship(r), str(i)) for i, r in ptree.getOrphanedRelationships()] | |
174 return render_template('producttree/edit_product.j2', product=product, action='Add', orphaned_leaves=leaves, orphaned_relationships=rels, current_rel='') | |
175 | |
176 if request.form['parent_branch'] and request.form['parent_relationship']: | |
177 flash('Cannot set a parent branch and parent relationship', 'danger') | |
178 return redirect(url_for('.edit_product', productid=productid)) | |
179 | |
180 parent = ptree | |
181 if request.form['parent_branch']: | |
182 parent = ptree.getBranch([int(p) for p in request.form['parent_branch'].split('/')]) | |
183 elif request.form['parent_relationship']: | |
184 parent = ptree._relationships[int(request.form['parent_relationship'])] | |
185 | |
186 product = CVRFFullProductName(request.form['productid'], request.form['name'], parent, request.form['cpe'] or None) | |
187 ptree.addProduct(product) | |
188 return redirect(url_for('document')) | |
189 | |
190 @producttree.route('/relationship/<int:index>/edit', methods=['GET', 'POST']) | |
191 @document_required | |
192 @producttree_required | |
193 def edit_relationship(index): | |
194 cvrf = get_current() | |
195 ptree = cvrf._producttree | |
196 try: | |
197 rel = ptree._relationships[index] | |
198 except IndexError: | |
199 abort(404) | |
200 | |
201 if request.method != 'POST': | |
202 return render_template('producttree/edit_relationship.j2', productreference=rel._productreference, relationtype=rel._relationtype, relatestoproductreference=rel._relatestoproductreference, types=rel.TYPES) | |
203 | |
204 rel._productreference = request.form['productreference'] | |
205 rel._relationtype = request.form['relationtype'] | |
206 rel._relatestoproductreference = request.form['relatestoproductreference'] | |
207 return redirect(url_for('document')) | |
208 | |
209 @producttree.route('/relationship/add', methods=['GET', 'POST']) | |
210 @document_required | |
211 @producttree_required | |
212 def add_relationship(): | |
213 cvrf = get_current() | |
214 ptree = cvrf._producttree | |
215 if len(ptree._products) < 2: | |
216 flash('You need to have at least two products to create a relationship', 'warning') | |
217 return redirect(url_for('.add_product')) | |
218 | |
219 if request.method != 'POST': | |
220 return render_template('producttree/edit_relationship.j2', action='Add', types=CVRFRelationship.TYPES) | |
221 | |
222 prodid1 = request.form['productreference'] | |
223 prodid2 = request.form['relatestoproductreference'] | |
224 | |
225 if prodid1 == prodid2: | |
226 flash('You cannot create a relationship with the same product twice', 'danger') | |
227 return redirect(url_for('.add_relationship')) | |
228 | |
229 rel = CVRFRelationship(prodid1, request.form['relationtype'], prodid2) | |
230 ptree.addRelationship(rel) | |
231 return redirect(url_for('document')) | |
232 | |
233 @producttree.route('/group/<groupid>/edit', methods=['GET', 'POST']) | |
234 @document_required | |
235 @producttree_required | |
236 def edit_group(groupid): | |
237 try: | |
238 group = get_current().getGroupForID(groupid) | |
239 except KeyError: | |
240 abort(404) | |
241 if request.method != 'POST': | |
242 return render_template('producttree/edit_group.j2', groupid=group._groupid, description=group._description, productids=group._productids) | |
243 | |
244 group._groupid = request.form['groupid'] | |
245 group.setDescription(request.form['description'] or None) | |
246 group._productids = [] | |
247 for productid in request.form.getlist('products'): | |
248 group.addProductID(productid) | |
249 return redirect(url_for('document')) | |
250 | |
251 @producttree.route('/group/add', methods=['GET', 'POST']) | |
252 @document_required | |
253 @producttree_required | |
254 def add_group(): | |
255 if request.method != 'POST': | |
256 return render_template('producttree/edit_group.j2', action='Add') | |
257 | |
258 group = CVRFGroup(request.form['groupid']) | |
259 group.setDescription(request.form['description'] or None) | |
260 for productid in request.form.getlist('products'): | |
261 group.addProductID(productid) | |
262 get_current()._producttree.addGroup(group) | |
263 return redirect(url_for('document')) |