# Path: feeds/models.py
# class Feed(models.Model):
#     title = models.CharField(max_length=200)
#     link = models.CharField(max_length=1000)
# 
#     def __unicode__(self):
#         return self.title
# 
# class Entry(models.Model):
#     title = models.CharField(max_length=200)
#     link = models.CharField(max_length=1000)
#     feed = models.ForeignKey(to=Feed)
#     published = models.DateTimeField()
# 
#     def __unicode__(self):
#         return u'{}: {}'.format(self.feed, self.title)
# 
# Path: profiles/models.py
# class UserProfile(models.Model):
#     """A user profile."""
#     user = models.ForeignKey(User)
#     feeds = models.ManyToManyField(to=Feed, through='profiles.Subscription')
#     entries = models.ManyToManyField(to=Entry,
#                                      through='profiles.UserEntryDetail')
#     next_slug = RandomSlugField(slug_length=10)
# 
#     def __unicode__(self):
#         return u"{}'s profile".format(self.user.username)
# 
#     def subscribe(self, feed):
#         Subscription.objects.create(profile=self, feed=feed)
# 
#     def unsubscribe(self, feed):
#         Subscription.objects.get(profile=self, feed=feed).delete()
#         self._get_entries(feed).delete()
# 
#     def mark_read(self, feed):
#         entries = self._get_entries(feed)
#         for entry in entries:
#             entry.read = True
#             entry.save()
# 
#     def mark_unread(self, feed):
#         entries = self._get_entries(feed)
#         for entry in entries:
#             entry.read = False
#             entry.save()
# 
#     def unread_entries(self, feed):
#         return self._get_entries(feed).filter(read=False).count()
# 
#     def _get_entries(self, feed):
#         return UserEntryDetail.objects.filter(profile=self, entry__feed=feed)
# 
# class UserEntryDetail(models.Model):
#     profile = models.ForeignKey(to=UserProfile)
#     entry = models.ForeignKey(to=Entry)
#     read = models.BooleanField()
# 
#     def __unicode__(self):
#         return u'User: {}, Entry: {}, Read: {}'.format(
#                                                 self.profile.user.username,
#                                                 self.entry,
#                                                 self.read)


from django.core.exceptions import MultipleObjectsReturned
from time import mktime
from datetime import datetime
from celery import task
from feeds.models import Feed, Entry
from profiles.models import UserProfile, UserEntryDetail
import feedparser
    parser = feedparser.parse(feed.link)

    # Add entries from feed
    entries = parser.entries
    for entry in entries:
        try:
            published_parsed = entry.published_parsed
        except AttributeError:  # an Atom feed
            published_parsed = entry.updated_parsed
        published = datetime.fromtimestamp(mktime(published_parsed))
        try:
            entry_obj, _ = Entry.objects.get_or_create(feed=feed,
                                                       title=entry.title,
                                                       link=entry.link,
                                                       published=published)
        except MultipleObjectsReturned:
            pass

        subscribers = UserProfile.objects.filter(feeds=feed)
        for profile in subscribers:
            if not UserEntryDetail.objects.filter(entry=entry_obj,
                                                  profile=profile)\
                                          .exists():
                UserEntryDetail(entry=entry_obj,
                                profile=profile,
                                read=False).save()


@task
def poll_all_feeds():

# Path: feeds/models.py
# class Feed(models.Model):
#     title = models.CharField(max_length=200)
#     link = models.CharField(max_length=1000)
# 
#     def __unicode__(self):
#         return self.title
# 
# class Entry(models.Model):
#     title = models.CharField(max_length=200)
#     link = models.CharField(max_length=1000)
#     feed = models.ForeignKey(to=Feed)
#     published = models.DateTimeField()
# 
#     def __unicode__(self):
#         return u'{}: {}'.format(self.feed, self.title)
# 
# Path: profiles/models.py
# class UserProfile(models.Model):
#     """A user profile."""
#     user = models.ForeignKey(User)
#     feeds = models.ManyToManyField(to=Feed, through='profiles.Subscription')
#     entries = models.ManyToManyField(to=Entry,
#                                      through='profiles.UserEntryDetail')
#     next_slug = RandomSlugField(slug_length=10)
# 
#     def __unicode__(self):
#         return u"{}'s profile".format(self.user.username)
# 
#     def subscribe(self, feed):
#         Subscription.objects.create(profile=self, feed=feed)
# 
#     def unsubscribe(self, feed):
#         Subscription.objects.get(profile=self, feed=feed).delete()
#         self._get_entries(feed).delete()
# 
#     def mark_read(self, feed):
#         entries = self._get_entries(feed)
#         for entry in entries:
#             entry.read = True
#             entry.save()
# 
#     def mark_unread(self, feed):
#         entries = self._get_entries(feed)
#         for entry in entries:
#             entry.read = False
#             entry.save()
# 
#     def unread_entries(self, feed):
#         return self._get_entries(feed).filter(read=False).count()
# 
#     def _get_entries(self, feed):
#         return UserEntryDetail.objects.filter(profile=self, entry__feed=feed)
# 
# class UserEntryDetail(models.Model):
#     profile = models.ForeignKey(to=UserProfile)
#     entry = models.ForeignKey(to=Entry)
#     read = models.BooleanField()
# 
#     def __unicode__(self):
#         return u'User: {}, Entry: {}, Read: {}'.format(
#                                                 self.profile.user.username,
#                                                 self.entry,
#                                                 self.read)


# Path: feeds/tasks.py
from django.core.exceptions import MultipleObjectsReturned
from time import mktime
from datetime import datetime
from celery import task
from feeds.models import Feed, Entry
from profiles.models import UserProfile, UserEntryDetail
import feedparser

    parser = feedparser.parse(feed.link)

    # Add entries from feed
    entries = parser.entries
    for entry in entries:
        try:
            published_parsed = entry.published_parsed
        except AttributeError:  # an Atom feed
            published_parsed = entry.updated_parsed
        published = datetime.fromtimestamp(mktime(published_parsed))
        try:
            entry_obj, _ = Entry.objects.get_or_create(feed=feed,
                                                       title=entry.title,
                                                       link=entry.link,
                                                       published=published)
        except MultipleObjectsReturned:
            pass

        subscribers = UserProfile.objects.filter(feeds=feed)
        for profile in subscribers:
            if not UserEntryDetail.objects.filter(entry=entry_obj,
                                                  profile=profile)\
                                          .exists():
                UserEntryDetail(entry=entry_obj,
                                profile=profile,
                                read=False).save()


@task
def poll_all_feeds():

    feeds = Feed.objects.all()