From 48253bee6f0d02381372fdd75230fb38ab3de48c Mon Sep 17 00:00:00 2001
From: James Graham <J.Graham@software.ac.uk>
Date: Thu, 15 Nov 2018 15:42:49 +0000
Subject: [PATCH] Add audit view to datasources and via organisational unit
 detail

---
 .../datasources/datasource/audit.html         | 51 +++++++++++++++++++
 datasources/urls.py                           |  4 ++
 datasources/views/datasource.py               | 14 +++++
 .../profiles/organisational_unit/detail.html  | 32 +++++++++++-
 profiles/templates/profiles/user/profile.html |  2 +-
 profiles/urls.py                              |  2 +-
 profiles/views/organisational_unit.py         |  9 ++++
 7 files changed, 111 insertions(+), 3 deletions(-)
 create mode 100644 datasources/templates/datasources/datasource/audit.html

diff --git a/datasources/templates/datasources/datasource/audit.html b/datasources/templates/datasources/datasource/audit.html
new file mode 100644
index 0000000..ca0348f
--- /dev/null
+++ b/datasources/templates/datasources/datasource/audit.html
@@ -0,0 +1,51 @@
+{% extends "base.html" %}
+{% load bootstrap4 %}
+
+{% block content %}
+    <nav aria-label="breadcrumb">
+        <ol class="breadcrumb">
+            <li class="breadcrumb-item" aria-current="page">
+                <a href="{% url 'index' %}">Home</a>
+            </li>
+            <li class="breadcrumb-item" aria-current="page">
+                <a href="{% url 'datasources:datasource.list' %}">Data Sources</a>
+            </li>
+            <li class="breadcrumb-item" aria-current="page">
+                <a href="{% url 'datasources:datasource.detail' pk=datasource.pk %}">{{ datasource.name }}</a>
+            </li>
+            <li class="breadcrumb-item active" aria-current="page">
+                Audit
+            </li>
+        </ol>
+    </nav>
+
+    <h2>Audit Data Source - {{ datasource.name }}</h2>
+
+    {% if datasource.description %}
+        <p>{{ datasource.description }}</p>
+    {% endif %}
+
+    <hr/>
+
+    <h4>Records</h4>
+
+    <table class="table">
+        <thead>
+            <tr>
+                <th scope="col"></th>
+            </tr>
+        </thead>
+        <tbody>
+            {% for record in records %}
+                <tr>
+                    <td>{{ record.to_json }}</td>
+                </tr>
+            {% empty %}
+                <tr>
+                    <td>No records</td>
+                </tr>
+            {% endfor %}
+        </tbody>
+    </table>
+
+{% endblock %}
\ No newline at end of file
diff --git a/datasources/urls.py b/datasources/urls.py
index cdba90c..23584dc 100644
--- a/datasources/urls.py
+++ b/datasources/urls.py
@@ -13,6 +13,10 @@ urlpatterns = [
          views.datasource.DataSourceDetailView.as_view(),
          name='datasource.detail'),
 
+    path('<int:pk>/audit',
+         views.datasource.DataSourceAuditView.as_view(),
+         name='datasource.audit'),
+
     path('<int:pk>/query',
          views.datasource.DataSourceQueryView.as_view(),
          name='datasource.query'),
diff --git a/datasources/views/datasource.py b/datasources/views/datasource.py
index fc2f6cc..c5e18b0 100644
--- a/datasources/views/datasource.py
+++ b/datasources/views/datasource.py
@@ -6,6 +6,7 @@ import requests.exceptions
 
 from datasources import models
 from profiles.permissions import HasViewPermissionMixin
+from provenance.models import ProvWrapper
 
 
 class DataSourceListView(ListView):
@@ -111,3 +112,16 @@ class DataSourceExploreView(HasViewPermissionMixin, DetailView):
     model = models.DataSource
     template_name = 'datasources/datasource/explore.html'
     context_object_name = 'datasource'
+
+
+class DataSourceAuditView(DetailView):
+    model = models.DataSource
+    template_name = 'datasources/datasource/audit.html'
+    context_object_name = 'datasource'
+
+    def get_context_data(self, **kwargs):
+        context = super().get_context_data()
+
+        context['records'] = ProvWrapper.filter_model_instance(self.object)
+
+        return context
diff --git a/profiles/templates/profiles/organisational_unit/detail.html b/profiles/templates/profiles/organisational_unit/detail.html
index be5c251..22e75d4 100644
--- a/profiles/templates/profiles/organisational_unit/detail.html
+++ b/profiles/templates/profiles/organisational_unit/detail.html
@@ -16,7 +16,15 @@
         </ol>
     </nav>
 
-    <h2>{{ organisational_unit.name }}</h2>
+    <div class="row">
+        <div class="col-md-10 col-sm-8">
+            <h2>{{ organisational_unit.name }}</h2>
+        </div>
+
+        <div class="col-md-2 col-sm-4">
+        </div>
+    </div>
+
 
     <h4 class="pt-3">Auditors</h4>
 
@@ -36,4 +44,26 @@
         </tbody>
     </table>
 
+    <h4 class="pt-3">Data Sources</h4>
+    <table class="table">
+        <thead>
+        <tr>
+            <th scope="col">Name</th>
+            <th scope="col"></th>
+        </tr>
+        </thead>
+
+        <tbody>
+        {% for datasource in datasources %}
+            <tr>
+                <td>{{ datasource.name }}</td>
+                <td>
+                    <a href="{% url 'datasources:datasource.audit' pk=datasource.pk %}"
+                       class="btn btn-secondary" role="button">Audit</a>
+                </td>
+            </tr>
+        {% endfor %}
+        </tbody>
+    </table>
+
 {% endblock %}
\ No newline at end of file
diff --git a/profiles/templates/profiles/user/profile.html b/profiles/templates/profiles/user/profile.html
index 7ff10e8..91786dd 100644
--- a/profiles/templates/profiles/user/profile.html
+++ b/profiles/templates/profiles/user/profile.html
@@ -27,7 +27,7 @@
                 <td>Organisational Unit</td>
                 <td>
                     {{ request.user.organisational_unit.name }}
-                    <a href="{% url 'profiles:organisational_unit.detail' pk=request.user.organisational_unit_id %}"
+                    <a href="{% url 'profiles:org_unit.detail' pk=request.user.organisational_unit_id %}"
                        class="btn btn-info">Detail</a>
                 </td>
             </tr>
diff --git a/profiles/urls.py b/profiles/urls.py
index c55df55..4e3c29e 100644
--- a/profiles/urls.py
+++ b/profiles/urls.py
@@ -21,5 +21,5 @@ urlpatterns = [
 
     path('orgunit/<int:pk>',
          views.organisational_unit.OrganisationalUnitDetailView.as_view(),
-         name='organisational_unit.detail'),
+         name='org_unit.detail'),
 ]
diff --git a/profiles/views/organisational_unit.py b/profiles/views/organisational_unit.py
index fa42b51..c5f8b66 100644
--- a/profiles/views/organisational_unit.py
+++ b/profiles/views/organisational_unit.py
@@ -7,3 +7,12 @@ class OrganisationalUnitDetailView(DetailView):
     model = models.OrganisationalUnit
     template_name = 'profiles/organisational_unit/detail.html'
     context_object_name = 'organisational_unit'
+
+    def get_context_data(self, **kwargs):
+        context = super().get_context_data()
+
+        context['datasources'] = []
+        for user in self.object.users.all():
+            context['datasources'].extend(user.datasources.all())
+
+        return context
-- 
GitLab