diff --git a/PythonTBC/sitemap.py b/PythonTBC/sitemap.py index 6f70945..a332adf 100644 --- a/PythonTBC/sitemap.py +++ b/PythonTBC/sitemap.py @@ -1,7 +1,23 @@ +"""sitemap.py +A sitemap is an XML file on your website that tells search-engine +indexers how frequently your pages change and how 'important' certain +pages are in relation to other pages on your site. This information +helps search engines index your site. + +The Django sitemap framework automates the creation of this XML file by +letting you express this information in Python code. + +It works much like Djangos syndication framework. To create a sitemap, +just write a Sitemap class and point to it in your URLconf. +""" from django.contrib.sitemaps import Sitemap from tbc.models import Chapters + class TbcBookSitemap(Sitemap): + """class TbcBookSitemap + class with one argument + """ changefreq = "never" priority = 0.5 diff --git a/commentingapp/commenting_new.py b/commentingapp/commenting_new.py index 33f4923..5a78a91 100644 --- a/commentingapp/commenting_new.py +++ b/commentingapp/commenting_new.py @@ -10,14 +10,14 @@ class DisqusCommenting(object): """ A class for getting disqus comments per url, also features getting flagged comments.""" - base_disqus_url = "http://disqus.com/api/" + 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) + requests.get(self.base_disqus_url, timeout=10) self.internet_status = {"status":True, "message": "Connection Passed."} except (requests.exceptions.Timeout, requests.exceptions.ConnectionError): @@ -32,7 +32,8 @@ def check_authentication(self, public_key, forum_name, api_version=3.0): 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. + 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() @@ -78,12 +79,12 @@ def get_comments(self): 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 = {} + 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() + 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"]) diff --git a/scripts/crawler/tbc_web_crawler/spiders/items.py b/scripts/crawler/tbc_web_crawler/spiders/items.py index 9dda20f..8a83f9c 100644 --- a/scripts/crawler/tbc_web_crawler/spiders/items.py +++ b/scripts/crawler/tbc_web_crawler/spiders/items.py @@ -1,18 +1,18 @@ import scrapy - class TbcErrorItems(scrapy.Item): - - - chapter_name = scrapy.Field() - chapter_urls = scrapy.Field() - completed_book_urls = scrapy.Field() - number_of_errors = scrapy.Field() - error_messages = scrapy.Field() - - + """items.py + class TbcErrorItems + """ + chapter_name = scrapy.Field() + chapter_urls = scrapy.Field() + completed_book_urls = scrapy.Field() + number_of_errors = scrapy.Field() + error_messages = scrapy.Field() class TbcBrokenItems(scrapy.Item): - - broken_url = scrapy.Field() - broken_status = scrapy.Field() + """items.py + class TbcBrokenItems + """ + broken_url = scrapy.Field() + broken_status = scrapy.Field() diff --git a/scripts/database_updater.py b/scripts/database_updater.py index 71813ea..556524a 100644 --- a/scripts/database_updater.py +++ b/scripts/database_updater.py @@ -2,7 +2,7 @@ import sys os.environ.setdefault("DJANGO_SETTINGS_MODULE", "PythonTBC.settings") -base_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +base_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.append(base_path) from commentingapp.models import Url, Comments @@ -11,40 +11,38 @@ from django.contrib.auth.models import User class CronForCommenting(object): - + """ + class CronForCommenting + """ def fetch_comments_from_script(self): """ Fetches comment from Commenting script""" commenting_instance = DisqusCommenting() check_net = commenting_instance.check_internet_connection() - check_auth = commenting_instance.check_authentication("enter your disqus api public key here", - "enter your forum name here" - ) + check_auth = commenting_instance.check_authentication("enter\ + your disqus api public key here", "enter your forum name here") thread = commenting_instance.get_thread_ids() self.comments_for_db = commenting_instance.get_comments() - return self.comments_for_db - - def add_comments_to_db(self): - if not Url.objects.exists(): """ Populates the db if empty""" for comment_details in self.comments_for_db: - url_instance = Url(url = comment_details["chapter_urls"]) #url_instance is actually an object + url_instance = Url(url=comment_details["chapter_urls"]) + #url_instance is actually an object url_instance.save() for comment in comment_details["comment_list"]: - Comments.objects.create(url = url_instance, comments = comment) + Comments.objects.create(url=url_instance, comments=comment) return "Database is created" else: """ if the db isnt empty""" for comment_details in self.comments_for_db: - url_object, url_status = Url.objects.get_or_create(url = comment_details["chapter_urls"]) - url_primary_key = url_object.pk + url_object, url_status = Url.objects.get_or_create(url=comment_details["chapter_urls"]) + url_primary_key = url_object.pk for comment in comment_details["comment_list"]: - Comments.objects.get_or_create(comments = comment, url_id = url_primary_key) + Comments.objects.get_or_create(comments=comment, url_id=url_primary_key) return "Database is updated." @@ -52,18 +50,18 @@ def delete_redundant_comments(self): "delete urls that have no comments in them anymore" url_list = [urls["chapter_urls"] for urls in self.comments_for_db] - url_list_db = Url.objects.values_list("url", flat = True) + url_list_db = Url.objects.values_list("url", flat=True) url_difference = set(url_list_db)-set(url_list) for delete_url in url_difference: - Url.objects.filter(url = delete_url).delete() + Url.objects.filter(url=delete_url).delete() "delete comments that have been deleted from tbc notebooks" for comment_details in self.comments_for_db: - url_instance = Url.objects.get(url = comment_details["chapter_urls"]) - comment_list_db = url_instance.comments_set.values_list("comments", flat = True) + url_instance = Url.objects.get(url=comment_details["chapter_urls"]) + comment_list_db = url_instance.comments_set.values_list("comments", flat=True) redundant_comment_list = set(comment_list_db)-set(comment_details["comment_list"]) for delete_comment in redundant_comment_list: - url_instance.comments_set.filter(comments = delete_comment).delete() + url_instance.comments_set.filter(comments=delete_comment).delete() return "Redundant Comments deleted." diff --git a/scripts/split_json.py b/scripts/split_json.py index baa0b90..e009602 100644 --- a/scripts/split_json.py +++ b/scripts/split_json.py @@ -1,20 +1,19 @@ import cPickle import json -from os.path import dirname, abspath,join +from os.path import dirname, abspath, join try: with open('crawler/items.json', "r") as json_dump: json_data = json.load(json_dump) json_dump.close() a = [saved_data for saved_data in json_data if str(saved_data).startswith("{u'ch")] - with open(join(dirname(abspath(dirname(__file__))),'tbc_error_page/error.pickle'), "w+") as error_json: + with open(join(dirname(abspath(dirname(__file__))), 'tbc_error_page/error.pickle'), "w+") as error_json: cPickle.dump(a, error_json) error_json.close() b = [saved_data for saved_data in json_data if str(saved_data).startswith("{u'br")] - with open(join(dirname(abspath(dirname(__file__))),'tbc_error_page/broken.pickle'), "w+") as broken_json: + with open(join(dirname(abspath(dirname(__file__))), 'tbc_error_page/broken.pickle'), "w+") as broken_json: cPickle.dump(b, broken_json) broken_json.close() - except ValueError: print "Couldn't find file" diff --git a/tbc/forms.py b/tbc/forms.py index 327c32a..c306558 100644 --- a/tbc/forms.py +++ b/tbc/forms.py @@ -1,3 +1,9 @@ +""" +With forms we will have absolute power over our interface - +we can do almost anything we can imagine! +Like every important part of Django, forms have their own +file: forms.py +""" from django import forms from django.db import models from django.contrib.auth.models import User @@ -7,33 +13,49 @@ class UserProfileForm(forms.ModelForm): + """ + class UserProfileForm + """ def __init__(self, *args, **kwargs): super(UserProfileForm, self).__init__(*args, **kwargs) self.fields['about'].label = "About Yourself" self.fields['insti_org'].label = "Institute/Organizaiton" self.fields['dept_desg'].label = "Department/Branch/Designation" self.fields['phone_no'].label = "Mobile No" - self.fields['about_proj'].label = "How did you come to know about the project" + self.fields['about_proj'].label = "How did you come to know about the\ + project" class Meta: + """ + class Meta + """ model = Profile exclude = ('user') widgets = { 'about':forms.TextInput(attrs={'placeholder':'Tell us about yourself'}), 'dob':forms.TextInput(attrs={'placeholder':'mm/dd/yyyy'}), - 'insti_org':forms.TextInput(attrs={'placeholder':'Name of University/Organizaiton(if corporate)'}), - 'dept_desg':forms.TextInput(attrs={'placeholder':'Name of the Department/Branch or your designation'}), + 'insti_org':forms.TextInput(attrs={'placeholder':'Name of University/\ + Organizaiton(if corporate)'}), + 'dept_desg':forms.TextInput(attrs={'placeholder':'Name of the \ + Department/Branch or your designation'}), 'phone_no':forms.TextInput(attrs={'placeholder':'Phone Number Please'}), } - class UserRegisterForm(UserCreationForm): + """ + class UserRegisterForm + """ class Meta: + """ + class Meta + """ model = User fields = ('first_name', 'last_name', 'email', 'username', 'password1', 'password2') - class UserLoginForm(forms.Form): + """ + class UserLoginForm + """ username = forms.CharField(widget=forms.TextInput(attrs={ 'class': 'form-control', 'placeholder': 'Username'}), label='') @@ -43,16 +65,21 @@ class UserLoginForm(forms.Form): class PasswordResetForm(forms.Form): + """ + class PasswordResetForm + """ new_password = forms.CharField(widget=forms.PasswordInput(attrs={ 'class': 'form-control', 'placeholder': 'New Password'}), label='') confirm_new_password = forms.CharField(widget=forms.PasswordInput(attrs={ 'class': 'form-control', - 'placeholder': 'Confirm New Password'}), label='') - - + 'placeholder': 'Confirm New Password\ + '}), label='') class BookForm(forms.ModelForm): + """ + class BookForm + """ def __init__(self, *args, **kwargs): super(BookForm, self).__init__(*args, **kwargs) self.fields['publisher_place'].label = "Publisher with Place" @@ -61,14 +88,23 @@ def __init__(self, *args, **kwargs): self.fields['year_of_pub'].label = "Year of Publication" self.fields['no_chapters'].label = "Number of Chapters" class Meta: + """ + class Meta + """ model = Book exclude = ('contributor', 'approved', 'reviewer') widgets = { 'title':forms.TextInput(attrs={'placeholder':'Title of the Book'}), 'author':forms.TextInput(attrs={'placeholder':'Author of the Book'}), - 'publisher_place':forms.TextInput(attrs={'placeholder':'Name of the Publisher with Place'}), - 'isbn':forms.TextInput(attrs={'placeholder':'Valid ISBN no. of the Book'}), - 'edition':forms.TextInput(attrs={'placeholder':'Edition of the Book'}), - 'year_of_pub':forms.TextInput(attrs={'placeholder':'Year when the Book was published'}), - 'no_chapters':forms.TextInput(attrs={'placeholder':'Total number of chapters in the Book (only include chapters that have solved examples)'}), + 'publisher_place':forms.TextInput(attrs={'placeholder':'Name of\ + the Publisher with Place'}), + 'isbn':forms.TextInput(attrs={'placeholder':'Valid ISBN no. of\ + the Book'}), + 'edition':forms.TextInput(attrs={'placeholder':'Edition of the\ + Book'}), + 'year_of_pub':forms.TextInput(attrs={'placeholder':'Year when the Book\ + was published'}), + 'no_chapters':forms.TextInput(attrs={'placeholder':'Total number of\ + chapters in the Book(only include chapters that have solved\ + examples)'}), } diff --git a/tbc/tests.py b/tbc/tests.py index 501deb7..b65bd9f 100644 --- a/tbc/tests.py +++ b/tbc/tests.py @@ -9,6 +9,9 @@ class SimpleTest(TestCase): + """ + class SimpleTest + """ def test_basic_addition(self): """ Tests that 1 + 1 always equals 2. diff --git a/tbc/views.py b/tbc/views.py index 7985128..b435f20 100755 --- a/tbc/views.py +++ b/tbc/views.py @@ -1,3 +1,6 @@ +""" +A view is a callable which takes a request and returns a response. This can be more than just a function, and Django provides an example of some classes which can be used as views. These allow you to structure your views and reuse code by harnessing inheritance and mixins. There are also some generic views for simple tasks which we’ll get to later, but you may want to design your own structure of reusable views which suits your use case. +""" from django.utils.encoding import force_text from django.contrib.contenttypes.models import ContentType from django.http import HttpResponse, HttpResponseRedirect, Http404 @@ -32,12 +35,12 @@ def add_log(user, object, flag, message, proposal_id=None, chat='No message'): object_repr=force_text(object), action_flag=flag, change_message=message, - proposal_id = proposal_id, - conversation = chat, + proposal_id=proposal_id, + conversation=chat, ).save() -def email_send(to,subject,msg): +def email_send(to, subject, msg): try: smtpObj = smtplib.SMTP('localhost') mail_from = "textbook@fossee.in" @@ -118,9 +121,9 @@ def Home(request): if 'sample_notebook' in request.GET: context['sample_notebook'] = True if 'cannot_submit_sample' in request.GET: - context['cannot_submit_sample'] =True + context['cannot_submit_sample'] = True if 'bookupdate' in request.GET: - context['bookupdate'] =True + context['bookupdate'] = True books = Book.objects.filter(approved=True).order_by("-id")[0:6] for book in books: @@ -140,14 +143,17 @@ def Home(request): curr_user = request.user user_profile = Profile.objects.filter(user=curr_user) - pending_proposal_list = list(Proposal.objects.filter(status="pending").order_by('id')) + pending_proposal_list = list(Proposal.objects.filter(status="pend\ + ing").order_by('id')) try: - pending_user_proposal = Proposal.objects.get(user=user_profile, status="pending") + pending_user_proposal = Proposal.objects.get( + user=user_profile, status="pending") except: pending_user_proposal = None if pending_user_proposal: - context['proposal_position'] = pending_proposal_list.index(pending_user_proposal) + 1 + context['proposal_position'] = pending_proposal_list.index( + pending_user_proposal) + 1 return render_to_response('base.html', context) @@ -238,7 +244,7 @@ def UserProfile(request): data = form.save(commit=False) data.user = request.user data.save() - add_log(user, user, CHANGE,'Profile entry') + add_log(user, user, CHANGE, 'Profile entry') return HttpResponseRedirect('/') else: context.update(csrf(request)) @@ -315,7 +321,12 @@ def ForgotPassword(request): user.set_password(password) user.save() subject = "PythonTBC: Password Reset" - message = """Dear """+user.first_name+""",\nYour password for Python TBC interface has been reset. Your credentials are:\nUsername: """+user.username+"""\nPassword: """+password+"""\n\nKindly login with the given password and update your password through the link given below.\nLink: http://tbc-python.fossee.in/update-password.\n\nThank You !\n\nRegards,\n Python TBC Team,\nFOSSEE - IIT Bombay.""" + message = """Dear """+user.first_name+""",\nYour password for Python TBC\ + interface has been reset. Your credentials are:\nUsername: """+user.username+"""\n\ + Password: """+password+"""\n\nKindly login with the given password\ + and update your password through the link given below.\nLink: \ + http://tbc-python.fossee.in/update-password.\n\nThank You !\n\nRegards,\n Python\ + TBC Team,\nFOSSEE - IIT Bombay.""" email_send(email, subject, message) form = UserLoginForm() context['form'] = form @@ -441,10 +452,17 @@ def SubmitCodeOld(request, book_id=None): chapter.screen_shots.add(screenshot) chapter.save() subject = "Python-TBC: Codes Submitted Acknowledgement" - message = """Hi """+curr_book.contributor.user.first_name+""",\nThank you for your contribution to Python TBC.\nWe have received the book & codes submitted by you.\nDetails of the book are given below: \nBook Title: """+curr_book.title+"""\nAuthor: """+curr_book.author+"""\n Publisher: """+curr_book.publisher_place+"""\nISBN: """+curr_book.isbn+"""\n\nPlease be patient while we review your book & get back to you. Review of the book will take a minimum of 45 days. Hoping for kind cooperation.""" + message = """Hi """+curr_book.contributor.user.first_name+""",\nThank you for your \ + contribution to Python TBC.\nWe have received the book & codes submitted by you\ + .\nDetails of the book are given below: \nBook Title: """+curr_book.title+"""\nAuthor:\ + """+curr_book.author+"""\n Publisher: """+curr_book.publisher_place+"""\nISBN: \ + """+curr_book.isbn+"""\n\nPlease be patient while we review your book & get back to \ + you. Review of the book will take a minimum of 45 days. Hoping for kind cooperation.""" email_send(curr_book.contributor.user.email, subject, message) subject = "Python-TBC: Book Submission" - message = """Hi """+curr_book.reviewer.name+""",\nA book has been submitted on the Python TBC interface.\n Details of the book & contributor:\n Contributor: """+curr_book.contributor.user.first_name+""" """+curr_book.contributor.user.last_name+"""\nBook Title"""+curr_book.title+"""\nAuthor: """+curr_book.title+"""\nAuthor: """+curr_book.author+"""\n Publisher: """+curr_book.publisher_place+"""\nISBN: """+curr_book.isbn+"""\nFollow the link to riview the book:\nhttp://tbc-python.fosse.in/book-review/"""+str(curr_book.id) + message = """Hi """+curr_book.reviewer.name+""",\nA book has been submitted on the \ + Python TBC interface.\n Details of the book & contributor:\n Contributor\ + : """+curr_book.contributor.user.first_name+""" """+curr_book.contributor.user.last_name+"""\nBook Title"""+curr_book.title+"""\nAuthor: """+curr_book.title+"""\nAuthor: """+curr_book.author+"""\n Publisher: """+curr_book.publisher_place+"""\nISBN: """+curr_book.isbn+"""\nFollow the link to riview the book:\nhttp://tbc-python.fosse.in/book-review/"""+str(curr_book.id) email_send(curr_book.reviewer.email, subject, message) return HttpResponseRedirect('/?up=done') else: