<commit_before>import os

from django.conf import settings
from django.db import models
from partner_feeds import tasks
from django.template.defaultfilters import slugify

# If we can import caching (IE, CacheMachine is installed) then use it
try:
    from caching.base import CachingManager as Manager, CachingMixin as Mixin
# Otherwise, just use the standard Django model without a mixin
except ImportError:
    from django.db.models import Manager

    class Mixin(object):
        pass


# set the upload path
PARTNERS_UPLOAD_PATH = getattr(settings, 'PARTNER_FEED_UPLOAD_PATH',
                               os.path.join(settings.MEDIA_ROOT, 'partner_logos'))


class Partner(Mixin, models.Model):
    """ The partner who's RSS or ATOM formated content we want to retrieve and save in the database
    """

    logo = models.ImageField(upload_to=PARTNERS_UPLOAD_PATH, blank=True)
    name = models.CharField(max_length=75)
    url = models.URLField('URL', help_text='Partner Website')
    feed_url = models.URLField(
        'Feed URL', help_text='URL of a RSS or ATOM feed', unique=True)
    date_feed_updated = models.DateTimeField(
        'Feed last updated', null=True, blank=True)

    @property
    def slug(self):
        return slugify(self.name)

    def __unicode__(self):
        return u"%s" % self.name

    def get_absolute_url(self):
        return self.url

    def save(self, *args, **kwargs):
        """ When saving a parter update it's related posts as an asynchronous Celery task
        """
        super(Partner, self).save(*args, **kwargs)
        tasks.update_posts_for_feed.delay(self)


class Post(Mixin, models.Model):
    """ Post retrieved from syndicated RSS or ATOM feed
    """

    partner = models.ForeignKey(Partner)
    title = models.CharField(max_length=255)
    url = models.URLField('URL', verify_exists=False)
    guid = models.CharField('GUID', max_length=255, unique=True)
    byline = models.CharField(max_length=255, blank=True, default='')
    date = models.DateTimeField()
    image_url = models.URLField(verify_exists=False, null=True, blank=True)
    description = models.TextField(null=True, blank=True)

    def __unicode__(self):
        return self.title

    def get_absolute_url(self):
        return self.url

    def save(self, *args, **kwargs):
        """
        If a post with the same partner_id and GUID already exists,
        then it is the same post so we should use it's ID when saving.
        """

        if not self.pk:
            old_post = Post.objects.filter(partner_id=self.partner_id, guid=self.guid)
            if len(old_post) > 0:
                self.pk = old_post[0].pk
        super(Post, self).save(*args, **kwargs)

    class Meta:
        ordering = ('-date', )<commit_msg>Fixed bugs with last commit, GUID can no longer be unique<commit_after>import os

from django.conf import settings
from django.db import models
from partner_feeds import tasks
from django.template.defaultfilters import slugify

# If we can import caching (IE, CacheMachine is installed) then use it
try:
    from caching.base import CachingManager as Manager, CachingMixin as Mixin
# Otherwise, just use the standard Django model without a mixin
except ImportError:
    from django.db.models import Manager

    class Mixin(object):
        pass


# set the upload path
PARTNERS_UPLOAD_PATH = getattr(settings, 'PARTNER_FEED_UPLOAD_PATH',
                               os.path.join(settings.MEDIA_ROOT, 'partner_logos'))


class Partner(Mixin, models.Model):
    """ The partner who's RSS or ATOM formated content we want to retrieve and save in the database
    """

    logo = models.ImageField(upload_to=PARTNERS_UPLOAD_PATH, blank=True)
    name = models.CharField(max_length=75)
    url = models.URLField('URL', help_text='Partner Website')
    feed_url = models.URLField(
        'Feed URL', help_text='URL of a RSS or ATOM feed', unique=True)
    date_feed_updated = models.DateTimeField(
        'Feed last updated', null=True, blank=True)

    @property
    def slug(self):
        return slugify(self.name)

    def __unicode__(self):
        return u"%s" % self.name

    def get_absolute_url(self):
        return self.url

    def save(self, *args, **kwargs):
        """ When saving a parter update it's related posts as an asynchronous Celery task
        """
        super(Partner, self).save(*args, **kwargs)
        tasks.update_posts_for_feed.delay(self)


class Post(Mixin, models.Model):
    """ Post retrieved from syndicated RSS or ATOM feed
    """

    partner = models.ForeignKey(Partner)
    title = models.CharField(max_length=255)
    url = models.URLField('URL', verify_exists=False)
    guid = models.CharField('GUID', max_length=255)
    byline = models.CharField(max_length=255, blank=True, default='')
    date = models.DateTimeField()
    image_url = models.URLField(verify_exists=False, null=True, blank=True)
    description = models.TextField(null=True, blank=True)

    class Meta:
        unique_together = ("guid", "partner")

    def __unicode__(self):
        return self.title

    def get_absolute_url(self):
        return self.url

    def save(self, *args, **kwargs):
        """
        If a post with the same partner_id and GUID already exists,
        then it is the same post so we should use it's ID when saving.
        """

        if not self.pk:
            old_post = Post.objects.filter(partner__id=self.partner.id, guid=self.guid)
            if len(old_post) > 0:
                self.pk = old_post[0].pk
        super(Post, self).save(*args, **kwargs)

    class Meta:
        ordering = ('-date', )
{'fraction_non_alphanumeric': 0.09927188776416267, 'fraction_numerical': 0.004617297105309891, 'mean_word_length': 3.0930232558139537, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '1424674', 'n_tokens_mistral': 1731, 'n_tokens_neox': 1664, 'n_words': 496}