summaryrefslogtreecommitdiff
path: root/tbc_error_page
diff options
context:
space:
mode:
authorankitjavalkar2016-04-18 12:04:05 +0530
committerankitjavalkar2016-04-18 12:04:05 +0530
commit32b8712249d7fa7891576b5c15c902c6604ee3d5 (patch)
treefad1c66150da512716d5654419e0098590119025 /tbc_error_page
parent8876df52d088a1de0ea769a46b82ad6fb0682a80 (diff)
parentdd0e366a19d89a249257e5e3f5bd61ad020a0430 (diff)
downloadPython-TBC-Interface-32b8712249d7fa7891576b5c15c902c6604ee3d5.tar.gz
Python-TBC-Interface-32b8712249d7fa7891576b5c15c902c6604ee3d5.tar.bz2
Python-TBC-Interface-32b8712249d7fa7891576b5c15c902c6604ee3d5.zip
Merge pull request #26 from maheshgudi/master
Adding Commenting and Error detection apps
Diffstat (limited to 'tbc_error_page')
-rw-r--r--tbc_error_page/models.py107
-rw-r--r--tbc_error_page/templates/broken.html28
-rw-r--r--tbc_error_page/templates/deliberate.html17
-rw-r--r--tbc_error_page/templates/error.html42
-rw-r--r--tbc_error_page/views.py56
5 files changed, 250 insertions, 0 deletions
diff --git a/tbc_error_page/models.py b/tbc_error_page/models.py
new file mode 100644
index 0000000..82c4da6
--- /dev/null
+++ b/tbc_error_page/models.py
@@ -0,0 +1,107 @@
+from django.db import models
+import os
+import cPickle
+
+def get_json_from_file(filename):
+ path = os.path.join(os.path.dirname(os.path.abspath(__file__)), filename)
+ if os.path.isfile(path):
+ with open(path) as json_dump:
+ json_data =cPickle.load(json_dump)
+ return json_data
+ else:
+ return False
+
+
+
+class Error(models.Model):
+
+ chapter_url = models.URLField(max_length = 255)
+ number_of_errors = models.IntegerField()
+ chapter_name = models.CharField(max_length = 200,)
+ is_deliberate = models.IntegerField(default = False)
+
+ def create_new_error_data(self, error_json_data):
+ # Populates an empty table
+ for error_details in error_json_data:
+ Error.objects.create(chapter_url = error_details["chapter_urls"],
+ chapter_name = error_details["chapter_name"],
+ number_of_errors = int(error_details["number_of_errors"]),
+ is_deliberate = 0
+ )
+
+ def delete_redundant_error_data(self, error_json_data):
+ # delete errors which have been solved
+ for error_details in error_json_data:
+ db_url_list = Error.objects.values_list("chapter_url", flat=True)
+ json_url_list = [url_list["chapter_urls"] for url_list in error_json_data]
+ c = set(db_url_list)-set(json_url_list) #change variable name.
+ for somelist in c:
+ Error.objects.filter(chapter_url = somelist).delete()
+
+ def update_error_data(self, error_json_data):
+
+ # a little more refined.
+
+ for error_details in error_json_data:
+ original_value = Error.objects.get(chapter_url = error_details["chapter_urls"]).number_of_errors
+ # if number of errors have increased
+ if original_value < error_details["number_of_errors"]:
+
+ Error.objects.filter(chapter_url = error_details["chapter_urls"])\
+ .update(number_of_errors = error_details["number_of_errors"],
+ is_deliberate = 0
+ )
+ # if number of errors have decreased
+ elif original_value > error_details["number_of_errors"]:
+ Error.objects.filter(chapter_url = error_details["chapter_urls"])\
+ .update(number_of_errors = error_details["number_of_errors"], is_deliberate = 0)
+ else:
+ # if new errors have been added.
+ Error.objects.get_or_create(chapter_url = error_details["chapter_urls"],
+ number_of_errors = error_details["number_of_errors"]
+ )
+
+ Error.objects.filter(chapter_url = error_details["chapter_urls"])\
+ .update(chapter_url = error_details["chapter_urls"],
+ number_of_errors = error_details["number_of_errors"],
+ chapter_name = error_details["chapter_name"]
+ )
+
+
+
+
+
+ def update_deliberate_error(self, deliberate_error_list):
+
+ for deliberate_urls in deliberate_error_list:
+ a = Error.objects.filter(chapter_url = deliberate_urls).update(is_deliberate = 1)
+
+
+
+
+class Broken(models.Model):
+
+ broken_url = models.URLField(max_length = 255)
+ error_status = models.IntegerField()
+
+ def create_new_broken_data(self, broken_data):
+ for broken_details in broken_data:
+
+ Broken.objects.create(broken_url = broken_details["broken_url"],
+ error_status = broken_details["broken_status"])
+
+ def delete_redundant_broken_data(self, broken_data):
+ for broken_details in broken_data:
+ db_url_list = Broken.objects.values_list("broken_url", flat=True)
+ json_url_list = [url_list["broken_url"] for url_list in broken_data]
+ redundant_url = set(db_url_list)-set(json_url_list) #change variable name.
+ for delete_url in redundant_url:
+ Broken.objects.filter(broken_url = delete_url).delete()
+
+
+ def update_broken_data(self, broken_data):
+ for broken_details in broken_data:
+
+ Broken.objects.get_or_create(broken_url = broken_details["broken_url"],
+ error_status = broken_details["broken_status"]
+ )
diff --git a/tbc_error_page/templates/broken.html b/tbc_error_page/templates/broken.html
new file mode 100644
index 0000000..841069c
--- /dev/null
+++ b/tbc_error_page/templates/broken.html
@@ -0,0 +1,28 @@
+{% extends "base.html" %}
+{% block title %} TBC Broken Links {% endblock %}
+{% block content %}
+ {% if not broken %}
+ <center><h4> There are no new comments </h4></center>
+ {% else %}
+ <h3><u><center>TBC Broken Links Page </center></u></h3>
+ <h5> Hi <b><u> {{user}} </b><u> </h5>
+ <a href = "{% url 'tbc_error_page.views.error' %}"> TBC Error Status Page </a>
+<p></p>
+ <table border = 1>
+ <tr>
+ <th> Sr no. </th>
+ <th> Broken Urls </th>
+ <th> <b> HTTP status error code </th>
+ </tr>
+ {% for broken_data in broken %}
+ <tr>
+ <td> {{ forloop.counter }} </td>
+ <td> <a href = {{ broken_data.broken_url }} target = "blank"> {{ broken_data.broken_url }} </a> </td>
+ <td><b> {{ broken_data.error_status }} </b> error </td>
+ </tr>
+ {% endfor %}
+
+ </table>
+{% endif %}
+
+{% endblock %}
diff --git a/tbc_error_page/templates/deliberate.html b/tbc_error_page/templates/deliberate.html
new file mode 100644
index 0000000..89a8974
--- /dev/null
+++ b/tbc_error_page/templates/deliberate.html
@@ -0,0 +1,17 @@
+{% extends "base.html" %}
+{% block title %} Success {% endblock %}
+{% block content %}
+<p> You have added following urls as deliberate </p>
+<table border = 1>
+<th>Urls</th>
+
+{% for deliberate_links in deliberate %}
+
+<tr><td> {{ deliberate_links }} </tr></td>
+
+
+{% endfor %}
+</table>
+<p></p>
+<p><a href = "{% url 'tbc_error_page.views.error' %}"> <<< Go back to Error Page </a></p>
+{% endblock %}
diff --git a/tbc_error_page/templates/error.html b/tbc_error_page/templates/error.html
new file mode 100644
index 0000000..237c7f3
--- /dev/null
+++ b/tbc_error_page/templates/error.html
@@ -0,0 +1,42 @@
+{% extends "base.html" %}
+{% block title %} TBC Error Page {% endblock %}
+
+
+ </head>
+
+{% block content %}
+ <body>
+ <h3><u><center>TBC Error Page </center></u></h3>
+ <h5> Hi <b><u>{{ user }} </b></u></h5>
+ <p><a href = "{% url 'tbc_error_page.views.broken' %}"> TBC Broken Links page </a></p>
+ {% if not context %}
+ <center><h4> There are no new errors </h4></center>
+ {% else %}
+ <table border = 2>
+ <tr>
+ <th> Chapters With errors</th>
+ <th> Number of errors</th>
+ <th> Delibrate Errors</th>
+ </tr>
+
+ <form name = "Send Email" action = "{% url 'tbc_error_page.views.error' %}" method = "POST"> {% csrf_token %}
+ {% for errors in context %}
+ <div class = "error"">
+
+ <tr>
+
+ {% if errors.is_deliberate == 0 %}
+ <td><a href = {{ errors.chapter_url }} target = "blank"> {{ errors.chapter_name }} </a></td>
+ <td> {{ errors.number_of_errors }} </td>
+ <td> <input type = "checkbox" name = "deliberate" value = "{{ errors.chapter_url }}">
+ {% endif %}
+ </tr>
+ </div>
+
+ {% endfor %}
+ </table>
+ <br/>
+ <input class = "btn" type = "submit" value = "Submit"> </input>
+ </form>
+{% endif %}
+{% endblock %}
diff --git a/tbc_error_page/views.py b/tbc_error_page/views.py
new file mode 100644
index 0000000..aa32453
--- /dev/null
+++ b/tbc_error_page/views.py
@@ -0,0 +1,56 @@
+from django.shortcuts import render_to_response
+from .models import Error, Broken, get_json_from_file
+from django.contrib.auth.decorators import user_passes_test
+from django.template import RequestContext
+import json
+import os
+
+
+#@login_required(login_url="/admin/login/")
+@user_passes_test(lambda u:u.is_superuser, login_url="/admin/login")
+
+
+
+def error(req):
+ ci = RequestContext(req)
+ db_instance = Error()
+ error_json_data = get_json_from_file("error.pickle")
+
+ if not Error.objects.exists():
+ db_instance.create_new_error_data(error_json_data)
+ else:
+ db_instance.delete_redundant_error_data(error_json_data)
+ db_instance.update_error_data(error_json_data)
+
+ error_details = Error.objects.filter(is_deliberate = 0)
+
+ if req.method == "POST":
+ deliberate_urls_list = req.POST.getlist("deliberate")
+ db_instance.update_deliberate_error(deliberate_urls_list)
+
+ context = {"user":req.user, "deliberate" :deliberate_urls_list}
+
+ return render_to_response ("deliberate.html", context, ci)
+
+
+ context = {"context": error_details, "user": req.user}
+ return render_to_response ("error.html", context, ci)
+
+def broken(req):
+
+ ci = RequestContext(req)
+ db_instance = Broken()
+ broken_json_data = get_json_from_file("broken.pickle")
+
+ if not Broken.objects.exists():
+ db_instance.create_new_broken_data(broken_json_data)
+
+ else:
+ db_instance.delete_redundant_broken_data(broken_json_data)
+ db_instance.update_broken_data(broken_json_data)
+
+ broken = Broken.objects.all()
+ context = {"broken": broken, "user": req.user}
+ return render_to_response("broken.html", context, ci)
+
+