From 0c897852dbe875c20f2b164ee9c4ae1097570d91 Mon Sep 17 00:00:00 2001
From: Martin Burchell <martinb@aptivate.org>
Date: Tue, 11 Dec 2018 11:14:49 +0000
Subject: [PATCH] Add links from table cells to the edit page where applicable

Also reinstate the created/imported column in the code but hide it in the fixtures.
---
 .../fixtures/bangladesh-refugee-crisis.json   |  2 +-
 internewshid/hid/tables.py                    | 31 ++++++++--------
 .../hid/templates/hid/age_column.html         |  2 +-
 .../hid/templates/hid/created_column.html     |  1 +
 .../hid/templates/hid/ennumerator_column.html |  2 +-
 .../hid/templates/hid/gender_column.html      |  2 +-
 .../hid/templates/hid/location_column.html    |  2 +-
 .../hid/templates/hid/source_column.html      |  2 +-
 .../hid/templates/hid/tags_column.html        |  2 +-
 .../hid/templates/hid/timestamp_column.html   |  1 +
 internewshid/hid/tests/itemtable_tests.py     | 37 +++++++++++++++++++
 11 files changed, 62 insertions(+), 22 deletions(-)
 create mode 100644 internewshid/hid/templates/hid/created_column.html
 create mode 100644 internewshid/hid/templates/hid/timestamp_column.html

diff --git a/internewshid/hid/fixtures/bangladesh-refugee-crisis.json b/internewshid/hid/fixtures/bangladesh-refugee-crisis.json
index aadb230a..6fc73d66 100644
--- a/internewshid/hid/fixtures/bangladesh-refugee-crisis.json
+++ b/internewshid/hid/fixtures/bangladesh-refugee-crisis.json
@@ -302,7 +302,7 @@
             "name": "feedback",
             "page": 1,
             "position": 2,
-            "settings": "{\"label\":\"Feedback\",\"columns\":[\"select_item\",\"created\",\"timestamp\",\"body\",\"translation\",\"category\",\"tags\",\"location\",\"gender\",\"age\",\"ennumerator\",\"source\"],\"filters\":{\"terms\":[]},\"dynamic_filters\":[\"time_range\",\"category\",\"tags\",\"gender\",\"age\",\"location\",\"ennumerator\",\"source\"],\"categories\":[\"bangladesh-refugee-crisis-sectors\"]}",
+            "settings": "{\"label\":\"Feedback\",\"columns\":[\"select_item\",\"timestamp\",\"body\",\"translation\",\"category\",\"tags\",\"location\",\"gender\",\"age\",\"ennumerator\",\"source\"],\"filters\":{\"terms\":[]},\"dynamic_filters\":[\"time_range\",\"category\",\"tags\",\"gender\",\"age\",\"location\",\"ennumerator\",\"source\"],\"categories\":[\"bangladesh-refugee-crisis-sectors\"]}",
             "tab_type": "view-and-edit-table"
         },
         "model": "tabbed_page.tabinstance",
diff --git a/internewshid/hid/tables.py b/internewshid/hid/tables.py
index 71e8cc16..ce7160b6 100644
--- a/internewshid/hid/tables.py
+++ b/internewshid/hid/tables.py
@@ -24,18 +24,15 @@ class ItemTable(tables.Table):
         verbose_name=_('Select'),
         attrs={'td': {'class': 'col-md-1'}}
     )
-    # Note(lukem): based on adding a bunch of new table fields, we have little
-    # space now on the table view. So, since we have not seen any demands from
-    # for this field (seems useful but anyway), we drop it for now to save
-    # space. Un-commenting this will bring it right back
-    # created = tables.columns.DateTimeColumn(
-    #     verbose_name=_('Imported'),
-    #     format=settings.SHORT_DATETIME_FORMAT,
-    #     attrs={'td': {'class': 'col-md-1'}}
-    # )
-    timestamp = tables.columns.DateTimeColumn(
+
+    created = tables.columns.TemplateColumn(
+        template_name='hid/created_column.html',
+        verbose_name=_('Imported'),
+        attrs={'td': {'class': 'col-md-1'}}
+    )
+    timestamp = tables.columns.TemplateColumn(
+        template_name='hid/timestamp_column.html',
         verbose_name=_('Created'),
-        format=settings.SHORT_DATETIME_FORMAT,
         attrs={'td': {'class': 'col-md-2'}}
     )
     body = tables.TemplateColumn(
@@ -107,20 +104,24 @@ class ItemTable(tables.Table):
     def render_tags(self, record, value):
         Template = loader.get_template('hid/tags_column.html')
 
+        context = self.context
+
         try:
             tags = filter(None, [
                 term['name'] for term in record['terms']
                 if term['taxonomy'] == 'tags'
             ])
             tags = [' '.join([x.capitalize() for x in t.split()]) for t in tags]
-            ctx = {'tags': ', '.join(list(filter(
+            context['tags'] = ', '.join(list(filter(
                 lambda tag: tag != 'None' and tag is not None,
                 tags
-            )))}
+            )))
         except KeyError:
-            ctx = {'tags': []}
+            context['tags'] = []
 
-        return Template.render(ctx)
+        context['record'] = record
+
+        return Template.render(context.flatten())
 
     @staticmethod
     def get_selected(params):
diff --git a/internewshid/hid/templates/hid/age_column.html b/internewshid/hid/templates/hid/age_column.html
index c5aec9cd..6d9fe7cc 100644
--- a/internewshid/hid/templates/hid/age_column.html
+++ b/internewshid/hid/templates/hid/age_column.html
@@ -1 +1 @@
-{{ record.age }}
+<a href='{% url "edit-item" item_id=record.id %}?next={{ request.path }}'>{{ record.age }}</a>
diff --git a/internewshid/hid/templates/hid/created_column.html b/internewshid/hid/templates/hid/created_column.html
new file mode 100644
index 00000000..24361dde
--- /dev/null
+++ b/internewshid/hid/templates/hid/created_column.html
@@ -0,0 +1 @@
+<a href='{% url "edit-item" item_id=record.id %}?next={{ request.path }}'>{{ record.created|date:"SHORT_DATETIME_FORMAT"|default:default }}</a>
diff --git a/internewshid/hid/templates/hid/ennumerator_column.html b/internewshid/hid/templates/hid/ennumerator_column.html
index 6dc76a4a..197ddd6a 100644
--- a/internewshid/hid/templates/hid/ennumerator_column.html
+++ b/internewshid/hid/templates/hid/ennumerator_column.html
@@ -1 +1 @@
-{{ record.ennumerator | title }}
+<a href='{% url "edit-item" item_id=record.id %}?next={{ request.path }}'>{{ record.ennumerator | title }}</a>
diff --git a/internewshid/hid/templates/hid/gender_column.html b/internewshid/hid/templates/hid/gender_column.html
index d688b1e8..eac3fc54 100644
--- a/internewshid/hid/templates/hid/gender_column.html
+++ b/internewshid/hid/templates/hid/gender_column.html
@@ -1 +1 @@
-{{ record.gender | title }}
+<a href='{% url "edit-item" item_id=record.id %}?next={{ request.path }}'>{{ record.gender | title }}</a>
diff --git a/internewshid/hid/templates/hid/location_column.html b/internewshid/hid/templates/hid/location_column.html
index 7dfb820a..4162080a 100644
--- a/internewshid/hid/templates/hid/location_column.html
+++ b/internewshid/hid/templates/hid/location_column.html
@@ -1 +1 @@
-{{ record.location | title }}
+<a href='{% url "edit-item" item_id=record.id %}?next={{ request.path }}'>{{ record.location | title }}</a>
diff --git a/internewshid/hid/templates/hid/source_column.html b/internewshid/hid/templates/hid/source_column.html
index 5d958833..d3e6fe80 100644
--- a/internewshid/hid/templates/hid/source_column.html
+++ b/internewshid/hid/templates/hid/source_column.html
@@ -1 +1 @@
-{{ record.source | title }}
+<a href='{% url "edit-item" item_id=record.id %}?next={{ request.path }}'>{{ record.source | title }}</a>
diff --git a/internewshid/hid/templates/hid/tags_column.html b/internewshid/hid/templates/hid/tags_column.html
index 827ae20e..ad04951b 100644
--- a/internewshid/hid/templates/hid/tags_column.html
+++ b/internewshid/hid/templates/hid/tags_column.html
@@ -1 +1 @@
-{{ tags }}
+<a href='{% url "edit-item" item_id=record.id %}?next={{ request.path }}'>{{ tags }}</a>
diff --git a/internewshid/hid/templates/hid/timestamp_column.html b/internewshid/hid/templates/hid/timestamp_column.html
new file mode 100644
index 00000000..ab83e050
--- /dev/null
+++ b/internewshid/hid/templates/hid/timestamp_column.html
@@ -0,0 +1 @@
+<a href='{% url "edit-item" item_id=record.id %}?next={{ request.path }}'>{{ record.timestamp|date:"SHORT_DATETIME_FORMAT"|default:default }}</a>
diff --git a/internewshid/hid/tests/itemtable_tests.py b/internewshid/hid/tests/itemtable_tests.py
index 36bb3deb..e1ad19f5 100644
--- a/internewshid/hid/tests/itemtable_tests.py
+++ b/internewshid/hid/tests/itemtable_tests.py
@@ -1,5 +1,7 @@
 import mock
 
+from django.template import Context
+
 from hid.constants import ITEM_TYPE_CATEGORY
 from hid.tables import ItemTable
 
@@ -97,3 +99,38 @@ def test_render_category_passes_context_to_template(mock_loader):
     }
 
     mock_template.render.assert_called_with(context)
+
+
+@mock.patch('hid.tables.loader')
+def test_render_tags_passes_record_and_tags_to_template(mock_loader):
+    mock_template = mock.MagicMock()
+    mock_template.render = mock.MagicMock()
+    mock_loader.get_template = mock.MagicMock(
+        return_value=mock_template)
+
+    record = {
+        'terms': [
+            {
+                'name': 'foo',
+                'taxonomy': 'tags',
+            },
+            {
+                'name': 'bar',
+                'taxonomy': 'tags',
+            },
+            {
+                'name': 'baz',
+                'taxonomy': 'not tags',
+            },
+        ]
+    }
+
+    table = ItemTable([], categories=None)
+    table.context = Context()
+
+    table.render_tags(record, None)
+
+    args, kwargs = mock_template.render.call_args
+
+    assert args[0]['record'] == record
+    assert args[0]['tags'] == 'Foo, Bar'
-- 
GitLab