<commit_before>from django.db.models import Model, CharField, FileField, DateTimeField

from django.conf import settings

from django.utils import timezone

import string
import random

class Meal(Model):
    id_length = 4
    id = CharField(max_length=id_length, db_index=True, primary_key=True)
    file = FileField(upload_to=settings.LASANA_UPLOAD_ROOT)
    expiration_time = DateTimeField(db_index=True)

    def generate_auto_id(self):
        #Theoretically, we can have up to 46656 meals, but having 10k would be enough to worry
        if Meal.objects.count() > 10000:
            raise "Too much meals"

        while True:
            random_string = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(4))
            if len(Meal.objects.filter(id=random_string)) == 0:
                self.id = random_string
                return self.id

    def is_expired(self, now=None):
        if not now:
            now = timezone.now()
        return now > self.expiration_time

    class Meta:
        ordering = ['expiration_time']

    def __unicode__(self):
        now = timezone.now()
        if now > self.expiration_time:
            return "%s, expired for %s" % (self.file.url, now - self.expiration_time)
        else:
            return "%s, expires in %s" % (self.file.url, self.expiration_time - now)
<commit_msg>Added a hook for deleting files on model remove, since Django doesn't.<commit_after>from django.db.models import Model, CharField, FileField, DateTimeField
from django.db.models.signals import pre_delete

from django.conf import settings

from django.utils import timezone

import string
import random

class Meal(Model):
    id_length = 4
    id = CharField(max_length=id_length, db_index=True, primary_key=True)
    file = FileField(upload_to=settings.LASANA_UPLOAD_ROOT)
    expiration_time = DateTimeField(db_index=True)

    def generate_auto_id(self):
        #Theoretically, we can have up to 46656 meals, but having 10k would be enough to worry
        if Meal.objects.count() > 10000:
            raise "Too much meals"

        while True:
            random_string = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(4))
            if len(Meal.objects.filter(id=random_string)) == 0:
                self.id = random_string
                return self.id

    def is_expired(self, now=None):
        if not now:
            now = timezone.now()
        return now > self.expiration_time

    class Meta:
        ordering = ['expiration_time']

    def __unicode__(self):
        now = timezone.now()
        if now > self.expiration_time:
            return "%s, expired for %s" % (self.file.url, now - self.expiration_time)
        else:
            return "%s, expires in %s" % (self.file.url, self.expiration_time - now)


def delete_file(sender, **kwargs):
    obj = kwargs['instance']
    obj.file.delete()

pre_delete.connect(delete_file, sender=Meal)

{'fraction_non_alphanumeric': 0.09543147208121827, 'fraction_numerical': 0.01015228426395939, 'mean_word_length': 2.746514575411914, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '4766385', 'n_tokens_mistral': 937, 'n_tokens_neox': 885, 'n_words': 245}