<commit_before>import uuid
from os.path import join
from shutil import rmtree

from django.conf import settings
from django.contrib.auth.models import User, Group
from django.db import models
from django.forms import ModelForm
from django.utils.translation import ugettext_lazy as _


class StateError(Exception):
    def __init__(self, value):
        self.value = value

    def __str__(self):
        return repr(self.value)

class State(models.Model):
    name = models.CharField(unique=True, max_length=32)
    next_states = models.ManyToManyField("self", symmetrical=False, blank=True)
    responsible = models.ManyToManyField(Group)
    sort_order = models.PositiveSmallIntegerField()

    def __unicode__(self):
        return self.name

    def transitionTo(self, state):
        if not isinstance(state, State):
            raise TypeError("'%s' is not a registered state" % state)
        if not state in self.next_states.all():
            raise StateError("Cannot transition to %s from %s" % (self.name, state.name))
        return state
    
    # used for admin view
    def all_next_states(self):
        return ",".join([state.name for state in self.next_states.all()])

    # used for admin view
    def all_responsible(self):
        return ",".join([group.name for group in self.responsible.all()])

    class Meta:
        ordering = ['sort_order']

class Document(models.Model):

    title = models.CharField(
        _("Title"),
        max_length=255, 
        help_text=_("The title of the DTB, including any subtitles"))
    author = models.CharField(
        _("Author"),
        max_length=255, 
        help_text=_("Names of primary author or creator of the intellectual content of the publication"),
        blank=True)
    subject = models.CharField(
        _("Subject"),
        max_length=255, 
        help_text=_("The topic of the content of the publication"),
        blank=True)
    description = models.TextField(
        _("Description"),
        help_text=_("Plain text describing the publication's content"),
        blank=True)
    publisher = models.CharField(
        _("Publisher"),
        max_length=255,
        default=settings.DAISY_DEFAULT_PUBLISHER,
        help_text=_("The agency responsible for making the DTB available"))
    date = models.DateField(
        _("Date"),
        auto_now_add=True,
        help_text=_("Date of publication of the DTB"))
    identifier = models.CharField(
        _("Identifier"),
        max_length=255, 
        help_text=_("A string or number identifying the DTB"))
    source = models.CharField(
        _("Source"),
        max_length=20, 
        help_text=_("A reference to a resource (e.g., a print original, ebook, etc.) from which the DTB is derived. Best practice is to use the ISBN when available"), 
        blank=True)
    language_choices = (('de', 'de'),
                        ('de-1901', 'de-1901',),
#                        ('de-CH', 'de-CH',),
                        )
    language = models.CharField(
        _("Language"),
        max_length=10,
        choices=language_choices,
        help_text=_("Language of the content of the publication"))
    rights = models.CharField(
        _("Rights"),
        max_length=255, 
        help_text=_("Information about rights held in and over the DTB"),
        blank=True)
    
    source_date = models.DateField(
        _("Source Date"),
        help_text=_("Date of publication of the resource (e.g., a print original, ebook, etc.) from which the DTB is derived"),
        null=True, blank=True)
    source_edition = models.CharField(
        _("Source Edition"), 
        max_length=255, 
        help_text=_("A string describing the edition of the resource (e.g., a print original, ebook, etc.) from which the DTB is derived"),
        blank=True)
    source_publisher = models.CharField(
        _("Source Publisher"), 
        max_length=255, 
        help_text=_("The agency responsible for making available the resource (e.g., a print original, ebook, etc.) from which the DTB is derived"),
        blank=True)
    source_rights = models.CharField(
        _("Source Rights"), 
        max_length=255, 
        help_text=_("Information about rights held in and over the resource (e.g., a print original, ebook, etc.) from which the DTB is derived"),
        blank=True)

    state = models.ForeignKey(State, verbose_name=_("State"))
    assigned_to = models.ForeignKey(User, verbose_name=_("Assigned to"), null=True, blank=True)
    created_at = models.DateTimeField(_("Created"), auto_now_add=True)
    modified_at = models.DateTimeField(_("Last Modified"), auto_now=True)

    def __unicode__(self):
        return self.title

    def latest_version(self):
        return self.version_set.latest()

    def transitionTo(self, state):
        self.assigned_to = None
        self.state = self.state.transitionTo(state)
        self.save()

    def save(self):
        # set initial state
        if not self.pk:
            self.state = State.objects.filter(name='new')[0]
        if not self.identifier:
            self.identifier = "ch-sbs-%s" % str(uuid.uuid4())
        super(Document, self).save()

    def delete(self, *args, **kwargs):
        old_id = self.id
        super(Document, self).delete(*args, **kwargs)
        # remove the folders for versions and attachments on the file system
        rmtree(join(settings.MEDIA_ROOT, str(old_id)))

def get_version_path(instance, filename):
        return '%s/versions/%s.xml' % (instance.document_id, instance.id)
    
class Version(models.Model):
    comment = models.CharField(max_length=255)
    document = models.ForeignKey(Document)
    content = models.FileField(upload_to=get_version_path)<commit_msg>Make sure the dc:Date is only a date<commit_after>import uuid, datetime
from os.path import join
from shutil import rmtree

from django.conf import settings
from django.contrib.auth.models import User, Group
from django.db import models
from django.forms import ModelForm
from django.utils.translation import ugettext_lazy as _


class StateError(Exception):
    def __init__(self, value):
        self.value = value

    def __str__(self):
        return repr(self.value)

class State(models.Model):
    name = models.CharField(unique=True, max_length=32)
    next_states = models.ManyToManyField("self", symmetrical=False, blank=True)
    responsible = models.ManyToManyField(Group)
    sort_order = models.PositiveSmallIntegerField()

    def __unicode__(self):
        return self.name

    def transitionTo(self, state):
        if not isinstance(state, State):
            raise TypeError("'%s' is not a registered state" % state)
        if not state in self.next_states.all():
            raise StateError("Cannot transition to %s from %s" % (self.name, state.name))
        return state
    
    # used for admin view
    def all_next_states(self):
        return ",".join([state.name for state in self.next_states.all()])

    # used for admin view
    def all_responsible(self):
        return ",".join([group.name for group in self.responsible.all()])

    class Meta:
        ordering = ['sort_order']

class Document(models.Model):

    title = models.CharField(
        _("Title"),
        max_length=255, 
        help_text=_("The title of the DTB, including any subtitles"))
    author = models.CharField(
        _("Author"),
        max_length=255, 
        help_text=_("Names of primary author or creator of the intellectual content of the publication"),
        blank=True)
    subject = models.CharField(
        _("Subject"),
        max_length=255, 
        help_text=_("The topic of the content of the publication"),
        blank=True)
    description = models.TextField(
        _("Description"),
        help_text=_("Plain text describing the publication's content"),
        blank=True)
    publisher = models.CharField(
        _("Publisher"),
        max_length=255,
        default=settings.DAISY_DEFAULT_PUBLISHER,
        help_text=_("The agency responsible for making the DTB available"))
    date = models.DateField(
        _("Date"),
        help_text=_("Date of publication of the DTB"))
    identifier = models.CharField(
        _("Identifier"),
        max_length=255, 
        help_text=_("A string or number identifying the DTB"))
    source = models.CharField(
        _("Source"),
        max_length=20, 
        help_text=_("A reference to a resource (e.g., a print original, ebook, etc.) from which the DTB is derived. Best practice is to use the ISBN when available"), 
        blank=True)
    language_choices = (('de', 'de'),
                        ('de-1901', 'de-1901',),
#                        ('de-CH', 'de-CH',),
                        )
    language = models.CharField(
        _("Language"),
        max_length=10,
        choices=language_choices,
        help_text=_("Language of the content of the publication"))
    rights = models.CharField(
        _("Rights"),
        max_length=255, 
        help_text=_("Information about rights held in and over the DTB"),
        blank=True)
    
    source_date = models.DateField(
        _("Source Date"),
        help_text=_("Date of publication of the resource (e.g., a print original, ebook, etc.) from which the DTB is derived"),
        null=True, blank=True)
    source_edition = models.CharField(
        _("Source Edition"), 
        max_length=255, 
        help_text=_("A string describing the edition of the resource (e.g., a print original, ebook, etc.) from which the DTB is derived"),
        blank=True)
    source_publisher = models.CharField(
        _("Source Publisher"), 
        max_length=255, 
        help_text=_("The agency responsible for making available the resource (e.g., a print original, ebook, etc.) from which the DTB is derived"),
        blank=True)
    source_rights = models.CharField(
        _("Source Rights"), 
        max_length=255, 
        help_text=_("Information about rights held in and over the resource (e.g., a print original, ebook, etc.) from which the DTB is derived"),
        blank=True)

    state = models.ForeignKey(State, verbose_name=_("State"))
    assigned_to = models.ForeignKey(User, verbose_name=_("Assigned to"), null=True, blank=True)
    created_at = models.DateTimeField(_("Created"))
    modified_at = models.DateTimeField(_("Last Modified"))

    def __unicode__(self):
        return self.title

    def latest_version(self):
        return self.version_set.latest()

    def transitionTo(self, state):
        self.assigned_to = None
        self.state = self.state.transitionTo(state)
        self.save()

    def save(self):
        if not self.id:
            self.created_at = datetime.datetime.now()
            self.date = datetime.date.today() 
        self.modified_at = datetime.datetime.now()             
        # set initial state
        if not self.pk:
            self.state = State.objects.filter(name='new')[0]
        if not self.identifier:
            self.identifier = "ch-sbs-%s" % str(uuid.uuid4())
        super(Document, self).save()

    def delete(self, *args, **kwargs):
        old_id = self.id
        super(Document, self).delete(*args, **kwargs)
        # remove the folders for versions and attachments on the file system
        rmtree(join(settings.MEDIA_ROOT, str(old_id)))

def get_version_path(instance, filename):
        return '%s/versions/%s.xml' % (instance.document_id, instance.id)
    
class Version(models.Model):
    comment = models.CharField(max_length=255)
    document = models.ForeignKey(Document)
    content = models.FileField(upload_to=get_version_path)
{'fraction_non_alphanumeric': 0.11006965345257545, 'fraction_numerical': 0.007911256341903861, 'mean_word_length': 2.732349165596919, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '765543', 'n_tokens_mistral': 3391, 'n_tokens_neox': 3279, 'n_words': 937}