Skip to content

Adding Docstrings and performance some code with PEP8 conventions #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions PythonTBC/sitemap.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
19 changes: 10 additions & 9 deletions commentingapp/commenting_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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()

Expand Down Expand Up @@ -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"])
Expand Down
26 changes: 13 additions & 13 deletions scripts/crawler/tbc_web_crawler/spiders/items.py
Original file line number Diff line number Diff line change
@@ -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()
36 changes: 17 additions & 19 deletions scripts/database_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -11,59 +11,57 @@
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."


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."


Expand Down
7 changes: 3 additions & 4 deletions scripts/split_json.py
Original file line number Diff line number Diff line change
@@ -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"
62 changes: 49 additions & 13 deletions tbc/forms.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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='')
Expand All @@ -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"
Expand All @@ -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)'}),
}
3 changes: 3 additions & 0 deletions tbc/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@


class SimpleTest(TestCase):
"""
class SimpleTest
"""
def test_basic_addition(self):
"""
Tests that 1 + 1 always equals 2.
Expand Down
Loading