Browse Source

Allow switching between manual and isp.json updates.

Fixes #1
pitchum 6 years ago
parent
commit
4d3c9822fa

+ 7 - 0
ffdnispdb/templates/project_form_generic.html

@@ -1,6 +1,13 @@
 {% extends "layout.html" %}
 {% import "form_macros.html" as fm %}
 {% block container %}
+
+{% if isp %}
+<ul class="nav nav-tabs">
+  <li role="presentation" class="active"><a href="#">{{ _("Manual updates") }}</a></li>
+  <li role="presentation"><a href="{{ url_for('.edit_project_auto_update', projectid=isp.id) }}">{{ _("Automatic updates (isp.json)") }}</a></li>
+</ul>
+{% endif %}
 <div class="row">
   <div class="span11 well">
     <form method="post" class="form-horizontal">

+ 8 - 1
ffdnispdb/templates/project_json_form_generic.html

@@ -1,14 +1,21 @@
 {% extends "layout.html" %}
 {% import "form_macros.html" as fm %}
 {% block container %}
+
+{% if isp %}
+<ul class="nav nav-tabs">
+  <li role="presentation"><a href="{{ url_for('.edit_project', projectid=isp.id) }}">{{ _("Manual updates") }}</a></li>
+  <li role="presentation" class="active"><a href="#">{{ _("Automatic updates (isp.json)") }}</a></li>
+</ul>
+{% endif %}
 <div class="row">
   <div class="span11 well">
     <form method="post" class="form-horizontal">
       {{ form.csrf_token }}
       <fieldset>
         <legend>{{ page_title }}</legend>
-        {{ fm.render_field(form.json_url) }}
         {{ fm.render_field(form.tech_email) }}
+        {{ fm.render_field(form.json_url) }}
         <div class="form-actions">
           <input type="submit" class="btn btn-primary" value="{{ _("Next") }}" />
           <input type="reset" class="btn" value="{{ _("Cancel") }}" />

BIN
ffdnispdb/translations/fr/LC_MESSAGES/messages.mo


+ 10 - 0
ffdnispdb/translations/fr/LC_MESSAGES/messages.po

@@ -382,6 +382,16 @@ msgstr ""
 msgid "That JSON thing is too cool for me"
 msgstr "Le JSON c'est trop fort pour moi"
 
+#: ffdnispdb/templates/project_form_generic.html:7
+#: ffdnispdb/templates/project_json_form_generic.html:7
+msgid "Manual updates"
+msgstr "Mises à jour manuelles"
+
+#: ffdnispdb/templates/project_form_generic.html:8
+#: ffdnispdb/templates/project_json_form_generic.html:8
+msgid "Automatic updates (isp.json)"
+msgstr "Mises à jour automatiques (isp.json)"
+
 #: ffdnispdb/templates/add_project_form.html:1
 msgid "Add a new project"
 msgstr "Ajouter un nouveau projet"

+ 50 - 25
ffdnispdb/views.py

@@ -178,32 +178,57 @@ def edit_project(projectid):
     elif (sess_token is None or (datetime.utcnow() - sess_token).total_seconds() > MAX_TOKEN_AGE):
         return redirect(url_for('.gen_edit_token', projectid=isp.id))
 
-    if isp.is_local:
-        form = forms.ProjectForm.edit_json(isp)
-        if form.validate_on_submit():
-            isp.name = form.name.data
-            isp.shortname = form.shortname.data or None
-            isp.json = form.to_json(isp.json)
-            isp.tech_email = form.tech_email.data
-
-            db.session.add(isp)
-            db.session.commit()
-            flash(_(u'Project modified'), 'info')
-            return redirect(url_for('.project', projectid=isp.id))
-        return render_template('edit_project_form.html', form=form)
-    else:
-        form = forms.ProjectJSONForm(obj=isp)
-        if form.validate_on_submit():
-            isp.tech_email = form.tech_email.data
-            url = utils.make_ispjson_url(form.json_url.data)
-            isp.json_url = url
-
-            db.session.add(isp)
-            db.session.commit()
-            flash(_(u'Project modified'), 'info')
-            return redirect(url_for('.project', projectid=isp.id))
-        return render_template('edit_project_json_form.html', form=form)
+    form = forms.ProjectForm.edit_json(isp)
+    if form.validate_on_submit():
+        isp.name = form.name.data
+        isp.shortname = form.shortname.data or None
+        isp.json = form.to_json(isp.json)
+        isp.tech_email = form.tech_email.data
+        isp.json_url = None
+
+        db.session.add(isp)
+        db.session.commit()
+        flash(_(u'Project modified'), 'info')
+        return redirect(url_for('.project', projectid=isp.id))
+    return render_template('edit_project_form.html', form=form, isp=isp)
+
 
+@ispdb.route('/isp/<projectid>/edit_json_url', methods=['GET', 'POST'])
+def edit_project_auto_update(projectid):
+    MAX_TOKEN_AGE = 3600
+    isp = ISP.query.filter_by(id=projectid, is_disabled=False).first_or_404()
+    sess_token = session.get('edit_tokens', {}).get(isp.id)
+
+    if 'token' in request.args:
+        s = itsdangerous.URLSafeTimedSerializer(current_app.secret_key, salt='edit')
+        try:
+            r = s.loads(request.args['token'], max_age=MAX_TOKEN_AGE,
+                        return_timestamp=True)
+        except:
+            abort(403)
+
+        if r[0] != isp.id:
+            abort(403)
+
+        tokens = session.setdefault('edit_tokens', {})
+        session.modified = True  # ITS A TARP
+        tokens[r[0]] = r[1]
+        # refresh page, without the token in the url
+        return redirect(url_for('.edit_project', projectid=r[0]))
+    elif (sess_token is None or (datetime.utcnow() - sess_token).total_seconds() > MAX_TOKEN_AGE):
+        return redirect(url_for('.gen_edit_token', projectid=isp.id))
+
+    form = forms.ProjectJSONForm(obj=isp)
+    if form.validate_on_submit():
+        isp.tech_email = form.tech_email.data
+        url = utils.make_ispjson_url(form.json_url.data)
+        isp.json_url = url
+
+        db.session.add(isp)
+        db.session.commit()
+        flash(_(u'Project modified'), 'info')
+        return redirect(url_for('.project', projectid=isp.id))
+    return render_template('edit_project_json_form.html', form=form, isp=isp)
 
 @ispdb.route('/isp/<projectid>/gen_edit_token', methods=['GET', 'POST'])
 def gen_edit_token(projectid):