Skip to content

Commit

Permalink
Issue #1193. Create a get_form helper method to share between views.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Taylor committed Oct 13, 2016
1 parent b1b1d3a commit 98c6910
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
9 changes: 9 additions & 0 deletions webcompat/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from flask import request
from flask import session
from flask import url_for
from form import IssueForm
from functools import wraps
from ua_parser import user_agent_parser

Expand Down Expand Up @@ -143,6 +144,14 @@ def get_browser_name(user_agent_string=None):
return get_browser(user_agent_string).rsplit(' ', 1)[0].lower()
return "unknown"

def get_form(ua_header):
"""Return an instance of flask_wtf.Form with browser and os info added on"""
bug_form = IssueForm()
# add browser and version to bug_form object data
bug_form.browser.data = get_browser(ua_header)
bug_form.os.data = get_os(ua_header)
return bug_form


def get_os(user_agent_string=None):
'''Return operating system name.
Expand Down
15 changes: 3 additions & 12 deletions webcompat/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@
from flask import url_for

from form import AUTH_REPORT
from form import IssueForm
from form import PROXY_REPORT
from helpers import get_browser
from helpers import get_browser_name
from helpers import get_os
from helpers import get_form
from helpers import get_referer
from helpers import get_user_info
from helpers import set_referer
Expand Down Expand Up @@ -123,11 +121,8 @@ def file_issue():
@app.route('/', methods=['GET', 'POST'])
def index():
'''Main view where people come to report issues.'''
bug_form = IssueForm()
# add browser and version to bug_form object data
ua_header = request.headers.get('User-Agent')
bug_form.browser.data = get_browser(ua_header)
bug_form.os.data = get_os(ua_header)
bug_form = get_form(ua_header)
# browser_name is used in topbar.html to show the right add-on link
browser_name = get_browser_name(ua_header)
# GET means you want to file a report.
Expand Down Expand Up @@ -163,11 +158,7 @@ def create_issue():
POST will create a new issue
"""
if request.method == 'GET':
bug_form = IssueForm()
# add browser and version to bug_form object data
ua_header = request.headers.get('User-Agent')
bug_form.browser.data = get_browser(ua_header)
bug_form.os.data = get_os(ua_header)
bug_form = get_form(request.headers.get('User-Agent'))
if g.user:
get_user_info()
return render_template('new-issue.html', form=bug_form)
Expand Down

0 comments on commit 98c6910

Please sign in to comment.