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