<commit_before>import re
from datetime import datetime
import calendar

from django.db import models, transaction, connection
from django.contrib import admin
from django.utils.html import strip_tags
from django.utils.translation import ugettext_lazy as _

from ella.core.box import Box
from ella.core.cache.utils import get_cached_object_or_404

class Server(models.Model):
    title = models.CharField(_('Title'), maxlength=100)
    domain = models.URLField(_('Domain'), verify_exists=False)
    slug = models.CharField(db_index=True, maxlength=100)
    url = models.URLField(_('Atom URL'), verify_exists=False, max_length=300)


    def regenerate (self):
        return u'<a href="%s/fetch/">%s - %s</a>' % (self.id, _('Fetch'), self.title)
    regenerate.allow_tags = True

    def __unicode__(self):
        return self.title

    class Meta:
        verbose_name = _('Server')
        verbose_name_plural = _('Servers')
        ordering = ('title',)

    def get_imports_by_time(self):
        return self.serveritem_set.order_by("-updated")

    def get_imports_by_priority(self):
        return self.serveritem_set.order_by("-priority", "-updated")

    @transaction.commit_on_success
    def fetch(self):
        try:
            import feedparser
        except ImportError:
            from django.core.exceptions import ImproperlyConfigured
            raise ImproperlyConfigured, "You must have feedparser installed in order to use automated imports."

        output = feedparser.parse(self.url)
        if output['status'] == 200:
            # for all articles in last but not in this import reset priority to 0
            cursor = connection.cursor()
            cursor.execute(
                    'UPDATE %(table)s SET %(priority)s = 0 WHERE %(priority)s > 0' % {
                        'table' : connection.ops.quote_name(self._meta.db_table),
                        'priority' : connection.ops.quote_name(self._meta.get_field('priority')),
},
                    []
)
            importlen =  len(output['entries'])
            for index in range(importlen):
                e = output['entries'][index]
                si, created = ServerItem.objects.get_or_create(
                    server=self,
                    link=e['link'],
                    defaults={
                        'priority': importlen - index,
                        'title' : e['title'],
                        'updated' : datetime.utcfromtimestamp(calendar.timegm(e['updated_parsed'])),
                        'summary' : e['summary'],
}
)
                # repair priority if needed
                if not created and si.priority >= 0:
                    # unwanted import item has negative priority - never more update priority
                    si.priority = importlen - index
                    si.save()

PHOTO_REG = re.compile(r'<img[^>]*src="(?P<url>http://[^"]*)"')
<commit_msg>Repair priority reset query to relevant table/column<commit_after>import re
from datetime import datetime
import calendar

from django.db import models, transaction, connection
from django.contrib import admin
from django.utils.html import strip_tags
from django.utils.translation import ugettext_lazy as _

from ella.core.box import Box
from ella.core.cache.utils import get_cached_object_or_404

class Server(models.Model):
    title = models.CharField(_('Title'), maxlength=100)
    domain = models.URLField(_('Domain'), verify_exists=False)
    slug = models.CharField(db_index=True, maxlength=100)
    url = models.URLField(_('Atom URL'), verify_exists=False, max_length=300)


    def regenerate (self):
        return u'<a href="%s/fetch/">%s - %s</a>' % (self.id, _('Fetch'), self.title)
    regenerate.allow_tags = True

    def __unicode__(self):
        return self.title

    class Meta:
        verbose_name = _('Server')
        verbose_name_plural = _('Servers')
        ordering = ('title',)

    def get_imports_by_time(self):
        return self.serveritem_set.order_by("-updated")

    def get_imports_by_priority(self):
        return self.serveritem_set.order_by("-priority", "-updated")

    @transaction.commit_on_success
    def fetch(self):
        try:
            import feedparser
        except ImportError:
            from django.core.exceptions import ImproperlyConfigured
            raise ImproperlyConfigured, "You must have feedparser installed in order to use automated imports."

        output = feedparser.parse(self.url)
        if output['status'] == 200:
            # for all articles in last but not in this import reset priority to 0
            cursor = connection.cursor()
            cursor.execute(
                    'UPDATE %(table)s SET %(priority)s = 0 WHERE %(priority)s > 0' % {
                        'table' : connection.ops.quote_name(ServerItem._meta.db_table),
                        'priority' : connection.ops.quote_name(ServerItem._meta.get_field('priority').column),
},
                    []
)
            importlen =  len(output['entries'])
            for index in range(importlen):
                e = output['entries'][index]
                si, created = ServerItem.objects.get_or_create(
                    server=self,
                    link=e['link'],
                    defaults={
                        'priority': importlen - index,
                        'title' : e['title'],
                        'updated' : datetime.utcfromtimestamp(calendar.timegm(e['updated_parsed'])),
                        'summary' : e['summary'],
}
)
                # repair priority if needed
                if not created and si.priority >= 0:
                    # unwanted import item has negative priority - never more update priority
                    si.priority = importlen - index
                    si.save()

PHOTO_REG = re.compile(r'<img[^>]*src="(?P<url>http://[^"]*)"')

{'fraction_non_alphanumeric': 0.11031420765027322, 'fraction_numerical': 0.006489071038251366, 'mean_word_length': 2.2199010445299616, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, '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': '5249118', 'n_tokens_mistral': 1647, 'n_tokens_neox': 1554, 'n_words': 390}