summaryrefslogtreecommitdiff
path: root/scripts/database_updater.py
diff options
context:
space:
mode:
authormahesh2016-04-15 16:17:40 +0530
committermahesh2016-04-15 16:17:40 +0530
commitf2404683c3098eed71bc414d61dadb4c8b7c37b2 (patch)
tree3ab9f96b77dd3a008d5c655e29a2ebd4769ba9b0 /scripts/database_updater.py
parent6ebf233256a1892808d3a9535d6f602e52915381 (diff)
downloadPython-TBC-Interface-f2404683c3098eed71bc414d61dadb4c8b7c37b2.tar.gz
Python-TBC-Interface-f2404683c3098eed71bc414d61dadb4c8b7c37b2.tar.bz2
Python-TBC-Interface-f2404683c3098eed71bc414d61dadb4c8b7c37b2.zip
Deletes comments/urls which have been deleted from tbc notebooks
Diffstat (limited to 'scripts/database_updater.py')
-rw-r--r--scripts/database_updater.py25
1 files changed, 24 insertions, 1 deletions
diff --git a/scripts/database_updater.py b/scripts/database_updater.py
index 5148548..71813ea 100644
--- a/scripts/database_updater.py
+++ b/scripts/database_updater.py
@@ -47,9 +47,32 @@ class CronForCommenting(object):
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_difference = set(url_list_db)-set(url_list)
+ for delete_url in url_difference:
+ 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)
+ 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()
+ return "Redundant Comments deleted."
+
+
+
if __name__ == '__main__':
a = CronForCommenting()
b = a.fetch_comments_from_script()
- c = a.add_comments_to_db()
+ c = a.add_comments_to_db() #This should always be before delete_redundant_comments
+ d = a.delete_redundant_comments() #This should always be after add_comments_to_db
print c
+ print d