Skip to content

Commit 46eeaee

Browse files
committed
feat: added option to use action when POST method is used
1 parent 4b2e528 commit 46eeaee

File tree

4 files changed

+71
-11
lines changed

4 files changed

+71
-11
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "django-smartbase-admin"
3-
version = "0.2.42"
3+
version = "0.2.43"
44
description = ""
55
authors = ["SmartBase <[email protected]>"]
66
readme = "README.md"

src/django_smartbase_admin/actions/admin_action_list.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,23 @@ def __init__(
5858
self.all_params = json.loads(
5959
request.request_data.request_get.get(BASE_PARAMS_NAME, "{}")
6060
)
61-
if (
62-
request.headers.get("X-TabulatorRequest", None) == "true"
63-
and request.request_data.request_method == "POST"
64-
):
65-
try:
66-
self.all_params = json.loads(request.body)
67-
except json.JSONDecodeError:
68-
pass
61+
if request.request_data.request_method == "GET":
62+
source_data = request.request_data.request_get.get(
63+
BASE_PARAMS_NAME, "{}"
64+
)
65+
elif request.request_data.request_method == "POST":
66+
if request.headers.get("X-TabulatorRequest", None) == "true":
67+
source_data = request.body
68+
else:
69+
source_data = request.request_data.request_post.get(
70+
BASE_PARAMS_NAME, "{}"
71+
)
72+
else:
73+
source_data = "{}"
74+
try:
75+
self.all_params = json.loads(source_data)
76+
except json.JSONDecodeError:
77+
pass
6978
else:
7079
self.all_params = all_params
7180
if not self.all_params:

src/django_smartbase_admin/static/sb_admin/src/js/table.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,57 @@ class SBAdminTable {
272272
self.refreshTableDataIfNotUrlLoad()
273273
})
274274
}
275+
276+
executeListAction(action_url, no_params) {
277+
const params = this.getUrlParamsString()
278+
if (this.tabulatorOptions["ajaxConfig"]["method"] === "POST") {
279+
const urlParams = new URLSearchParams(params)
280+
let headers = {
281+
"Content-Type": "application/json",
282+
"X-TabulatorRequest": true
283+
}
284+
headers = {
285+
...headers,
286+
...JSON.parse(document.body.getAttribute('hx-headers')),
287+
}
288+
let filename = ''
289+
fetch(action_url, {
290+
method: "POST",
291+
headers: headers,
292+
body: JSON.stringify(JSON.parse(urlParams.get(this.constants.BASE_PARAMS_NAME)) || {})
293+
})
294+
.then(function(response) {
295+
if (!response.ok) {
296+
throw new Error("Network response was not ok " + response.statusText)
297+
}
298+
if (response.redirected) {
299+
window.location.href = response.url
300+
}
301+
const header = response.headers.get('Content-Disposition')
302+
const parts = header.split(';')
303+
filename = parts[1].split('=')[1]
304+
return response.blob()
305+
})
306+
.then(function(blob) {
307+
const url = window.URL.createObjectURL(blob)
308+
const a = document.createElement("a")
309+
a.style.display = "none"
310+
a.href = url
311+
a.download = filename
312+
document.body.appendChild(a)
313+
a.click()
314+
window.URL.revokeObjectURL(url)
315+
})
316+
.catch(function(error) {
317+
console.error("There was a problem with the fetch operation:", error)
318+
})
319+
} else {
320+
if (!no_params) {
321+
action_url += params
322+
}
323+
window.location.href = action_url
324+
}
325+
}
275326
}
276327

277328
window.SBAdminTableClass = SBAdminTable

src/django_smartbase_admin/templates/sb_admin/actions/list.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ <h1 class="text-24 md:text-30 text-dark-900 font-bold font-heading line-clamp-1
2727
{% block dropdown_actions %}
2828
{% for list_action in content_context.list_actions %}
2929
<li>
30-
<a {% if list_action.open_in_modal %}{% include 'sb_admin/actions/partials/open_modal_attrs.html' with action=list_action %} hx-vals="js:{params: window.SBAdminTable['{{ view_id }}'].getAllUrlParams()}" {% else %}onclick="window.location.href='{{ list_action.url }}'{% if not list_action.no_params %} + window.SBAdminTable['{{ view_id }}'].getUrlParamsString(){% endif %}"{% endif %}
30+
<a {% if list_action.open_in_modal %}{% include 'sb_admin/actions/partials/open_modal_attrs.html' with action=list_action %} hx-vals="js:{params: window.SBAdminTable['{{ view_id }}'].getAllUrlParams()}" {% else %}onclick="window.SBAdminTable['{{ view_id }}'].executeListAction('{{ list_action.url }}', {{ list_action.no_params|yesno:'true,false' }})"{% endif %}
3131
class="dropdown-menu-link {{ list_action.css_class|default_if_none:'' }}">{{ list_action.title }}</a>
3232
</li>
3333
{% endfor %}
@@ -39,7 +39,7 @@ <h1 class="text-24 md:text-30 text-dark-900 font-bold font-heading line-clamp-1
3939
{% block actions %}
4040
{% for list_action in content_context.list_actions %}
4141
<li class="max-sm:hidden">
42-
<button {% if list_action.open_in_modal %}{% include 'sb_admin/actions/partials/open_modal_attrs.html' with action=list_action %} hx-vals="js:{params: window.SBAdminTable['{{ view_id }}'].getAllUrlParams()}" {% else %}onclick="window.location.href='{{ list_action.url }}'{% if not list_action.no_params %} + window.SBAdminTable['{{ view_id }}'].getUrlParamsString(){% endif %}"{% endif %}
42+
<button {% if list_action.open_in_modal %}{% include 'sb_admin/actions/partials/open_modal_attrs.html' with action=list_action %} hx-vals="js:{params: window.SBAdminTable['{{ view_id }}'].getAllUrlParams()}" {% else %}onclick="window.SBAdminTable['{{ view_id }}'].executeListAction('{{ list_action.url }}', {{ list_action.no_params|yesno:'true,false' }})"{% endif %}
4343
class="btn btn-empty {{ list_action.css_class|default_if_none:'' }}">{{ list_action.title }}
4444
</button>
4545
</li>

0 commit comments

Comments
 (0)