# Path: redditpoller/models.py
# class WatchedThread(models.Model):
#     user = models.ForeignKey(User)
#     url = models.CharField(max_length=500)
#     title = models.CharField(max_length=500)
#     thread_date = models.CharField(max_length=50)
#     archived = models.BooleanField(default=False)
#     self_post = models.BooleanField(default=False)
#     self_text = models.CharField(max_length=10000, null=True, blank=True)
#     submitted_date = models.DateTimeField(default=timezone.now)
#     last_checked_date = models.DateTimeField(default=timezone.now)
#     watch_interval = models.IntegerField(blank=True, null=True)
# 
#     def __str__(self):
#         return self.title
# 
# class WatchedComment(models.Model):
#     parent_thread = models.ForeignKey(WatchedThread, on_delete=models.CASCADE)
#     comment_author = models.CharField(max_length=100, null=True, blank=True)
#     url = models.CharField(max_length=500)
#     text = models.CharField(max_length=10000)
# 
#     def __str__(self):
#         return self.url
# 
# Path: redditpoller/poller.py
# def get_reddit_thread(reddit_url):
#     thread = r.submission(url=reddit_url)
#     return thread
# 
# def get_reddit_comments_from_thread(thread):
#     thread.comments.replace_more(limit=0)
#     thread_comments = thread.comments.list()
#     return thread_comments
# 
# def notify_user(redditor, message):
#     r.redditor(redditor).message('Reddit Watcher', message)


from django.core.management import BaseCommand
from django.utils import timezone
from redditpoller.models import WatchedThread, WatchedComment
from redditpoller.poller import get_reddit_thread, get_reddit_comments_from_thread, notify_user
from datetime import timedelta
        notifications_sent = 0
        for thread in watched_threads:
            current_time = timezone.now()
            if current_time - timedelta(hours=thread.watch_interval) < thread.last_checked_date:
                pass
            else:
                thread.last_checked_date = current_time
                thread.save()
                praw_thread = get_reddit_thread('https://reddit.com' + thread.url)
                username = thread.user.username
                if thread.archived != praw_thread.archived:
                    notify_user(username, 'The thread {} has been archived'
                                          'If you would like to watch another thread go [here.]'
                                          '(http://www.reddit.com'.format(thread.title))
                    thread.delete()
                    break
                if thread.self_text != praw_thread.selftext:
                    notify_user(username, 'The author of thread "{}" has changed the submission text.'
                                          ' You can view the post [here](https://reddit.com/{})'
                                .format(thread.title, thread.url))
                    thread.delete()
                    break
                if praw_thread.num_comments > 500:
                    notify_user(username, 'The thread {} has reached 500 comments'
                                          ' and will no longer be watched.  This tool is only designed to watch small'
                                          ' threads. If you would like to watch another thread go [here.]'
                                          '(http://www.reddit.com)'.format(thread.title))
                    thread.delete()
                    break
                linked_comments = WatchedComment.objects.filter(parent_thread=thread)

# Path: redditpoller/models.py
# class WatchedThread(models.Model):
#     user = models.ForeignKey(User)
#     url = models.CharField(max_length=500)
#     title = models.CharField(max_length=500)
#     thread_date = models.CharField(max_length=50)
#     archived = models.BooleanField(default=False)
#     self_post = models.BooleanField(default=False)
#     self_text = models.CharField(max_length=10000, null=True, blank=True)
#     submitted_date = models.DateTimeField(default=timezone.now)
#     last_checked_date = models.DateTimeField(default=timezone.now)
#     watch_interval = models.IntegerField(blank=True, null=True)
# 
#     def __str__(self):
#         return self.title
# 
# class WatchedComment(models.Model):
#     parent_thread = models.ForeignKey(WatchedThread, on_delete=models.CASCADE)
#     comment_author = models.CharField(max_length=100, null=True, blank=True)
#     url = models.CharField(max_length=500)
#     text = models.CharField(max_length=10000)
# 
#     def __str__(self):
#         return self.url
# 
# Path: redditpoller/poller.py
# def get_reddit_thread(reddit_url):
#     thread = r.submission(url=reddit_url)
#     return thread
# 
# def get_reddit_comments_from_thread(thread):
#     thread.comments.replace_more(limit=0)
#     thread_comments = thread.comments.list()
#     return thread_comments
# 
# def notify_user(redditor, message):
#     r.redditor(redditor).message('Reddit Watcher', message)


# Path: redditpoller/management/commands/check_watched_threads.py
from django.core.management import BaseCommand
from django.utils import timezone
from redditpoller.models import WatchedThread, WatchedComment
from redditpoller.poller import get_reddit_thread, get_reddit_comments_from_thread, notify_user
from datetime import timedelta

        notifications_sent = 0
        for thread in watched_threads:
            current_time = timezone.now()
            if current_time - timedelta(hours=thread.watch_interval) < thread.last_checked_date:
                pass
            else:
                thread.last_checked_date = current_time
                thread.save()
                praw_thread = get_reddit_thread('https://reddit.com' + thread.url)
                username = thread.user.username
                if thread.archived != praw_thread.archived:
                    notify_user(username, 'The thread {} has been archived'
                                          'If you would like to watch another thread go [here.]'
                                          '(http://www.reddit.com'.format(thread.title))
                    thread.delete()
                    break
                if thread.self_text != praw_thread.selftext:
                    notify_user(username, 'The author of thread "{}" has changed the submission text.'
                                          ' You can view the post [here](https://reddit.com/{})'
                                .format(thread.title, thread.url))
                    thread.delete()
                    break
                if praw_thread.num_comments > 500:
                    notify_user(username, 'The thread {} has reached 500 comments'
                                          ' and will no longer be watched.  This tool is only designed to watch small'
                                          ' threads. If you would like to watch another thread go [here.]'
                                          '(http://www.reddit.com)'.format(thread.title))
                    thread.delete()
                    break
                linked_comments = WatchedComment.objects.filter(parent_thread=thread)

                new_comments = get_reddit_comments_from_thread(praw_thread)