Make a nice function to reassemble a splitted URL into an isp.json URL
authorGu1 <gu1@cafai.fr>
Sun, 29 Dec 2013 14:47:09 +0000 (15:47 +0100)
committerGu1 <gu1@cafai.fr>
Sun, 29 Dec 2013 14:50:21 +0000 (15:50 +0100)
Death to code duplication. together we can win this fight !
Thx guerby <3

ffdnispdb/utils.py
ffdnispdb/views.py

index 8e9cf9a..d096961 100644 (file)
@@ -4,6 +4,7 @@ from flask import current_app
 from flask.globals import _request_ctx_stack
 from collections import OrderedDict
 from datetime import datetime
+from urlparse import urlunsplit
 import pytz
 import json
 import sys
@@ -114,3 +115,17 @@ def stream_with_ctx_and_exc(generator_or_function):
     wrapped_g = generator()
     next(wrapped_g)
     return wrapped_g
+
+
+def make_ispjson_url(split_url):
+    """
+    Take a tuple as returned by urlsplit and return the
+    isp.json url for that domain
+
+    >>> from urlparse import urlsplit
+    >>> make_ispjson_url(urlsplit('http://isp.com'))
+    'http://isp.com/isp.json'
+    """
+    u = list(split_url)
+    u[2] = '/isp.json'  # new path
+    return urlunsplit(u)
index b1beb97..7790434 100644 (file)
@@ -10,7 +10,6 @@ import docutils.core
 import ispformat.specs
 
 from datetime import datetime
-from urlparse import urlunsplit
 import locale
 locale.setlocale(locale.LC_ALL, '')
 from time import time
@@ -168,9 +167,7 @@ def edit_project(projectid):
         form = forms.ProjectJSONForm(obj=isp)
         if form.validate_on_submit():
             isp.tech_email = form.tech_email.data
-            u = list(form.json_url.data)
-            u[2] = '/isp.json'  # new path
-            url = urlunsplit(u)
+            url = utils.make_ispjson_url(form.json_url.data)
             isp.json_url = url
 
             db.session.add(isp)
@@ -263,9 +260,7 @@ def json_url_validator():
 def create_project_json():
     form = forms.ProjectJSONForm()
     if form.validate_on_submit():
-        u = list(form.json_url.data)
-        u[2] = '/isp.json'  # new path
-        url = urlunsplit(u)
+        url = utils.make_ispjson_url(form.json_url.data)
         session['form_json'] = {'url': url, 'tech_email': form.tech_email.data}
         return render_template('project_json_validator.html')
     return render_template('add_project_json_form.html', form=form)