# Path: redditpoller/models.py
# class WatchedSubreddit(models.Model):
#     user = models.ForeignKey(User)
#     name = models.CharField(max_length=30)
#     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.name
# 
# class WatchedSubredditThread(models.Model):
#     parent_subreddit = models.ForeignKey(WatchedSubreddit, on_delete=models.CASCADE)
#     url = models.CharField(max_length=500)
#     title = models.CharField(max_length=500)
#     thread_date = models.CharField(max_length=50)
# 
#     def __str__(self):
#         return self.title
# 
# Path: redditpoller/poller.py
# def get_subreddit_threads(subreddit_name, fetch_amount):
#     thread = r.subreddit(subreddit_name).new(limit=fetch_amount)
#     return thread
# 
# 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 WatchedSubreddit, WatchedSubredditThread
from redditpoller.poller import get_subreddit_threads, notify_user
from datetime import timedelta

class Command(BaseCommand):
    help = "Checks watched subreddits and notifies users of updates"

    def handle(self, *args, **options):
        watched_subreddits = WatchedSubreddit.objects.all()
        subreddit_count = watched_subreddits.count()
        notifications_sent = 0
        for subreddit in watched_subreddits:
            current_time = timezone.now()
            if current_time - timedelta(hours=subreddit.watch_interval) < subreddit.last_checked_date:
                pass
            else:
                subreddit.last_checked_date = current_time
                subreddit.save()
                new_threads = get_subreddit_threads(subreddit_name=subreddit.name, fetch_amount=50)

# Path: redditpoller/models.py
# class WatchedSubreddit(models.Model):
#     user = models.ForeignKey(User)
#     name = models.CharField(max_length=30)
#     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.name
# 
# class WatchedSubredditThread(models.Model):
#     parent_subreddit = models.ForeignKey(WatchedSubreddit, on_delete=models.CASCADE)
#     url = models.CharField(max_length=500)
#     title = models.CharField(max_length=500)
#     thread_date = models.CharField(max_length=50)
# 
#     def __str__(self):
#         return self.title
# 
# Path: redditpoller/poller.py
# def get_subreddit_threads(subreddit_name, fetch_amount):
#     thread = r.subreddit(subreddit_name).new(limit=fetch_amount)
#     return thread
# 
# def notify_user(redditor, message):
#     r.redditor(redditor).message('Reddit Watcher', message)


# Path: redditpoller/management/commands/check_watched_subreddits.py
from django.core.management import BaseCommand
from django.utils import timezone
from redditpoller.models import WatchedSubreddit, WatchedSubredditThread
from redditpoller.poller import get_subreddit_threads, notify_user
from datetime import timedelta


class Command(BaseCommand):
    help = "Checks watched subreddits and notifies users of updates"

    def handle(self, *args, **options):
        watched_subreddits = WatchedSubreddit.objects.all()
        subreddit_count = watched_subreddits.count()
        notifications_sent = 0
        for subreddit in watched_subreddits:
            current_time = timezone.now()
            if current_time - timedelta(hours=subreddit.watch_interval) < subreddit.last_checked_date:
                pass
            else:
                subreddit.last_checked_date = current_time
                subreddit.save()
                new_threads = get_subreddit_threads(subreddit_name=subreddit.name, fetch_amount=50)

                linked_threads = WatchedSubredditThread.objects.filter(parent_subreddit=subreddit)