From 124fdb972b318a6f25272b7788618debb562ca40 Mon Sep 17 00:00:00 2001
From: Benjamin Bertrand <benjamin.bertrand@esss.se>
Date: Mon, 2 Jul 2018 14:44:13 +0200
Subject: [PATCH] Add link to AWX job url

---
 app/models.py                     | 11 +++++++++++
 app/settings.py                   |  1 +
 app/static/js/tasks.js            | 26 ++++++++++++++++++++------
 app/task/views.py                 |  2 +-
 app/templates/task/view_task.html |  4 ++++
 5 files changed, 37 insertions(+), 7 deletions(-)

diff --git a/app/models.py b/app/models.py
index 2a83e8a..2f5597a 100644
--- a/app/models.py
+++ b/app/models.py
@@ -12,6 +12,7 @@ This module implements the models used in the app.
 import ipaddress
 import string
 import qrcode
+import urllib.parse
 import sqlalchemy as sa
 from enum import Enum
 from sqlalchemy.ext.declarative import declared_attr
@@ -969,6 +970,15 @@ class Task(db.Model):
     user_id = db.Column(db.Integer, db.ForeignKey('user_account.id'),
                         nullable=False, default=utils.fetch_current_user_id)
 
+    @property
+    def awx_job_url(self):
+        if self.awx_job_id is None:
+            return None
+        return urllib.parse.urljoin(
+            current_app.config['AWX_URL'],
+            f'/#/jobs/{self.awx_job_id}'
+        )
+
     def __str__(self):
         return str(self.id)
 
@@ -980,6 +990,7 @@ class Task(db.Model):
             'ended_at': utils.format_field(self.ended_at),
             'status': self.status.name,
             'awx_job_id': self.awx_job_id,
+            'awx_job_url': self.awx_job_url,
             'command': self.command,
             'user': str(self.user),
         }
diff --git a/app/settings.py b/app/settings.py
index b797c5b..5ec4621 100644
--- a/app/settings.py
+++ b/app/settings.py
@@ -68,6 +68,7 @@ MAC_OUI = '02:42:42'
 DOCUMENTATION_URL = 'http://ics-infrastructure.pages.esss.lu.se/csentry/index.html'
 CSENTRY_STAGING = False
 
+AWX_URL = 'https://torn.tn.esss.lu.se'
 # AWX job templates
 AWX_CORE_SERVICES_UPDATE = 'ics-ans-core @ DHCP test'
 AWX_CREATE_VM = 'ics-ans-deploy-proxmox-vm'
diff --git a/app/static/js/tasks.js b/app/static/js/tasks.js
index de87469..0aeb32e 100644
--- a/app/static/js/tasks.js
+++ b/app/static/js/tasks.js
@@ -12,18 +12,32 @@ $(document).ready(function() {
     "pageLength": 20,
     "lengthMenu": [[20, 50, 100, -1], [20, 50, 100, "All"]],
     "order": [[2, 'desc']],
-    "columnDefs": [
-      {
-        "targets": [0],
-        "render": function(data, type, row) {
+    "columns": [
+      { data: 'id',
+        render: function(data, type, row) {
           // render funtion to create link to Task view page
           if ( data === null ) {
             return data;
           }
           var url = $SCRIPT_ROOT + "/task/tasks/view/" + data;
-          return '<a href="'+ url + '">' + data + '</a>';
+          return '<a href="' + url + '">' + data + '</a>';
         }
-      }
+      },
+      { data: 'name' },
+      { data: 'created_at' },
+      { data: 'ended_at' },
+      { data: 'status' },
+      { data: null,
+        render: function(data, type, row) {
+          // render link to AWX job
+          if ( row.awx_job_id === null ) {
+            return null;
+          }
+          return '<a href="' + row.awx_job_url + '">' + row.awx_job_id + '</a>';
+        }
+      },
+      { data: 'command' },
+      { data: 'user' },
     ]
   });
 
diff --git a/app/task/views.py b/app/task/views.py
index 1950823..399785a 100644
--- a/app/task/views.py
+++ b/app/task/views.py
@@ -32,5 +32,5 @@ def view_task(id_):
 @bp.route('/_retrieve_tasks')
 @login_required
 def retrieve_tasks():
-    data = [list(task.to_dict().values()) for task in current_user.get_tasks()]
+    data = [task.to_dict() for task in current_user.get_tasks()]
     return jsonify(data=data)
diff --git a/app/templates/task/view_task.html b/app/templates/task/view_task.html
index 7823a36..2530f33 100644
--- a/app/templates/task/view_task.html
+++ b/app/templates/task/view_task.html
@@ -23,7 +23,11 @@
         <dt class="col-sm-3">Status</dt>
         <dd class="col-sm-9">{{ task.status.name }}</dd>
         <dt class="col-sm-3">AWX job</dt>
+        {% if task.awx_job_id %}
+        <dd class="col-sm-9"><a href="{{ task.awx_job_url }}">{{ task.awx_job_id }}</a></dd>
+        {% else %}
         <dd class="col-sm-9">{{ task.awx_job_id }}</dd>
+        {% endif %}
         <dt class="col-sm-3">Command</dt>
         <dd class="col-sm-9">{{ task.command }}</dd>
         <dt class="col-sm-3">User</dt>
-- 
GitLab