Mercurial > farol > farol
annotate farol/document.py @ 7:8f41bb7f4681
Move the Document management routines to a document Blueprint
author | Benoît Allard <benoit.allard@greenbone.net> |
---|---|
date | Thu, 25 Sep 2014 17:03:35 +0200 |
parents | farol/main.py@3478e20885fd |
children | fbc413b8a46e |
rev | line source |
---|---|
0 | 1 # -*- encoding: utf-8 -*- |
2 # Description: | |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
3 # The Document Blueprint |
0 | 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 | |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
25 from flask import (Blueprint, render_template, abort, redirect, request, |
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
26 url_for) |
0 | 27 |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
28 from farolluz.parsers.cvrf import parseDate, parseVersion |
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
29 from farolluz.cvrf import (CVRFNote, CVRFReference, CVRFAcknowledgment, |
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
30 CVRFPublisher, CVRFTracking, CVRFTrackingID, CVRFGenerator, CVRFRevision, |
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
31 CVRFAggregateSeverity) |
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
32 from farolluz.renderer import utcnow |
0 | 33 |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
34 from .session import document_required, get_current |
0 | 35 |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
36 |
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
37 document = Blueprint('document', __name__) |
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
38 |
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
39 @document.route('/') |
0 | 40 @document_required |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
41 def view(): |
0 | 42 cvrf = get_current() |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
43 return render_template('document/view.j2', cvrf=cvrf) |
0 | 44 |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
45 @document.route('/title/edit', methods=['GET', 'POST']) |
0 | 46 @document_required |
47 def edit_title(): | |
48 if request.method != 'POST': | |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
49 return render_template('document/edit_title.j2', title = get_current()._title, _type = get_current()._type) |
0 | 50 |
51 | |
52 get_current()._title = request.form['title'] | |
53 get_current()._type = request.form['type'] | |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
54 return redirect(url_for('.view')) |
0 | 55 |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
56 @document.route('/publisher/edit', methods=['GET', 'POST']) |
0 | 57 @document_required |
58 def edit_publisher(): | |
59 if request.method != 'POST': | |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
60 return render_template('document/edit_publisher.j2', publisher = get_current()._publisher or CVRFPublisher(''), types=CVRFPublisher.TYPES) |
0 | 61 |
62 publisher = CVRFPublisher(request.form['type'], request.form['vendorid'] or None) | |
63 publisher.setContact(request.form['contact'] or None) | |
64 publisher.setAuthority(request.form['authority'] or None) | |
65 get_current().setPublisher(publisher) | |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
66 return redirect(url_for('.view')) |
0 | 67 |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
68 @document.route('/tracking/edit', methods=['GET', 'POST']) |
0 | 69 @document_required |
70 def edit_tracking(): | |
71 wasNone = False | |
72 tracking = get_current()._tracking | |
73 if tracking is None: | |
74 wasNone = True | |
75 tracking = CVRFTracking(CVRFTrackingID(''), 'Draft', (0,), utcnow(), utcnow()) | |
76 generator = tracking._generator | |
77 if not tracking._generator: | |
78 generator = CVRFGenerator() | |
79 if request.method != 'POST': | |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
80 return render_template('document/edit_tracking.j2', tracking=tracking, version='.'.join('%s'%v for v in tracking._version), generator=generator, now=utcnow(), statuses=tracking.STATUSES) |
0 | 81 |
82 tracking._identification._id = request.form['id'] | |
83 aliases = [] | |
84 if request.form['id_aliases']: | |
85 aliases = [a.strip() for a in request.form['id_aliases'].split(',')] | |
86 tracking._identification._aliases = aliases | |
87 tracking._status = request.form['status'] | |
88 tracking._version = parseVersion(request.form['version']) | |
89 tracking._initialDate = parseDate(request.form['initial']) | |
90 tracking._currentDate = parseDate(request.form['current']) | |
91 if wasNone: | |
92 get_current().setTracking(tracking) | |
93 if (not request.form['gen_engine']) and (not request.form['gen_date']): | |
94 generator = None | |
95 else: | |
96 generator._engine = request.form['gen_engine'] or None | |
97 if request.form['gen_date']: | |
98 generator._date = parseDate(request.form['gen_date']) | |
99 else: | |
100 generator._date = None | |
101 tracking.setGenerator(generator) | |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
102 return redirect(url_for('.view')) |
0 | 103 |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
104 @document.route('/revision/<int:index>/edit', methods=['GET', 'POST']) |
0 | 105 @document_required |
106 def edit_revision(index): | |
107 cvrf = get_current() | |
108 if cvrf._tracking is None: | |
109 abort(404) | |
110 try: | |
111 revision = cvrf._tracking._history[index] | |
112 except IndexError: | |
113 abort(404) | |
114 if request.method != 'POST': | |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
115 return render_template('document/edit_revision.j2', number='.'.join('%s'%v for v in revision._number), date=revision._date, description=revision._description, action='Update') |
0 | 116 |
117 revision._number = parseVersion(request.form['number']) | |
118 revision._date = parseDate(request.form['date']) | |
119 revision._description = request.form['description'] | |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
120 return redirect(url_for('.view')) |
0 | 121 |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
122 @document.route('/revision/add', methods=['GET', 'POST']) |
0 | 123 @document_required |
124 def add_revision(): | |
125 tracking = get_current()._tracking | |
126 if request.method != 'POST': | |
127 version = tracking._version | |
128 version = version[:-1] + (version[-1] + 1,) | |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
129 return render_template('document/edit_revision.j2', number='.'.join("%d"%v for v in version), date=utcnow(), action='Add') |
0 | 130 |
131 version = parseVersion(request.form['number']) | |
132 date = parseDate(request.form['date']) | |
133 revision = CVRFRevision(version, date, request.form['description']) | |
134 tracking.addRevision(revision) | |
135 if 'update_tracking' in request.form: | |
136 tracking._version = version | |
137 tracking._currentDate = date | |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
138 return redirect(url_for('.view')) |
0 | 139 |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
140 @document.route('/distribution/edit', methods=['GET', 'POST']) |
0 | 141 @document_required |
142 def edit_distribution(): | |
143 if request.method != 'POST': | |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
144 return render_template('document/edit_distribution.j2', distribution=get_current()._distribution) |
0 | 145 |
146 get_current().setDistribution(request.form['distribution']) | |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
147 return redirect(url_for('.view')) |
0 | 148 |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
149 @document.route('/severity/edit', methods=['GET', 'POST']) |
0 | 150 @document_required |
151 def edit_severity(): | |
152 severity = get_current()._aggregateseverity | |
153 if severity is None: | |
154 severity = CVRFAggregateSeverity('') | |
155 if request.method != 'POST': | |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
156 return render_template('document/edit_severity.j2', severity=severity) |
0 | 157 if not request.form['severity']: |
158 severity = None | |
159 else: | |
160 severity._severity = request.form['severity'] | |
161 severity.setNamespace(request.form['namespace'] or None) | |
162 get_current().setAggregateSeverity(severity) | |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
163 return redirect(url_for('.view')) |
0 | 164 |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
165 @document.route('/note/<int:ordinal>') |
0 | 166 @document_required |
167 def view_note(ordinal): | |
168 note = get_current().getNote(ordinal) | |
169 if note is None: | |
170 abort(404) | |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
171 return render_template('document/view_note.j2', note=note) |
0 | 172 |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
173 @document.route('/note/<int:ordinal>/edit', methods=['GET', 'POST']) |
0 | 174 @document_required |
175 def edit_note(ordinal): | |
176 note = get_current().getNote(ordinal) | |
177 if note is None: | |
178 abort(404) | |
179 if request.method != 'POST': | |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
180 return render_template('document/edit_note.j2', note=note, types = note.TYPES) |
0 | 181 |
182 note._type = request.form['type'] | |
183 note._ordinal = int(request.form['ordinal']) | |
184 note._note = request.form['note'] | |
185 note._title = request.form['title'] or None | |
186 note._audience = request.form['audience'] or None | |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
187 return redirect(url_for('.view_note', ordinal=note._ordinal )) |
0 | 188 |
189 | |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
190 @document.route('/note/add', methods=['GET', 'POST']) |
0 | 191 @document_required |
192 def add_note(): | |
193 if request.method != 'POST': | |
194 next_ordinal = 1 | |
195 notes = get_current()._notes | |
196 if notes: | |
197 next_ordinal = notes[-1]._ordinal + 1 | |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
198 return render_template('document/edit_note.j2', ordinal=next_ordinal, types=CVRFNote.TYPES, action='Add') |
0 | 199 |
200 title = None | |
201 audience = None | |
202 title = request.form['title'] or None | |
203 audience = request.form['audience'] or None | |
204 | |
205 note = CVRFNote(request.form['type'], int(request.form['ordinal']), request.form['note'], title, audience) | |
206 get_current().addNote(note) | |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
207 return redirect(url_for('.view')) |
0 | 208 |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
209 @document.route('/reference/<int:index>/edit', methods=['GET', 'POST']) |
0 | 210 @document_required |
211 def edit_reference(index): | |
212 try: | |
213 ref = get_current()._references[index] | |
214 except IndexError: | |
215 abort(404) | |
216 if request.method != 'POST': | |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
217 return render_template('document/edit_reference.j2', _type=ref._type, url=ref._url, description=ref._description, types=('',) + ref.TYPES) |
0 | 218 |
219 ref._type = request.form['type'] or None | |
220 ref._url = request.form['url'] | |
221 ref._description = request.form['description'] | |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
222 return redirect(url_for('.view')) |
0 | 223 |
224 | |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
225 @document.route('/reference/add', methods=['GET', 'POST']) |
0 | 226 @document_required |
227 def add_reference(): | |
228 if request.method != 'POST': | |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
229 return render_template('document/edit_reference.j2', action='Add', types=('',) + CVRFReference.TYPES) |
0 | 230 |
231 ref = CVRFReference(request.form['url'], request.form['description'], request.form['type'] or None) | |
232 get_current().addReference(ref) | |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
233 return redirect(url_for('.view')) |
0 | 234 |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
235 @document.route('/acknowledgment/<int:index>') |
0 | 236 @document_required |
237 def view_acknowledgment(index): | |
238 try: | |
239 ack = get_current()._acknowledgments[index] | |
240 except IndexError: | |
241 abort(404) | |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
242 return render_template('document/view_acknowledgment.j2', acknowledgment=ack, index=index, action='Update') |
0 | 243 |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
244 @document.route('/acknowledgment/<int:index>/edit', methods=['GET', 'POST']) |
0 | 245 @document_required |
246 def edit_acknowledgment(index): | |
247 try: | |
248 ack = get_current()._acknowledgments[index] | |
249 except IndexError: | |
250 abort(404) | |
251 if request.method != 'POST': | |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
252 return render_template('document/edit_acknowledgment.j2', name=ack._name, organization=ack._organization, description=ack._description, url=ack._url, action='Update') |
0 | 253 |
254 ack._name = request.form['name'] or None | |
255 ack._organization = request.form['organization'] or None | |
256 ack._description = request.form['description'] or None | |
257 ack._url = request.form['url'] or None | |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
258 return redirect(url_for('.view')) |
0 | 259 |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
260 @document.route('/acknowledgment/add', methods=['GET', 'POST']) |
0 | 261 @document_required |
262 def add_acknowledgment(): | |
263 if request.method != 'POST': | |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
264 return render_template('document/edit_acknowledgment.j2', action='Add') |
0 | 265 |
266 ack = CVRFAcknowledgment() | |
267 ack._name = request.form['name'] or None | |
268 ack._organization = request.form['organization'] or None | |
269 ack._description = request.form['description'] or None | |
270 ack._url = request.form['url'] or None | |
271 get_current().addAcknowledgment(ack) | |
7
8f41bb7f4681
Move the Document management routines to a document Blueprint
Benoît Allard <benoit.allard@greenbone.net>
parents:
3
diff
changeset
|
272 return redirect(url_for('.view')) |