From d8f81dd5f356046f34455b6d3f096b200692cea5 Mon Sep 17 00:00:00 2001
From: kaiyou <pierre@jaury.eu>
Date: Fri, 20 Mar 2020 15:31:06 +0100
Subject: [PATCH] Support switching application type

---
 .../templates/application_pick.html           |  7 ++--
 hiboo/service/admin.py                        | 40 +++++++++++++++----
 hiboo/service/templates/service_details.html  |  1 +
 hiboo/service/templates/service_list.html     |  2 +-
 4 files changed, 38 insertions(+), 12 deletions(-)

diff --git a/hiboo/application/templates/application_pick.html b/hiboo/application/templates/application_pick.html
index 2e5dc67..8e87f3b 100644
--- a/hiboo/application/templates/application_pick.html
+++ b/hiboo/application/templates/application_pick.html
@@ -1,7 +1,6 @@
 {% extends "base.html" %}
 
-{% block title %}{% trans %}Create a service{% endtrans %}{% endblock %}
-{% block subtitle %}{% trans %}pick an application{% endtrans %}{% endblock %}
+{% block title %}{% trans %}Select application type{% endtrans %}{% endblock %}
 
 {% block content %}
 <div class="row">
@@ -9,8 +8,8 @@
 <div class="col-md-4 col-s-6 col-xs-12">
   <div class="box box-widget widget-user-2">
     <div class="widget-user-header bg-{{ macros.colors[loop.index0 % 7] }}">
-      <a href="{{ url_for(route, application_id=application_id) }}" style="opacity: 0.8" class="btn btn-lg bg-gray text-black pull-right">
-        {% trans %}Create{% endtrans %}
+      <a href="{{ url_for(route, application_id=application_id, **kwargs) }}" style="opacity: 0.8" class="btn btn-lg bg-gray text-black pull-right">
+        {% trans %}Select{% endtrans %}
       </a>
       <h3 class="widget-header-username">{{ application.name }}</h3>
       <h5 class="widget-header-desc">{{ application.__doc__ }}&nbsp;</h5>
diff --git a/hiboo/service/admin.py b/hiboo/service/admin.py
index e721951..bd41a14 100644
--- a/hiboo/service/admin.py
+++ b/hiboo/service/admin.py
@@ -14,15 +14,19 @@ def list():
 
 
 @blueprint.route("/create")
+@security.admin_required()
+def create_select():
+    return flask.render_template(
+        "application_pick.html",
+        applications=application.registry,
+        route="service.create",
+        kwargs={}
+    )
+
+
 @blueprint.route("/create/<application_id>", methods=["GET", "POST"])
 @security.admin_required()
-def create(application_id=None):
-    if application_id is None:
-        return flask.render_template(
-            "application_pick.html",
-            applications=application.registry,
-            route="service.create"
-        )
+def create(application_id):      
     app = application.registry.get(application_id) or flask.abort(404)
     form = app.Form()
     if form.validate_on_submit():
@@ -72,3 +76,25 @@ def delete(service_uuid):
     models.db.session.delete(service)
     models.db.session.commit()
     return flask.redirect(flask.url_for(".list"))
+
+
+@blueprint.route("/setapp/<service_uuid>")
+@security.admin_required()
+def setapp_select(service_uuid):
+    return flask.render_template(
+        "application_pick.html",
+        applications=application.registry,
+        route="service.setapp",
+        kwargs={"service_uuid": service_uuid}
+    )
+
+
+@blueprint.route("/setapp/<service_uuid>/<application_id>", methods=["GET", "POST"])
+@security.admin_required()
+@security.confirmation_required("change the service application template")
+def setapp(service_uuid, application_id):
+    service = models.Service.query.get(service_uuid) or flask.abort(404)
+    app = application.registry.get(application_id) or flask.abort(404)
+    service.application_id = application_id
+    models.db.session.commit()
+    return flask.redirect(flask.url_for(".edit", service_uuid=service_uuid))
\ No newline at end of file
diff --git a/hiboo/service/templates/service_details.html b/hiboo/service/templates/service_details.html
index 3a9963d..f54b44a 100644
--- a/hiboo/service/templates/service_details.html
+++ b/hiboo/service/templates/service_details.html
@@ -43,6 +43,7 @@
 
 {% block actions %}
 <a href="{{ url_for("profile.list_for_service", service_uuid=service.uuid) }}" class="btn btn-primary">{% trans %}View profiles{% endtrans %}</a>
+<a href="{{ url_for(".setapp_select", service_uuid=service.uuid) }}" class="btn btn-primary">{% trans %}Change application{% endtrans %}</a>
 <a href="{{ url_for(".edit", service_uuid=service.uuid) }}" class="btn btn-primary">{% trans %}Edit this service{% endtrans %}</a>
 <a href="{{ url_for(".delete", service_uuid=service.uuid) }}" class="btn btn-danger">{% trans %}Delete this service{% endtrans %}</a>
 {% endblock %}
diff --git a/hiboo/service/templates/service_list.html b/hiboo/service/templates/service_list.html
index d6bc62b..615ada8 100644
--- a/hiboo/service/templates/service_list.html
+++ b/hiboo/service/templates/service_list.html
@@ -38,5 +38,5 @@
 {% endblock %}
 
 {% block actions %}
-<a href="{{ url_for(".create") }}" class="btn btn-success">{% trans %}Create a service{% endtrans %}</a>
+<a href="{{ url_for(".create_select") }}" class="btn btn-success">{% trans %}Create a service{% endtrans %}</a>
 {% endblock %}
-- 
GitLab