diff mbox series

[v2,1/5] static: add JS Cookie Library to get csrftoken for fetch requests

Message ID 20210722171251.2554142-2-raxel@google.com
State Superseded
Headers show
Series [v2,1/5] static: add JS Cookie Library to get csrftoken for fetch requests | expand
Related show

Commit Message

Raxel Gutierrez July 22, 2021, 5:12 p.m. UTC
Currently, requests are made only through form submission and the
csrftoken is added to templates using {% csrf_token %}. Following Django
docs [1], the library is useful to add csrftoken when making requests in
JavaScript.

[1] https://docs.djangoproject.com/en/3.2/ref/csrf/#ajax

Signed-off-by: Raxel Gutierrez <raxel@google.com>
---
 htdocs/README.rst          | 9 +++++++++
 htdocs/js/js.cookie.min.js | 3 +++
 templates/base.html        | 1 +
 3 files changed, 13 insertions(+)
 create mode 100644 htdocs/js/js.cookie.min.js

Comments

Jonathan Nieder July 26, 2021, 11:53 p.m. UTC | #1
Hi,

Raxel Gutierrez wrote:

> Currently, requests are made only through form submission and the
> csrftoken is added to templates using {% csrf_token %}. Following Django
> docs [1], the library is useful to add csrftoken when making requests in
> JavaScript.
>
> [1] https://docs.djangoproject.com/en/3.2/ref/csrf/#ajax
>
> Signed-off-by: Raxel Gutierrez <raxel@google.com>
> ---
>  htdocs/README.rst          | 9 +++++++++
>  htdocs/js/js.cookie.min.js | 3 +++
>  templates/base.html        | 1 +
>  3 files changed, 13 insertions(+)
>  create mode 100644 htdocs/js/js.cookie.min.js

Just moments ago, 3.0.0 came out, which is importable as a js module.
Worth trying?

With or without that change,
Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>

Thanks.
Daniel Axtens July 27, 2021, 12:01 a.m. UTC | #2
Raxel Gutierrez <raxel@google.com> writes:

> Currently, requests are made only through form submission and the
> csrftoken is added to templates using {% csrf_token %}. Following Django
> docs [1], the library is useful to add csrftoken when making requests in
> JavaScript.
>
> [1] https://docs.djangoproject.com/en/3.2/ref/csrf/#ajax
>
> Signed-off-by: Raxel Gutierrez <raxel@google.com>
> ---
>  htdocs/README.rst          | 9 +++++++++
>  htdocs/js/js.cookie.min.js | 3 +++
>  templates/base.html        | 1 +
>  3 files changed, 13 insertions(+)
>  create mode 100644 htdocs/js/js.cookie.min.js
>
> diff --git a/htdocs/README.rst b/htdocs/README.rst
> index 62f15c2..fa1616c 100644
> --- a/htdocs/README.rst
> +++ b/htdocs/README.rst
> @@ -122,6 +122,15 @@ js
>    :GitHub: jQuery plug-in to drag and drop rows in HTML tables
>    :Version: ???
>  
> +``js.cookie.min.js``
> +
> +  Library used to handle cookies.
> +
> +  This is used to get the ``csrftoken`` cookie for AJAX requests in JavaScript.
> +
> +  :GitHub: https://github.com/js-cookie/js-cookie/
> +  :Version: 2.2.1
> +
>  ``selectize.min.js``
>  
>    Selectize is the hybrid of a ``textbox`` and ``<select>`` box. It's jQuery
> diff --git a/htdocs/js/js.cookie.min.js b/htdocs/js/js.cookie.min.js
> new file mode 100644
> index 0000000..f5f4c36
> --- /dev/null
> +++ b/htdocs/js/js.cookie.min.js
> @@ -0,0 +1,3 @@
> +/*! js-cookie v2.2.1 | MIT */
> +
> +!function(a){var b;if("function"==typeof define&&define.amd&&(define(a),b=!0),"object"==typeof exports&&(module.exports=a(),b=!0),!b){var c=window.Cookies,d=window.Cookies=a();d.noConflict=function(){return window.Cookies=c,d}}}(function(){function a(){for(var a=0,b={};a<arguments.length;a++){var c=arguments[a];for(var d in c)b[d]=c[d]}return b}function b(a){return a.replace(/(%[0-9A-Z]{2})+/g,decodeURIComponent)}function c(d){function e(){}function f(b,c,f){if("undefined"!=typeof document){f=a({path:"/"},e.defaults,f),"number"==typeof f.expires&&(f.expires=new Date(1*new Date+864e5*f.expires)),f.expires=f.expires?f.expires.toUTCString():"";try{var g=JSON.stringify(c);/^[\{\[]/.test(g)&&(c=g)}catch(j){}c=d.write?d.write(c,b):encodeURIComponent(c+"").replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g,decodeURIComponent),b=encodeURIComponent(b+"").replace(/%(23|24|26|2B|5E|60|7C)/g,decodeURIComponent).replace(/[\(\)]/g,escape);var h="";for(var i in f)f[i]&&(h+="; "+
 i,
>  !0!==f[i]&&(h+="="+f[i].split(";")[0]));return document.cookie=b+"="+c+h}}function g(a,c){if("undefined"!=typeof document){for(var e={},f=document.cookie?document.cookie.split("; "):[],g=0;g<f.length;g++){var h=f[g].split("="),i=h.slice(1).join("=");c||'"'!==i.charAt(0)||(i=i.slice(1,-1));try{var j=b(h[0]);if(i=(d.read||d)(i,j)||b(i),c)try{i=JSON.parse(i)}catch(k){}if(e[j]=i,a===j)break}catch(k){}}return a?e[a]:e}}return e.set=f,e.get=function(a){return g(a,!1)},e.getJSON=function(a){return g(a,!0)},e.remove=function(b,c){f(b,"",a(c,{expires:-1}))},e.defaults={},e.withConverter=c,e}return c(function(){})});
> \ No newline at end of file

Amusingly applying this patch with git-pw breaks somehow: I don't get
the final line of js.cookie.min.js... another bug somewhere for someone to
follow up at some point in time!

Anyway I replaced it in my local tree with the unminified version and
proceeded. If you can point me to a canoncal source for the minified
version, I can pull that in.

Kind regards,
Daniel

> diff --git a/templates/base.html b/templates/base.html
> index 8accb4c..8700602 100644
> --- a/templates/base.html
> +++ b/templates/base.html
> @@ -21,6 +21,7 @@
>    <script src="{% static "js/bootstrap.min.js" %}"></script>
>    <script src="{% static "js/selectize.min.js" %}"></script>
>    <script src="{% static "js/clipboard.min.js" %}"></script>
> +  <script src="{% static "js/js.cookie.min.js" %}"></script>
>    <script>
>     $(document).ready(function() {
>         new Clipboard(document.querySelectorAll('button.btn-copy'));
> -- 
> 2.32.0.432.gabb21c7263-goog
>
> _______________________________________________
> Patchwork mailing list
> Patchwork@lists.ozlabs.org
> https://lists.ozlabs.org/listinfo/patchwork
Raxel Gutierrez July 27, 2021, 3:51 p.m. UTC | #3
> Anyway I replaced it in my local tree with the unminified version and
> proceeded. If you can point me to a canoncal source for the minified
> version, I can pull that in.

As Jonathan commented, I will be updating to v3 of the library. The
new file can be found here:
https://github.com/js-cookie/js-cookie/releases/tag/v3.0.0.

Best,
Raxel
diff mbox series

Patch

diff --git a/htdocs/README.rst b/htdocs/README.rst
index 62f15c2..fa1616c 100644
--- a/htdocs/README.rst
+++ b/htdocs/README.rst
@@ -122,6 +122,15 @@  js
   :GitHub: jQuery plug-in to drag and drop rows in HTML tables
   :Version: ???
 
+``js.cookie.min.js``
+
+  Library used to handle cookies.
+
+  This is used to get the ``csrftoken`` cookie for AJAX requests in JavaScript.
+
+  :GitHub: https://github.com/js-cookie/js-cookie/
+  :Version: 2.2.1
+
 ``selectize.min.js``
 
   Selectize is the hybrid of a ``textbox`` and ``<select>`` box. It's jQuery
diff --git a/htdocs/js/js.cookie.min.js b/htdocs/js/js.cookie.min.js
new file mode 100644
index 0000000..f5f4c36
--- /dev/null
+++ b/htdocs/js/js.cookie.min.js
@@ -0,0 +1,3 @@ 
+/*! js-cookie v2.2.1 | MIT */
+
+!function(a){var b;if("function"==typeof define&&define.amd&&(define(a),b=!0),"object"==typeof exports&&(module.exports=a(),b=!0),!b){var c=window.Cookies,d=window.Cookies=a();d.noConflict=function(){return window.Cookies=c,d}}}(function(){function a(){for(var a=0,b={};a<arguments.length;a++){var c=arguments[a];for(var d in c)b[d]=c[d]}return b}function b(a){return a.replace(/(%[0-9A-Z]{2})+/g,decodeURIComponent)}function c(d){function e(){}function f(b,c,f){if("undefined"!=typeof document){f=a({path:"/"},e.defaults,f),"number"==typeof f.expires&&(f.expires=new Date(1*new Date+864e5*f.expires)),f.expires=f.expires?f.expires.toUTCString():"";try{var g=JSON.stringify(c);/^[\{\[]/.test(g)&&(c=g)}catch(j){}c=d.write?d.write(c,b):encodeURIComponent(c+"").replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g,decodeURIComponent),b=encodeURIComponent(b+"").replace(/%(23|24|26|2B|5E|60|7C)/g,decodeURIComponent).replace(/[\(\)]/g,escape);var h="";for(var i in f)f[i]&&(h+="; "+i,
 !0!==f[i]&&(h+="="+f[i].split(";")[0]));return document.cookie=b+"="+c+h}}function g(a,c){if("undefined"!=typeof document){for(var e={},f=document.cookie?document.cookie.split("; "):[],g=0;g<f.length;g++){var h=f[g].split("="),i=h.slice(1).join("=");c||'"'!==i.charAt(0)||(i=i.slice(1,-1));try{var j=b(h[0]);if(i=(d.read||d)(i,j)||b(i),c)try{i=JSON.parse(i)}catch(k){}if(e[j]=i,a===j)break}catch(k){}}return a?e[a]:e}}return e.set=f,e.get=function(a){return g(a,!1)},e.getJSON=function(a){return g(a,!0)},e.remove=function(b,c){f(b,"",a(c,{expires:-1}))},e.defaults={},e.withConverter=c,e}return c(function(){})});
\ No newline at end of file
diff --git a/templates/base.html b/templates/base.html
index 8accb4c..8700602 100644
--- a/templates/base.html
+++ b/templates/base.html
@@ -21,6 +21,7 @@ 
   <script src="{% static "js/bootstrap.min.js" %}"></script>
   <script src="{% static "js/selectize.min.js" %}"></script>
   <script src="{% static "js/clipboard.min.js" %}"></script>
+  <script src="{% static "js/js.cookie.min.js" %}"></script>
   <script>
    $(document).ready(function() {
        new Clipboard(document.querySelectorAll('button.btn-copy'));