<commit_before>import datetime
import logging

from django.db import models
from django.conf import settings
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.template.base import mark_safe
from django_longliving.util import pack, get_redis_client
from object_permissions import register

from humfrey.update.utils import evaluate_pipeline

DEFINITION_STATUS_CHOICES = (
    ('idle', 'Idle'),
    ('queued', 'Queued'),
    ('active', 'Active'),
)

permission_check = lambda name : (lambda self, user : user.has_perm('update.%s_updatedefinition' % name)
                                                   or user.has_perm(name, self))

class UpdateDefinition(models.Model):
    UPDATE_QUEUE = 'humfrey:update:update-queue'

    class AlreadyQueued(AssertionError):
        pass

    slug = models.SlugField(primary_key=True)
    title = models.CharField(max_length=80)
    description = models.TextField(blank=True)

    owner = models.ForeignKey(User, related_name='owned_updates')

    cron_schedule = models.TextField(blank=True)

    status = models.CharField(max_length=10, choices=DEFINITION_STATUS_CHOICES, default='idle')

    last_queued = models.DateTimeField(null=True, blank=True)
    last_started = models.DateTimeField(null=True, blank=True)
    last_completed = models.DateTimeField(null=True, blank=True)

    class Meta:
        ordering = ('title',)
        permissions = (
            ("admin", "May use the dataset update admin pages"),
            ("view_updatedefinition", "Can view the update definition"),
            ("execute_updatedefinition", "Can perform an update"),
            ("administer_updatedefinition", "Can administer an update definition"),
        )

    can_view = permission_check('view')
    can_change = permission_check('change')
    can_execute = permission_check('execute')
    can_delete = permission_check('delete')
    can_administer = permission_check('administer')
    receives_notifications = permission_check('notifications')

    def queue(self, trigger, user=None, silent=False):
        if self.status != 'idle':
            if silent:
                return
            raise self.AlreadyQueued()
        self.status = 'queued'
        self.last_queued = datetime.datetime.now()
        self.save()

        update_log = UpdateLog.objects.create(update_definition=self,
                                              user=user,
                                              trigger=trigger,
                                              queued=self.last_queued)

        redis_client = get_redis_client()
        redis_client.lpush(self.UPDATE_QUEUE, pack(update_log))
<commit_msg>Added last udpate log relationship to UpdateDefinition model.<commit_after>import datetime
import logging

from django.db import models
from django.conf import settings
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.template.base import mark_safe
from django_longliving.util import pack, get_redis_client
from object_permissions import register

from humfrey.update.utils import evaluate_pipeline

DEFINITION_STATUS_CHOICES = (
    ('idle', 'Idle'),
    ('queued', 'Queued'),
    ('active', 'Active'),
)

permission_check = lambda name : (lambda self, user : user.has_perm('update.%s_updatedefinition' % name)
                                                   or user.has_perm(name, self))

class UpdateDefinition(models.Model):
    UPDATE_QUEUE = 'humfrey:update:update-queue'

    class AlreadyQueued(AssertionError):
        pass

    slug = models.SlugField(primary_key=True)
    title = models.CharField(max_length=80)
    description = models.TextField(blank=True)

    owner = models.ForeignKey(User, related_name='owned_updates')

    cron_schedule = models.TextField(blank=True)

    status = models.CharField(max_length=10, choices=DEFINITION_STATUS_CHOICES, default='idle')
    last_log = models.ForeignKey('UpdateLog', null=True, blank=True)

    last_queued = models.DateTimeField(null=True, blank=True)
    last_started = models.DateTimeField(null=True, blank=True)
    last_completed = models.DateTimeField(null=True, blank=True)

    class Meta:
        ordering = ('title',)
        permissions = (
            ("admin", "May use the dataset update admin pages"),
            ("view_updatedefinition", "Can view the update definition"),
            ("execute_updatedefinition", "Can perform an update"),
            ("administer_updatedefinition", "Can administer an update definition"),
        )

    can_view = permission_check('view')
    can_change = permission_check('change')
    can_execute = permission_check('execute')
    can_delete = permission_check('delete')
    can_administer = permission_check('administer')
    receives_notifications = permission_check('notifications')

    def queue(self, trigger, user=None, silent=False):
        if self.status != 'idle':
            if silent:
                return
            raise self.AlreadyQueued()
        self.status = 'queued'
        self.last_queued = datetime.datetime.now()

        update_log = UpdateLog.objects.create(update_definition=self,
                                              user=user,
                                              trigger=trigger,
                                              queued=self.last_queued)

        self.last_log = update_log
        self.save()

        redis_client = get_redis_client()
        redis_client.lpush(self.UPDATE_QUEUE, pack(update_log))

{'fraction_non_alphanumeric': 0.09636593744350028, 'fraction_numerical': 0.001446393057313325, 'mean_word_length': 2.895774647887324, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2231921', 'n_tokens_mistral': 1507, 'n_tokens_neox': 1463, 'n_words': 323}