Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip to select GET or POST for actions #168

Merged
merged 2 commits into from
Sep 9, 2024
Merged

Conversation

SupraSummus
Copy link
Contributor

Another try at enforcing POST actions. This change is more gradual than #149 - when library user doesn't change default options the behavior is exactly the same as before the change, that is:

  1. Action buttons send GET requests
  2. Action handlers accept GET and POST requests

However, user can change this behavior using methods and button_type kwargs. For example @action(methods=['POST'], button_type='form') results in

  1. Action button sends POST requests
  2. Action handler accepts only POST request

Unfortunately I have this tested only within my project. Also the docs are missing.

And one more thing - I think it is better to use <input type="submit"> instead of js to submit the form. This js is need to make the buttons look the same in both versions. With proper CSS (that is beyond my ability to write ;) ) js is avoidable and we could be using pretty semantic html submit button. I took the form button template from #149.

Copy link
Owner

@crccheck crccheck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is looking good. I just did a skim, I hope to do a full read this week.

Comment on lines -257 to -258
# HACK to allow POST requests too
post = get
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, getting rid of a HACK

<a href="{% add_preserved_filters action_url %}"
title="{{ tool.standard_attrs.title }}"
{% for k, v in tool.custom_attrs.items %}
{{ k }}="{{ v }}"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to look into if we need to make sure the html is safe (will be one of the things I experiment with)

Suggested change
{{ k }}="{{ v }}"
{{ k }}="{{ v|escape }}"

https://docs.djangoproject.com/el/4.2/ref/templates/builtins/#escape

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{% elif tool.button_type == 'form' %}
<form method="post" action="{% add_preserved_filters action_url %}">
{% csrf_token %}
<a href="#" onclick="this.parentNode.submit(); return false;"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really doubt Django has styling for anything other than the <a> but it would be nice to use <button> or <input type=submit> and not need the JavaScript.

@crccheck crccheck changed the title Action settings to controll allowed methods and display element wip to select GET or POST for actions Sep 9, 2024
@crccheck
Copy link
Owner

crccheck commented Sep 9, 2024

I'll merge this as a broken commit so you get attribution, but I'll work on the next commit to fix CI. I'd like to remove button_type (or make it a secret option) so users will only need to pick the method GET or POST. Since we need to support GET and POST for backwards compatibility, your way is good

@crccheck crccheck merged commit 1274ae7 into crccheck:master Sep 9, 2024
1 of 34 checks passed
@SupraSummus
Copy link
Contributor Author

@crccheck thanks a lot!

crccheck added a commit that referenced this pull request Sep 10, 2024
Followup to #168 to get CI to pass again, documents how to make a POST
only action, and adds some test coverage.

There are still a few cleanup issues but this should get things moving
on POST only actions again.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants