From cdba29c95ace67aafd9ff76684de73460be6e606 Mon Sep 17 00:00:00 2001 From: James Graham <J.Graham@software.ac.uk> Date: Thu, 25 Apr 2019 10:28:28 +0100 Subject: [PATCH] Remove metadata form from data source detail view - #70 --- datasources/forms.py | 6 -- datasources/static/js/metadata.js | 99 +------------------ .../datasources/datasource/detail.html | 41 -------- datasources/views/datasource.py | 2 - 4 files changed, 1 insertion(+), 147 deletions(-) diff --git a/datasources/forms.py b/datasources/forms.py index e3678de..774ae78 100644 --- a/datasources/forms.py +++ b/datasources/forms.py @@ -97,12 +97,6 @@ class PermissionGrantForm(forms.ModelForm): fields = ['granted', 'push_granted'] -class MetadataFieldForm(forms.ModelForm): - class Meta: - model = models.MetadataItem - fields = ['field', 'value'] - - class LicenceForm(forms.ModelForm): class Meta: model = models.Licence diff --git a/datasources/static/js/metadata.js b/datasources/static/js/metadata.js index f500c61..517865a 100644 --- a/datasources/static/js/metadata.js +++ b/datasources/static/js/metadata.js @@ -21,6 +21,7 @@ function setRatingUrl(url) { function getCookie(name) { + "use strict"; const re = new RegExp("(^| )" + name + "=([^;]+)"); const match = document.cookie.match(re); if (match) { @@ -28,104 +29,6 @@ function getCookie(name) { } } - -function tableAppendRow(table, values, id=null) { - const row = table.insertRow(-1); - if (id !== null) { - row.id = id; - } - - for (const value of values) { - const cell = row.insertCell(-1); - const text = document.createTextNode(value); - cell.appendChild(text); - } - - return row; -} - - -function postMetadata() { - fetch( - metadataUrl, - { - method: "POST", - body: JSON.stringify({ - field: document.getElementById("id_field").value, - value: document.getElementById("id_value").value - }), - headers: { - "Accept": "application/json", - "Content-Type": "application/json", - "X-CSRFToken": getCookie("csrftoken") - } - } - ).then( - response => response.json() - ).then( - function (response) { - if (response.status === "success") { - const table = document.getElementById("tableMetadata"); - const field = response.data.field; - const value = response.data.value; - - const row = tableAppendRow( - table, - [field, value], - "metadata-row-" + response.data.field_short + "-" + value - ); - - const buttonCell = row.insertCell(-1); - const button = document.createElement("button"); - - button.id = "btn-" + field + "-" + value; - button.classList.add("btn", "btn-sm", "btn-danger", "float-right"); - button.addEventListener( - "click", - function () { - const f = response.data.field_short; - const v = value; - deleteMetadata(f, v); - } - ); - button.textContent = "Delete"; - buttonCell.appendChild(button); - row.appendChild(buttonCell); - } - } - ).catch( - function (e) { - console.log(e); - } - ) -} - -function deleteMetadata(field, value) { - fetch( - metadataUrl, - { - method: "DELETE", - body: JSON.stringify({ - field: field, - value: value - }), - headers: { - "Accept": "application/json", - "Content-Type": "application/json", - "X-CSRFToken": getCookie("csrftoken") - } - } - ).then( - response => response.json() - ).then(function (response) { - if (response.status === "success") { - const row = document.getElementById("metadata-row-" + field + "-" + value); - row.parentNode.removeChild(row); - } - - }) -} - function updateMetadata(metadataIdField, valueField) { "use strict"; diff --git a/datasources/templates/datasources/datasource/detail.html b/datasources/templates/datasources/datasource/detail.html index 89c965b..9fb4762 100644 --- a/datasources/templates/datasources/datasource/detail.html +++ b/datasources/templates/datasources/datasource/detail.html @@ -1,18 +1,6 @@ {% extends "base.html" %} {% load bootstrap4 %} -{% block extra_head %} - {% if metadata_field_form %} - {% load static %} - - <script type="application/javascript" src="{% static 'js/metadata.js' %}"></script> - - <script type="application/javascript"> - setMetadataUrl("/datasources/{{ datasource.pk }}/metadata"); - </script> - {% endif %} -{% endblock %} - {% block content %} <nav aria-label="breadcrumb"> <ol class="breadcrumb"> @@ -144,9 +132,6 @@ <thead> <th scope="col" class="w-25 border-0"></th> <th scope="col" class="border-0"></th> - {% if metadata_field_form %} - <th scope="col" class="w-25 border-0"></th> - {% endif %} </thead> <tbody> @@ -154,36 +139,10 @@ <tr id="metadata-row-{{ metadata_item.field.short_name }}-{{ metadata_item.value }}"> <td>{{ metadata_item.field.name }}</td> <td>{{ metadata_item.value }}</td> - - {% if metadata_field_form %} - <td> - <button role="button" - onclick="deleteMetadata('{{ metadata_item.field.short_name }}','{{ metadata_item.value }}')" - class="btn btn-sm btn-danger float-right">Delete</button> - </td> - {% endif %} </tr> {% endfor %} </tbody> </table> - - {% if metadata_field_form %} - <form id="formMetadata" class="form" action="javascript:postMetadata();"> - {% csrf_token %} - - <div class="row"> - <div class="col-3"> - {% bootstrap_field metadata_field_form.field layout='inline' %} - </div> - <div class="col-6"> - {% bootstrap_field metadata_field_form.value layout='inline' %} - </div> - <div class="col-3"> - <button type="submit" class="btn btn-block btn-success">Add</button> - </div> - </div> - </form> - {% endif %} </div> </div> diff --git a/datasources/views/datasource.py b/datasources/views/datasource.py index b3d6ea1..57b2230 100644 --- a/datasources/views/datasource.py +++ b/datasources/views/datasource.py @@ -37,8 +37,6 @@ class DataSourceDetailView(DetailView): context = super().get_context_data(**kwargs) context['has_edit_permission'] = self.request.user.is_superuser or self.request.user == self.object.owner - if context['has_edit_permission']: - context['metadata_field_form'] = forms.MetadataFieldForm() try: context['is_catalogue'] = self.object.is_catalogue -- GitLab