From 806108aca8fa91849bf10020934dd5d990d3bd50 Mon Sep 17 00:00:00 2001 From: mahesh Date: Fri, 1 Apr 2016 02:14:45 +0530 Subject: disqus commenting app, which fetches, displays and notifies contributors --- commentingapp/.gitignore | 3 + commentingapp/__init__.py | 0 commentingapp/commenting_new.py | 106 ++++++++++++++++++++++++++++++++ commentingapp/models.py | 55 +++++++++++++++++ commentingapp/templates/commenting.html | 53 ++++++++++++++++ commentingapp/templates/notified.html | 14 +++++ commentingapp/views.py | 40 ++++++++++++ 7 files changed, 271 insertions(+) create mode 100644 commentingapp/.gitignore create mode 100644 commentingapp/__init__.py create mode 100644 commentingapp/commenting_new.py create mode 100644 commentingapp/models.py create mode 100644 commentingapp/templates/commenting.html create mode 100644 commentingapp/templates/notified.html create mode 100644 commentingapp/views.py (limited to 'commentingapp') 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 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 %} +

TBC Commenting

+
Hi {{user}}
+ + + {% if not url_context %} +

There are no new comments

+ {% else %} + +
+ {% csrf_token %} + + + + + + + + {% for urls in url_context %} + + + + + + + + + {% endfor %} +
Sr. no Url Comments
{{ forloop.counter }} {{ urls.url }} + {% for comments in urls.comments_set.all %} + {% if comments.is_notified == 0 %} +
  • + {{comments.comments}} +
  • + + {% endif %} + {% endfor %} + +
    + + +

    + +
    +{% 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 %} + + + +{% csrf_token %} +
    {{ notified_comments }}
    +

    +

    << Go back to Commenting Page

    + + +{% 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) -- cgit From 42bd8594b84a20b9be75c4bdadb490209e0fee81 Mon Sep 17 00:00:00 2001 From: mahesh Date: Fri, 15 Apr 2016 13:50:51 +0530 Subject: added direct link to go disqus admin page --- commentingapp/templates/commenting.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'commentingapp') diff --git a/commentingapp/templates/commenting.html b/commentingapp/templates/commenting.html index 4fff5b8..81961a8 100644 --- a/commentingapp/templates/commenting.html +++ b/commentingapp/templates/commenting.html @@ -4,8 +4,8 @@ {% block content %}

    TBC Commenting

    -
    Hi {{user}}
    - +
    Hi {{user}}

    + Go to Disqus admin Page {% if not url_context %}

    There are no new comments

    -- cgit From 664919e0ac7c586b27f6eb862c885a75ab62d8ef Mon Sep 17 00:00:00 2001 From: mahesh Date: Fri, 15 Apr 2016 13:54:15 +0530 Subject: url link will open a new tab --- commentingapp/templates/commenting.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'commentingapp') diff --git a/commentingapp/templates/commenting.html b/commentingapp/templates/commenting.html index 81961a8..09ae53f 100644 --- a/commentingapp/templates/commenting.html +++ b/commentingapp/templates/commenting.html @@ -23,7 +23,7 @@ {% for urls in url_context %} {{ forloop.counter }} - {{ urls.url }} + {{ urls.url }}