From bc97050020b35ecc7377e7874002d76eabb0d14a Mon Sep 17 00:00:00 2001
From: kinitrupti
Date: Tue, 8 Nov 2016 16:49:03 +0530
Subject: Hitcount update

---
 PythonTBC/settings.py               |  1 +
 tbc/static/js/hitcount-jquery.js    | 60 +++++++++++++++++++++++++++++++++++++
 tbc/static/js/jquery.postcsrf.js    | 59 ++++++++++++++++++++++++++++++++++++
 tbc/templates/tbc/book-details.html |  5 ++--
 4 files changed, 123 insertions(+), 2 deletions(-)
 create mode 100644 tbc/static/js/hitcount-jquery.js
 create mode 100644 tbc/static/js/jquery.postcsrf.js

diff --git a/PythonTBC/settings.py b/PythonTBC/settings.py
index bd0f0b8..c6fb730 100644
--- a/PythonTBC/settings.py
+++ b/PythonTBC/settings.py
@@ -144,6 +144,7 @@ INSTALLED_APPS = (
     'tbc_error_page',
     'taggit',
     'taggit_templatetags2',
+    
 )
 
 SESSION_SERIALIZER = 'django.contrib.sessions.serializers.JSONSerializer'
diff --git a/tbc/static/js/hitcount-jquery.js b/tbc/static/js/hitcount-jquery.js
new file mode 100644
index 0000000..e7106b3
--- /dev/null
+++ b/tbc/static/js/hitcount-jquery.js
@@ -0,0 +1,60 @@
+$(document).ready(function() {
+  /**
+   * https://docs.djangoproject.com/en/dev/ref/contrib/csrf/#ajax
+   *
+   * Remember you will need to ensure csrf tokens by adding:
+   * @ensure_csrf_cookie to your views that require this javascript
+   *
+   * Also, you will probably want to include this with your other sitewide
+   * javascript files ... this is just an example.
+   */
+
+   if ( typeof hitcountJS === 'undefined' ) {
+    // since this is loaded on every page only do something
+    // if a hit is going to be counted
+    return;
+   }
+
+  var hitcountPK = hitcountJS['hitcountPK'];
+  var hitcountURL = hitcountJS['hitcountURL'];
+  var csrftoken = getCookie('csrftoken');
+
+  $.ajaxSetup({
+      beforeSend: function(xhr, settings) {
+          if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
+              xhr.setRequestHeader("X-CSRFToken", csrftoken);
+          }
+      }
+  });
+
+  $.post( hitcountURL, { "hitcountPK" : hitcountPK },
+    function(data, status) {
+
+      console.log(data); // just so you can see the response
+
+      if (data.status == 'error') {
+        // do something for error?
+      }
+    }, 'json');
+});
+
+function getCookie(name) {
+    var cookieValue = null;
+    if (document.cookie && document.cookie != '') {
+        var cookies = document.cookie.split(';');
+        for (var i = 0; i < cookies.length; i++) {
+            var cookie = jQuery.trim(cookies[i]);
+            // Does this cookie string begin with the name we want?
+            if (cookie.substring(0, name.length + 1) == (name + '=')) {
+                cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
+                break;
+            }
+        }
+    }
+    return cookieValue;
+}
+
+function csrfSafeMethod(method) {
+    // these HTTP methods do not require CSRF protection
+    return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
+}
diff --git a/tbc/static/js/jquery.postcsrf.js b/tbc/static/js/jquery.postcsrf.js
new file mode 100644
index 0000000..911626a
--- /dev/null
+++ b/tbc/static/js/jquery.postcsrf.js
@@ -0,0 +1,59 @@
+/**
+ * Wrapper for jQuery's $.post() that retrieves the CSRF token from the browser
+ * cookie and sets then sets "X-CSRFToken" header in one fell swoop.
+ *
+ * Based on the example code given at the Django docs:
+ * https://docs.djangoproject.com/en/1.9/ref/csrf/#ajax
+ *
+ * Use as you would $.post().
+ */
+
+(function($) {
+
+  $.postCSRF = function(url, data, callback, type) {
+
+    function csrfSafeMethod(method) {
+      // these HTTP methods do not require CSRF protection
+      return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
+    }
+
+    function getCookie(name) {
+      var cookieValue = null;
+      if (document.cookie && document.cookie !== '') {
+        var cookies = document.cookie.split(';');
+        for (var i = 0; i < cookies.length; i++) {
+          var cookie = jQuery.trim(cookies[i]);
+          // Does this cookie string begin with the name we want?
+          if (cookie.substring(0, name.length + 1) == (name + '=')) {
+            cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
+            break;
+          }
+        }
+      }
+      return cookieValue;
+    }
+
+    var csrftoken = getCookie('csrftoken');
+
+    // shift arguments if data argument was omitted
+    if ($.isFunction(data)) {
+      type = type || callback;
+      callback = data;
+      data = undefined;
+    }
+
+    return $.ajax(jQuery.extend({
+      url: url,
+      type: "POST",
+      dataType: type,
+      data: data,
+      success: callback,
+      beforeSend: function(xhr, settings) {
+        if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
+          xhr.setRequestHeader("X-CSRFToken", csrftoken);
+        }
+      }
+    }, jQuery.isPlainObject(url) && url));
+  };
+
+}(jQuery));
diff --git a/tbc/templates/tbc/book-details.html b/tbc/templates/tbc/book-details.html
index 0119117..579b2db 100644
--- a/tbc/templates/tbc/book-details.html
+++ b/tbc/templates/tbc/book-details.html
@@ -3,7 +3,7 @@
 
 {% block script %}
 {% load staticfiles %}
-<script src="{% static 'hitcount/jquery.postcsrf.js' %}"></script>
+<script src="{% static 'js/jquery.postcsrf.js' %}"></script>
 
 {% load hitcount_tags %}
 {% get_hit_count_js_variables for book as hitcount %}
@@ -98,11 +98,12 @@ function redirectToIpynb(notebook)
         <td>GitHub: &nbsp;&nbsp;</td>
         <td><a href= 'https://github.com/FOSSEE/Python-Textbook-Companions/tree/master/{{ book.title.split|join:"_" }}_by_{{ book.author.split|join:"_" }}' target="_blank">{{ book.title }}</a></td>
     </tr>
-<tr>
+<!--<tr>
         <td>Page Hits: &nbsp;&nbsp;</td>
 <td>{% get_hit_count for book %}</td>
 
 </tr>
+-->
 </table>
 
 </div>
-- 
cgit