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