From 0c392d02b399e597db443647ed0b85dc6fc690f6 Mon Sep 17 00:00:00 2001
From: James Graham <J.Graham@software.ac.uk>
Date: Thu, 15 Nov 2018 10:54:46 +0000
Subject: [PATCH] Rename Institution to OrganisationalUnit - #30

Accounts for them to be created to manage e.g. research projects
---
 profiles/admin.py                             |  4 +-
 .../migrations/0003_rename_institution.py     | 22 +++++++++++
 profiles/models.py                            | 30 ++++++++------
 .../profiles/institution/detail.html          | 21 ----------
 .../profiles/organisational_unit/detail.html  | 39 +++++++++++++++++++
 profiles/templates/profiles/user/profile.html |  8 ++--
 profiles/urls.py                              |  6 +--
 profiles/views/__init__.py                    |  4 +-
 profiles/views/institution.py                 |  9 -----
 profiles/views/organisational_unit.py         |  9 +++++
 10 files changed, 99 insertions(+), 53 deletions(-)
 create mode 100644 profiles/migrations/0003_rename_institution.py
 delete mode 100644 profiles/templates/profiles/institution/detail.html
 create mode 100644 profiles/templates/profiles/organisational_unit/detail.html
 delete mode 100644 profiles/views/institution.py
 create mode 100644 profiles/views/organisational_unit.py

diff --git a/profiles/admin.py b/profiles/admin.py
index b69742b..7927dd2 100644
--- a/profiles/admin.py
+++ b/profiles/admin.py
@@ -7,6 +7,6 @@ from . import models
 admin.site.register(models.User, UserAdmin)
 
 
-@admin.register(models.Institution)
-class InstitutionAdmin(admin.ModelAdmin):
+@admin.register(models.OrganisationalUnit)
+class OrganisationalUnitAdmin(admin.ModelAdmin):
     pass
diff --git a/profiles/migrations/0003_rename_institution.py b/profiles/migrations/0003_rename_institution.py
new file mode 100644
index 0000000..553b094
--- /dev/null
+++ b/profiles/migrations/0003_rename_institution.py
@@ -0,0 +1,22 @@
+# Generated by Django 2.0.8 on 2018-11-15 09:45
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('profiles', '0002_add_institution'),
+    ]
+
+    operations = [
+        migrations.RenameModel(
+            old_name='Institution',
+            new_name='OrganisationalUnit',
+        ),
+        migrations.RenameField(
+            model_name='user',
+            old_name='institution',
+            new_name='organisational_unit',
+        ),
+    ]
diff --git a/profiles/models.py b/profiles/models.py
index aced23b..ae98a7a 100644
--- a/profiles/models.py
+++ b/profiles/models.py
@@ -7,12 +7,12 @@ from django.urls import reverse
 from core.models import MAX_LENGTH_NAME
 
 
-class Institution(models.Model):
+class OrganisationalUnit(models.Model):
     """
-    Model representing the institution with which a user is associated.
+    Model representing the organisational unit with which a user is associated.
     """
     @enum.unique
-    class InstitutionType(enum.Enum):
+    class OrganisationalUnitType(enum.Enum):
         ACADEMIC = 'aca'
         BUSINESS = 'bus'
         CHARITY = 'cha'
@@ -23,18 +23,24 @@ class Institution(models.Model):
         def choices(cls):
             return tuple((i.value, i.name.title()) for i in cls)
 
-    #: Name of the institution
+    #: Name of the organisational unit
     name = models.CharField(max_length=MAX_LENGTH_NAME,
                             blank=False, null=False)
 
-    #: Type of institution - e.g. academic, business, etc.
+    #: Type of organisational_unit - e.g. academic, business, etc.
     type = models.CharField(max_length=3,
-                            choices=InstitutionType.choices(),
+                            choices=OrganisationalUnitType.choices(),
                             blank=False, null=False)
 
+    @property
+    def all_auditors(self):
+        return self.auditors.union(
+            User.objects.filter(is_superauditor=True)
+        )
+
     @property
     def auditors(self):
-        return self.users.filter(is_auditor=True) + User.objects.filter(is_superauditor=True)
+        return self.users.filter(is_auditor=True)
 
     def __str__(self):
         return self.name
@@ -44,12 +50,12 @@ class User(AbstractUser):
     """
     Model representing a user of PEDASI.
     """
-    #: Institution to which this user belongs
-    institution = models.ForeignKey(Institution, related_name='users',
-                                    on_delete=models.SET_NULL,
-                                    blank=True, null=True)
+    #: Organisational unit to which this user belongs
+    organisational_unit = models.ForeignKey(OrganisationalUnit, related_name='users',
+                                            on_delete=models.SET_NULL,
+                                            blank=True, null=True)
 
-    #: Is this user an auditor of their institution?
+    #: Is this user an auditor of their organisational unit?
     is_auditor = models.BooleanField(default=False,
                                      blank=False, null=False)
 
diff --git a/profiles/templates/profiles/institution/detail.html b/profiles/templates/profiles/institution/detail.html
deleted file mode 100644
index ebcb60c..0000000
--- a/profiles/templates/profiles/institution/detail.html
+++ /dev/null
@@ -1,21 +0,0 @@
-{% 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">
-                Institutions
-            </li>
-            <li class="breadcrumb-item active" aria-current="page">
-                {{ institution.name }}
-            </li>
-        </ol>
-    </nav>
-
-    <h2>{{ institution.name }}</h2>
-
-{% endblock %}
\ No newline at end of file
diff --git a/profiles/templates/profiles/organisational_unit/detail.html b/profiles/templates/profiles/organisational_unit/detail.html
new file mode 100644
index 0000000..be5c251
--- /dev/null
+++ b/profiles/templates/profiles/organisational_unit/detail.html
@@ -0,0 +1,39 @@
+{% 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">
+                Organisational Units
+            </li>
+            <li class="breadcrumb-item active" aria-current="page">
+                {{ organisational_unit.name }}
+            </li>
+        </ol>
+    </nav>
+
+    <h2>{{ organisational_unit.name }}</h2>
+
+    <h4 class="pt-3">Auditors</h4>
+
+    <table class="table">
+        <thead>
+            <tr>
+                <th scope="col">Name</th>
+            </tr>
+        </thead>
+
+        <tbody>
+            {% for auditor in organisational_unit.auditors.all %}
+                <tr>
+                    <td>{{ auditor.get_full_name }}</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 f4e6e83..7ff10e8 100644
--- a/profiles/templates/profiles/user/profile.html
+++ b/profiles/templates/profiles/user/profile.html
@@ -22,12 +22,12 @@
         </thead>
 
         <tbody>
-        {% if request.user.institution %}
+        {% if request.user.organisational_unit %}
             <tr>
-                <td>Institution</td>
+                <td>Organisational Unit</td>
                 <td>
-                    {{ request.user.institution.name }}
-                    <a href="{% url 'profiles:institution.detail' pk=request.user.institution_id %}"
+                    {{ request.user.organisational_unit.name }}
+                    <a href="{% url 'profiles:organisational_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 d5bf9e9..c55df55 100644
--- a/profiles/urls.py
+++ b/profiles/urls.py
@@ -19,7 +19,7 @@ urlpatterns = [
          views.user.UserUriView.as_view(),
          name='uri'),
 
-    path('institutions/<int:pk>',
-         views.institution.InstitutionDetailView.as_view(),
-         name='institution.detail'),
+    path('orgunit/<int:pk>',
+         views.organisational_unit.OrganisationalUnitDetailView.as_view(),
+         name='organisational_unit.detail'),
 ]
diff --git a/profiles/views/__init__.py b/profiles/views/__init__.py
index cd9a456..e18dfd7 100644
--- a/profiles/views/__init__.py
+++ b/profiles/views/__init__.py
@@ -3,9 +3,9 @@ from django.views.generic import TemplateView
 from applications.models import Application
 from datasources.models import DataSource
 
-from . import institution, user
+from . import organisational_unit, user
 
-__all__ = ['IndexView', 'institution', 'user']
+__all__ = ['IndexView', 'organisational_unit', 'user']
 
 
 class IndexView(TemplateView):
diff --git a/profiles/views/institution.py b/profiles/views/institution.py
deleted file mode 100644
index c13b543..0000000
--- a/profiles/views/institution.py
+++ /dev/null
@@ -1,9 +0,0 @@
-from django.views.generic.detail import DetailView
-
-from profiles import models
-
-
-class InstitutionDetailView(DetailView):
-    model = models.Institution
-    template_name = 'profiles/institution/detail.html'
-    context_object_name = 'institution'
diff --git a/profiles/views/organisational_unit.py b/profiles/views/organisational_unit.py
new file mode 100644
index 0000000..fa42b51
--- /dev/null
+++ b/profiles/views/organisational_unit.py
@@ -0,0 +1,9 @@
+from django.views.generic.detail import DetailView
+
+from profiles import models
+
+
+class OrganisationalUnitDetailView(DetailView):
+    model = models.OrganisationalUnit
+    template_name = 'profiles/organisational_unit/detail.html'
+    context_object_name = 'organisational_unit'
-- 
GitLab