diff options
author | mahesh | 2016-04-01 02:14:45 +0530 |
---|---|---|
committer | mahesh | 2016-04-01 02:14:45 +0530 |
commit | 806108aca8fa91849bf10020934dd5d990d3bd50 (patch) | |
tree | 0f274eac0da1f72ee98c216e844fed8e4873e6fa /commentingapp | |
parent | 6984fe2bc799df174b0a47bf5d784912f86e0978 (diff) | |
download | Python-TBC-Interface-806108aca8fa91849bf10020934dd5d990d3bd50.tar.gz Python-TBC-Interface-806108aca8fa91849bf10020934dd5d990d3bd50.tar.bz2 Python-TBC-Interface-806108aca8fa91849bf10020934dd5d990d3bd50.zip |
disqus commenting app, which fetches, displays and notifies contributors
Diffstat (limited to 'commentingapp')
-rw-r--r-- | commentingapp/.gitignore | 3 | ||||
-rw-r--r-- | commentingapp/__init__.py | 0 | ||||
-rw-r--r-- | commentingapp/commenting_new.py | 106 | ||||
-rw-r--r-- | commentingapp/models.py | 55 | ||||
-rw-r--r-- | commentingapp/templates/commenting.html | 53 | ||||
-rw-r--r-- | commentingapp/templates/notified.html | 14 | ||||
-rw-r--r-- | commentingapp/views.py | 40 |
7 files changed, 271 insertions, 0 deletions
diff --git a/commentingapp/.gitignore b/commentingapp/.gitignore new file mode 100644 index 0000000..fad34df --- /dev/null +++ b/commentingapp/.gitignore @@ -0,0 +1,3 @@ +*.pyc +migrations/* + diff --git a/commentingapp/__init__.py b/commentingapp/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/commentingapp/__init__.py diff --git a/commentingapp/commenting_new.py b/commentingapp/commenting_new.py new file mode 100644 index 0000000..33f4923 --- /dev/null +++ b/commentingapp/commenting_new.py @@ -0,0 +1,106 @@ +# -*- coding: utf-8 -*- + +import requests +import collections +import os +from urlparse import urljoin + + + +class DisqusCommenting(object): + """ A class for getting disqus comments per url, also features getting flagged comments.""" + + base_disqus_url = "http://disqus.com/api/" + + + def check_internet_connection(self): + """ Checks for the internet connection.""" + + try: + requests.get(self.base_disqus_url, timeout = 10) + self.internet_status = {"status":True, "message": "Connection Passed."} + + except (requests.exceptions.Timeout, requests.exceptions.ConnectionError): + self.internet_status = {"status": False, "message": "Please Check the internet Connection."} + + return self.internet_status["message"] + + def check_authentication(self, public_key, forum_name, api_version=3.0): + + """ Checks if public key and forum is valid. Returns the public key, forum name for Disqus API.""" + # @TODO - Optional Authentication for read/write/moderate. + api_version = str(api_version) + try: + if self.internet_status["status"] == True: + url = urljoin(self.base_disqus_url,api_version)+"/forums/details.json" # get a better way to do this. Apparently urljoin doesnt work that way. + payload = {"api_key":public_key, "forum":forum_name} + connect_api = requests.get(url, params = payload).json() + + if connect_api["code"]== 0: + self.public_key = public_key + self.forum_name = forum_name + self.api_version = api_version + self.api_connection_status = {"status": True, "message": "Your api key and forum name are valid."} + return self.api_connection_status["message"] + + elif connect_api["code"] == 5: + self.api_connection_status = {"status": False, "message": "Your api key is invalid."} + return self.api_connection_status["message"] + + else: + self.api_connection_status = {"status": False, "message": "Your forum name is invalid."} + return self.api_connection_status["message"] + + else: + self.internet_status = {"status": False, "message": "You are still not connected to the internet. Please Check the internet Connection"} + return self.internet_status["message"] + + except AttributeError: + self.api_connection_status = {"status": False, "message": "Check your internet connection first."} + return self.api_connection_status["message"] + + def get_thread_ids(self): + """ Returns the counter for thread ids in a forum """ + + forum_url = urljoin(self.base_disqus_url,self.api_version)+"/forums/listPosts.json" # get a better way to do this. Apparently urljoin doesnt work that way. + payload = {"api_key":self.public_key,"forum": self.forum_name} + forum_data = requests.get(forum_url, params=payload).json() + thread_id_list = [thread_id["thread"] for thread_id in forum_data["response"]] + counter = collections.Counter(thread_id_list) + self.counter = counter + return counter + + def get_comments(self): + """ Returns the comments and the url of a thread """ + + json_like_list = [] + + for thread_id in self.counter.keys(): # Find a better way to do this + comment_list = [] + payload = {"api_key": self.public_key, "thread": thread_id} + thread_url = urljoin(self.base_disqus_url,self.api_version)+"/threads/list.json" + thread_data = requests.get(thread_url, params = payload).json() + comment_dict = {} + comment_dict["chapter_urls"] = thread_data["response"][0]["link"] + comment_url = urljoin(self.base_disqus_url,self.api_version)+"/threads/listPosts.json" + comment_data = requests.get(comment_url, params = payload).json() + + for comments in comment_data["response"]: + comment_list.append(comments["raw_message"]) + comment_dict["comment_list"] = comment_list + + + json_like_list.append(comment_dict) + + return json_like_list + + +if __name__ == "__main__": + x = DisqusCommenting() + + y = x.check_internet_connection() + d = x.check_authentication("enter your disqus api PUBLIC key here", 'enter disqus forum name here ') + z = x.get_thread_ids() + z1 = x.get_comments() + + print z1 # this will print out a json like list of all the urls and the comments on each url diff --git a/commentingapp/models.py b/commentingapp/models.py new file mode 100644 index 0000000..79e120e --- /dev/null +++ b/commentingapp/models.py @@ -0,0 +1,55 @@ +from __future__ import unicode_literals +from django.db import models +from tbc.models import Chapters, Book +from django.contrib.auth.models import User +from django.db.models import Q +import os +import smtplib +from email.mime.text import MIMEText + + + +class Url (models.Model): + id = models.AutoField(primary_key = True) + url = models.URLField() + + def get_contributor_details(self, counter): + notebooks = [os.path.join(chapter_name.split("/")[-2], chapter_name.split('/')[-1]) for chapter_name in counter.keys()] + contributor_list = [] + for notebook,url,number_of_comments in zip(notebooks, counter.keys(), counter.values()): + contributor_dict = {} + contributor_id = Book.objects.filter(Q(chapters__notebook = notebook)).values_list("contributor_id", flat = True) + contributor = User.objects.filter(id = contributor_id[0]).values("email", "first_name", "last_name") + contributor_dict ["contributor_email"] = contributor[0]["email"] + contributor_dict["contributor_name"] = contributor[0]["first_name"]+" "+ contributor[0]["last_name"] + contributor_dict["url"] = url + contributor_dict["number_of_comments"] = number_of_comments + contributor_list.append(contributor_dict) + return contributor_list + + def send_mail_to_contributor(self, contributor_details): + me = 'put your localhost mail id' + + for info in contributor_details: + body = """ Hi {0}, this mail is from TBC-Python Team. You have {1} unread comments for your chapter - {2}""".format(info["contributor_name"], + info["number_of_comments"], + info["url"] + ) + you = info["contributor_email"] + + message = MIMEText(body) + message["Subject"] = "You have {0} unread comment(s).".format(info["number_of_comments"]) + message ["From"] = me + message ["To"] = you + smtp_instance = smtplib.SMTP('localhost') + smtp_instance.sendmail(me, you, message.as_string()) + smtp_instance.quit() + return True + + + +class Comments(models.Model): + url = models.ForeignKey(Url, on_delete=models.CASCADE) + comments = models.TextField() + is_notified = models.BooleanField(default = False) + diff --git a/commentingapp/templates/commenting.html b/commentingapp/templates/commenting.html new file mode 100644 index 0000000..4fff5b8 --- /dev/null +++ b/commentingapp/templates/commenting.html @@ -0,0 +1,53 @@ +{% extends "base.html" %} + + {% block title %} TBC Commenting {% endblock %} + +{% block content %} + <h3><center> TBC Commenting </center></h3> + <h5> Hi <u> <b>{{user}} </b> </u> </h5> + + + {% if not url_context %} + <center><h4> There are no new comments </h4></center> + {% else %} + + <form name = "Send Email" action = "{% url 'commentingapp.views.commenting' %}" method = "POST"> + {% csrf_token %} + <table id = "comment-table" border = 2 align = "center" style="empty-cells:hide;"> + <tr> + <th> Sr. no </th> + <th > Url </th> + <th colspan = ""> Comments </th> + </tr> + + {% for urls in url_context %} + <tr> + <td colspan = ""> {{ forloop.counter }}</td> + <td id = "urls" colspan = ""><a href = "{{ urls.url }}"> {{ urls.url }} </a> </td> + <ul> + <td > + {% for comments in urls.comments_set.all %} + {% if comments.is_notified == 0 %} + <li> + {{comments.comments}} + </li> + <input type = "checkbox" name = "comment" value = "{{ urls.url }}, {{comments.comments}}"></input> + {% endif %} + {% endfor %} + + </td> + </tr> + </ul> + + + {% endfor %} + </table> + + + <p> <input class = "btn" type = "submit" value = "Submit" style = "center"> </input></p> + + </form> +{% endif %} + +{% endblock %} + diff --git a/commentingapp/templates/notified.html b/commentingapp/templates/notified.html new file mode 100644 index 0000000..c062d3f --- /dev/null +++ b/commentingapp/templates/notified.html @@ -0,0 +1,14 @@ +{% extends "base.html" %} +{% block title %} Success {% endblock %} + +{% block content %} + +<body> + +{% csrf_token %} +<h5> {{ notified_comments }} </h5> +<p></p> +<p> <a href = "{% url 'commentingapp.views.commenting' %}"> << Go back to Commenting Page </a></p> + +</body> +{% endblock %} diff --git a/commentingapp/views.py b/commentingapp/views.py new file mode 100644 index 0000000..b4c2b84 --- /dev/null +++ b/commentingapp/views.py @@ -0,0 +1,40 @@ +from django.shortcuts import render, render_to_response +from django.contrib.auth.decorators import login_required +from django.template import RequestContext +from .models import Url, Comments +from django.contrib.auth.decorators import user_passes_test +from django.db.models import Q +from tbc.models import Book, Chapters +from django.contrib.auth.models import User +from collections import Counter +import os.path +from email.mime.text import MIMEText + +@user_passes_test(lambda u:u.is_superuser, login_url="/admin/login/") + +def commenting(req): + ci = RequestContext(req) + url_instance = Url.objects.filter(Q(comments__is_notified = 0)).distinct() + context = {"url_context": url_instance, "user": req.user} + + if req.method == "POST": + notified_comment_list = req.POST.getlist("comment") + url_list = [] + for notified_comments in notified_comment_list: + url_comment_list= notified_comments.split(", ") + url_list.append(url_comment_list[0]) + Comments.objects.filter(comments = url_comment_list[1]).update(is_notified = 1) + + counter = Counter(url_list) + url_db_instance = Url() + contributor_details = url_db_instance.get_contributor_details(counter) + status = url_db_instance.send_mail_to_contributor(contributor_details) + + if status == True: + context = {"notified_comments": "You have suceesfully notified the contributors"} + else: + context = {"notified_comments": "Mail couldnot be sent"} + return render_to_response("notified.html", context, ci) + + + return render_to_response ("commenting.html", context, ci) |