[PATCH v3 5/5] patch-list: add inline dropdown for delegate and state one-off changes

Raxel Gutierrez raxel at google.com
Thu Jul 29 01:24:19 AEST 2021


Add dropdown for the cell values of the Delegate and State columns for
each individual patch to make one-off changes to patches. The dropdowns
are only viewable when logged in and revert selections made by users
that don't have permission to change a given patch's state or delegate.
Change the generic_list method to pass the list of states and
maintainers to the patch list view context to populate the dropdown
options. The static patch-list.js file now uses the modularity of the
fetch request and update/error messages handling of rest.js.

Signed-off-by: Raxel Gutierrez <raxel at google.com>
---
 htdocs/js/patch-list.js                       | 48 +++++++++++++++++++
 htdocs/js/rest.js                             |  2 +-
 .../patchwork/partials/patch-list.html        | 40 ++++++++++++++--
 patchwork/views/__init__.py                   |  3 ++
 templates/base.html                           |  2 +-
 5 files changed, 90 insertions(+), 5 deletions(-)

diff --git a/htdocs/js/patch-list.js b/htdocs/js/patch-list.js
index 5c94e66..13f890d 100644
--- a/htdocs/js/patch-list.js
+++ b/htdocs/js/patch-list.js
@@ -1,4 +1,31 @@
+import { updateProperty } from "./rest.js";
+
 $( document ).ready(function() {
+    let inlinePropertyDropdowns = $("td > select[class^='change-property-']");
+    $(inlinePropertyDropdowns).each(function() {
+        // Store previous dropdown selection
+        $(this).data("prevProperty", $(this).val());
+    });
+
+    // Change listener for dropdowns that change an individual patch's delegate and state properties
+    $(inlinePropertyDropdowns).change((event) => {
+        const property = event.target.getAttribute("value");
+        const { url, data } = getPatchProperties(event.target, property);
+        const updateMessage = {
+            'none': "No patches updated",
+            'some': "1 patch updated",
+        };
+        updateProperty(url, data, updateMessage).then(is_success => {
+            if (!is_success) {
+                // Revert to previous selection
+                $(event.target).val($(event.target).data("prevProperty"));
+            } else {
+                // Update to new previous selection
+                $(event.target).data("prevProperty", $(event.target).val());
+            }
+        });
+    });
+
     $("#patchlist").stickyTableHeaders();
 
     $("#check-all").change(function(e) {
@@ -9,4 +36,25 @@ $( document ).ready(function() {
         }
         e.preventDefault();
     });
+
+    /**
+     * Returns the data to make property changes to a patch through fetch request.
+     * @param {Element} propertySelect Property select element modified.
+     * @param {string} property Patch property modified (e.g. "state", "delegate")
+     * @return {{property: string, value: string}}
+     *     property: Property field to be modified in request.
+     *     value: New value for property to be modified to in request.
+     */
+    function getPatchProperties(propertySelect, property) {
+        const selectedOption = propertySelect.options[propertySelect.selectedIndex];
+        const patchId = propertySelect.parentElement.parentElement.dataset.patchId;
+        const propertyValue = (property === "state") ? selectedOption.text
+                            : (selectedOption.value === "*") ? null : selectedOption.value
+        const data = {};
+        data[property] = propertyValue;
+        return {
+            "url": "/api/patches/" + patchId + "/",
+            "data": data,
+        };
+    }
 });
diff --git a/htdocs/js/rest.js b/htdocs/js/rest.js
index 18c0295..ede7295 100644
--- a/htdocs/js/rest.js
+++ b/htdocs/js/rest.js
@@ -69,4 +69,4 @@ function handleErrorMessages(errorMessage) {
     container.prepend(errorHeader);
 }
 
-export { updateProperty, handleUpdateMessages, handleUpdateMessages};
\ No newline at end of file
+export { updateProperty };
\ No newline at end of file
diff --git a/patchwork/templates/patchwork/partials/patch-list.html b/patchwork/templates/patchwork/partials/patch-list.html
index 5e2f0dd..90b22d1 100644
--- a/patchwork/templates/patchwork/partials/patch-list.html
+++ b/patchwork/templates/patchwork/partials/patch-list.html
@@ -5,7 +5,7 @@
 {% load static %}
 
 {% block headers %}
-  <script src="{% static "js/patch-list.js" %}"></script>
+  <script type="module" src="{% static "js/patch-list.js" %}"></script>
 {% endblock %}
 
 {% include "patchwork/partials/filters.html" %}
@@ -187,8 +187,42 @@
    <td id="patch-checks:{{patch.id}}" class="text-nowrap">{{ patch|patch_checks }}</td>
    <td id="patch-date:{{patch.id}}" class="text-nowrap">{{ patch.date|date:"Y-m-d" }}</td>
    <td id="patch-submitter:{{patch.id}}">{{ patch.submitter|personify:project }}</td>
-   <td id="patch-delegate:{{patch.id}}">{{ patch.delegate.username }}</td>
-   <td id="patch-state:{{patch.id}}">{{ patch.state }}</td>
+   <td id="patch-delegate:{{patch.id}}">
+    <td id="patch-delegate:{{patch.id}}">
+      {% if user.is_authenticated %}
+        <select class="change-property-delegate" value="delegate">
+          {% if not patch.delegate.username %}
+            <option value="*" selected>No delegate</option>
+          {% else %}
+            <option value="*">No delegate</option>
+          {% endif %}
+          {% for maintainer in maintainers %}
+            {% if maintainer.name == patch.delegate.username %}
+              <option value="{{ patch.delegate.username.id }}" selected>{{ patch.delegate.username }}</option>
+            {% else %}
+              <option value="{{ maintainer.id }}">{{ maintainer.name }}</option>
+            {% endif %}
+          {% endfor %}
+        </select>
+      {% else %}
+        {{ patch.delegate.username }}
+      {% endif %}
+     </td>
+     <td id="patch-state:{{patch.id}}">
+      {% if user.is_authenticated %}
+        <select class="change-property-state" value="state">
+          {% for state in states %}
+            {% if state.name == patch.state.name %}
+              <option value="{{ patch.state.ordering }}" selected>{{ patch.state }}</option>
+            {% else %}
+              <option value="{{ state.ordering  }}">{{ state.name }}</option>
+            {% endif %}
+          {% endfor %}
+        </select>
+      {% else %}
+        {{ patch.state }}
+      {% endif %}
+     </td>
   </tr>
  {% empty %}
   <tr>
diff --git a/patchwork/views/__init__.py b/patchwork/views/__init__.py
index 3ea2af4..c70c6be 100644
--- a/patchwork/views/__init__.py
+++ b/patchwork/views/__init__.py
@@ -16,6 +16,7 @@ from patchwork.models import Bundle
 from patchwork.models import BundlePatch
 from patchwork.models import Patch
 from patchwork.models import Project
+from patchwork.models import State
 from patchwork.models import Check
 from patchwork.paginator import Paginator
 
@@ -178,6 +179,8 @@ def generic_list(request, project, view, view_args=None, filter_settings=None,
         'project': project,
         'projects': Project.objects.all(),
         'filters': filters,
+        'maintainers': project.maintainer_project.all(),
+        'states': State.objects.all(),
     }
 
     # pagination
diff --git a/templates/base.html b/templates/base.html
index 8700602..e57e2d5 100644
--- a/templates/base.html
+++ b/templates/base.html
@@ -114,7 +114,7 @@
   {% endfor %}
   </div>
 {% endif %}
-  <div class="container-fluid">
+  <div id="main-content" class="container-fluid">
 {% block body %}
 {% endblock %}
   </div>
-- 
2.32.0.554.ge1b32706d8-goog



More information about the Patchwork mailing list