changeset 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 4ac1dd522998
children 64403f1b424e
files farol/controller.py farol/document.py farol/vulnerability.py
diffstat 3 files changed, 52 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/farol/controller.py	Wed Oct 08 11:34:11 2014 +0200
@@ -0,0 +1,46 @@
+# -*- encoding: utf-8 -*-
+# Description:
+# Common controller Web stuffs
+#
+# Authors:
+# BenoƮt Allard <benoit.allard@greenbone.net>
+#
+# Copyright:
+# Copyright (C) 2014 Greenbone Networks GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+
+"""\
+Some common routines for handling of Notes, Acknowledgments and Reference that
+is shared for the Document and the Vulnerabilities.
+"""
+
+from flask import request
+
+from farolluz.cvrf import CVRFNote
+
+def update_note_from_request(note):
+    note._type = request.form['type']
+    note._ordinal = int(request.form['ordinal'])
+    note._note = request.form['note']
+    note._title = request.form['title'] or None
+    note._audience = request.form['audience'] or None
+
+def create_note_from_request():
+    title = request.form['title'] or None
+    audience = request.form['audience'] or None
+
+    return CVRFNote(request.form['type'], int(request.form['ordinal']),
+                    request.form['note'], title, audience)
--- a/farol/document.py	Wed Oct 08 10:22:14 2014 +0200
+++ b/farol/document.py	Wed Oct 08 11:34:11 2014 +0200
@@ -31,6 +31,7 @@
     CVRFAggregateSeverity)
 from farolluz.renderer import utcnow
 
+from .controller import update_note_from_request, create_note_from_request
 from .session import document_required, get_current
 
 
@@ -179,11 +180,7 @@
     if request.method != 'POST':
         return render_template('document/edit_note.j2', note=note, types = note.TYPES)
 
-    note._type = request.form['type']
-    note._ordinal = int(request.form['ordinal'])
-    note._note = request.form['note']
-    note._title = request.form['title'] or None
-    note._audience = request.form['audience'] or None
+    update_note_from_request(note)
     return redirect(url_for('.view_note', ordinal=note._ordinal ))
 
 
@@ -197,12 +194,7 @@
             next_ordinal = notes[-1]._ordinal + 1
         return render_template('document/edit_note.j2', ordinal=next_ordinal, types=CVRFNote.TYPES, action='Add')
 
-    title = None
-    audience = None
-    title = request.form['title'] or None
-    audience = request.form['audience'] or None
-
-    note = CVRFNote(request.form['type'], int(request.form['ordinal']), request.form['note'], title, audience)
+    note = create_note_from_request()
     get_current().addNote(note)
     return redirect(url_for('.view'))
 
--- a/farol/vulnerability.py	Wed Oct 08 10:22:14 2014 +0200
+++ b/farol/vulnerability.py	Wed Oct 08 11:34:11 2014 +0200
@@ -31,6 +31,7 @@
     CVRFProductStatus, CVRFCVSSSet, CVRFRemediation)
 from farolluz.renderer import utcnow
 
+from .controller import update_note_from_request, create_note_from_request
 from .session import document_required, get_current
 
 
@@ -124,11 +125,7 @@
     if request.method != 'POST':
         return render_template('vulnerability/edit_note.j2', note=note, ordinal=ordinal, types=note.TYPES)
 
-    note._type = request.form['type']
-    note._ordinal = int(request.form['ordinal'])
-    note._note = request.form['note']
-    note._title = request.form['title'] or None
-    note._audience = request.form['audience'] or None
+    update_note_from_request(note)
     return redirect(url_for('.view_note', ordinal=ordinal, note_ordinal=note._ordinal))
 
 @vulnerability.route('/<int:ordinal>/note/add', methods=['GET', 'POST'])
@@ -141,10 +138,7 @@
             next_ordinal = notes[-1]._ordinal + 1
         return render_template('vulnerability/edit_note.j2', ordinal=ordinal, note_ordinal=next_ordinal, types=CVRFNote.TYPES, action='Add')
 
-    title = request.form['title'] or None
-    audience = request.form['audience'] or None
-
-    note = CVRFNote(request.form['type'], int(request.form['ordinal']), request.form['note'], title, audience)
+    note = create_note_from_request()
     get_vuln(ordinal).addNote(note)
     return redirect(url_for('.view', ordinal=ordinal))
 

http://farol.wald.intevation.org