<commit_before>import datetime

from time import mktime

from django.db import models
from django.contrib.auth.models import User
from django.conf import settings

from hadrian.utils.slugs import unique_slugify

import feedparser

from .managers import FeedItemManager


class Category(models.Model):
    name = models.CharField(max_length=250, blank=True)
    slug = models.SlugField(blank=True, null=True, editable=False)
    user = models.ForeignKey(User, blank=True, null=True)

    def __unicode__(self):
        return self.name

    def save(self, *args, **kwargs):
        unique_slugify(self, self.name)
        super(Category, self).save(*args, **kwargs)

    @property
    def get_unread_count(self):
        return FeedItem.objects.my_feed_items(self.user).category(self.slug).un_read().count()


class Feed(models.Model):
    class Meta:
        unique_together = (
            ("url", "user"),
        )

    link = models.CharField(blank=True, max_length=450)
    url = models.CharField(blank=True, max_length=450)
    title = models.CharField(blank=True, null=True, max_length=250)

    category = models.ForeignKey(Category, blank=True, null=True)
    user = models.ForeignKey(User, blank=True, null=True)
    last_update = models.DateField(blank=True, null=True, editable=False)

    def __unicode__(self):
        return self.url

    def _get_title(self):
        parser = feedparser.parse(self.url)
        return parser.feed.title

    def save(self, *args, **kwargs):
        if not self.title:
            self.title = self._get_title()
        super(Feed, self).save(*args, **kwargs)
        if self.last_update is None:
            self.update(force=True)

    @property
    def get_unread_count(self):
        return FeedItem.objects.filter(feed=self).un_read().count()

    def _update_feed(self):
        # Update the last update field
        self.last_update = datetime.date.today()
        self.save()
        for item in feedparser.parse(self.url).entries[:10]:
            # Search for an existing item
            if item.has_key("id"):
                guid = item.id
            else:
                guid = item.link
            try:
                # FIXME(halldor): this should use the feed item UID<commit_msg>Add feed link (the web page, not the feed xml url) to the Feed object on update.<commit_after>import datetime

from time import mktime

from django.db import models
from django.contrib.auth.models import User
from django.conf import settings

from hadrian.utils.slugs import unique_slugify

import feedparser

from .managers import FeedItemManager


class Category(models.Model):
    name = models.CharField(max_length=250, blank=True)
    slug = models.SlugField(blank=True, null=True, editable=False)
    user = models.ForeignKey(User, blank=True, null=True)

    def __unicode__(self):
        return self.name

    def save(self, *args, **kwargs):
        unique_slugify(self, self.name)
        super(Category, self).save(*args, **kwargs)

    @property
    def get_unread_count(self):
        return FeedItem.objects.my_feed_items(self.user).category(self.slug).un_read().count()


class Feed(models.Model):
    class Meta:
        unique_together = (
            ("url", "user"),
        )

    link = models.CharField(blank=True, max_length=450)
    url = models.CharField(blank=True, max_length=450)
    title = models.CharField(blank=True, null=True, max_length=250)

    category = models.ForeignKey(Category, blank=True, null=True)
    user = models.ForeignKey(User, blank=True, null=True)
    last_update = models.DateField(blank=True, null=True, editable=False)

    def __unicode__(self):
        return self.url

    def _get_title(self):
        parser = feedparser.parse(self.url)
        return parser.feed.title

    def save(self, *args, **kwargs):
        if not self.title:
            self.title = self._get_title()
        super(Feed, self).save(*args, **kwargs)
        if self.last_update is None:
            self.update(force=True)

    @property
    def get_unread_count(self):
        return FeedItem.objects.filter(feed=self).un_read().count()

    def _update_feed(self):
        # Update the last update field
        feed = feedparser.parse(self.url)
        self.last_update = datetime.date.today()
        if feed.feed.has_key("link"):
            self.link = feed.feed.link
        else:
            self.link = ""
        self.save()
        for item in feed.entries[:10]:
            # The RSS spec doesn't require the guid field so fall back on link
            if item.has_key("id"):
                guid = item.id
            else:
                guid = item.link

            # Search for an existing item
            try:
{'fraction_non_alphanumeric': 0.10875556497774008, 'fraction_numerical': 0.005935976256094976, 'mean_word_length': 2.697492163009404, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 1}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '60483', 'n_tokens_mistral': 1456, 'n_tokens_neox': 1398, 'n_words': 338}