<commit_before># -*- coding: utf-8 -*-
#
# Copyright 2015, Foxugly. All rights reserved.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.

from django.utils.translation import ugettext_lazy as _
from django.db import models
from documents.models import Document
import os
import uuid


class TypeCategory(models.Model):
    name = models.CharField(_("Type of documents"), max_length=128)
    priority = models.IntegerField(_('Priority'), unique=True)
    active = models.BooleanField(_('active'), default=True)

    def __str__(self):
        return self.name


class Category(models.Model):
    cat = models.ForeignKey(TypeCategory)
    documents = models.ManyToManyField(Document, verbose_name=_('documents'), blank=True)
    refer_trimester = models.ForeignKey('trimesters.Trimester', verbose_name=_('trimester'),
                                        related_name="back_trimester", null=True)
    active = models.BooleanField(_('active'), default=True)
    random = models.CharField(max_length=24, blank=True, null=True)

    def get_name(self):
        return u'%s' % self.cat.name

    def __str__(self):
        return u'%s - %s' % (self.refer_trimester, self.get_name())

    def add_doc(self, document):
        self.documents.add(document)

    def get_docs(self):
        return sorted(self.documents.all(), key=lambda x: x.date, reverse=True)

    def count_docs(self):
        return len(self.documents.all())

    def get_doc(self, i):
        return self.documents.get(id=i)

    def as_json(self):
        return dict(id=self.id, name=self.cat.name, n=str(self.count_docs()), )

    def get_relative_path(self):
        return os.path.join(self.refer_trimester.get_relative_path(), self.cat.name + "_" + self.random)

    def get_absolute_path(self):
        return os.path.join(self.refer_trimester.get_absolute_path(), self.cat.name + "_" + self.random)

    def save(self, *args, **kwargs):
        if not self.random:
            self.random = str(uuid.uuid4().get_hex().upper()[0:24])
        super(Category, self).save(*args, **kwargs)
        if not os.path.isdir(self.get_absolute_path()):
            os.mkdir(self.get_absolute_path(), 0711)

    def delete(self, **kwargs):
        for d in self.documents.all():
            d.delete()
        os.rmdir(self.get_absolute_path())
        super(Category, self).delete()<commit_msg>sort list documents and add_trimester manualy<commit_after># -*- coding: utf-8 -*-
#
# Copyright 2015, Foxugly. All rights reserved.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.

from django.utils.translation import ugettext_lazy as _
from django.db import models
from documents.models import Document
import os
import uuid


class TypeCategory(models.Model):
    name = models.CharField(_("Type of documents"), max_length=128)
    priority = models.IntegerField(_('Priority'), unique=True)
    active = models.BooleanField(_('active'), default=True)

    def __str__(self):
        return self.name


class Category(models.Model):
    cat = models.ForeignKey(TypeCategory)
    documents = models.ManyToManyField(Document, verbose_name=_('documents'), blank=True)
    refer_trimester = models.ForeignKey('trimesters.Trimester', verbose_name=_('trimester'),
                                        related_name="back_trimester", null=True)
    active = models.BooleanField(_('active'), default=True)
    random = models.CharField(max_length=24, blank=True, null=True)

    def get_name(self):
        return u'%s' % self.cat.name

    def __str__(self):
        return u'[%d] %s - %s' % (self.id, self.refer_trimester, self.get_name())

    def add_doc(self, document):
        self.documents.add(document)

    def get_docs(self):
        return sorted(self.documents.all(), key=lambda x: x.date, reverse=True)

    def count_docs(self):
        return len(self.documents.all())

    def get_doc(self, i):
        return self.documents.get(id=i)

    def as_json(self):
        return dict(id=self.id, name=self.cat.name, n=str(self.count_docs()), )

    def get_relative_path(self):
        return os.path.join(self.refer_trimester.get_relative_path(), self.cat.name + "_" + self.random)

    def get_absolute_path(self):
        return os.path.join(self.refer_trimester.get_absolute_path(), self.cat.name + "_" + self.random)

    def save(self, *args, **kwargs):
        if not self.random:
            self.random = str(uuid.uuid4().get_hex().upper()[0:16])
        super(Category, self).save(*args, **kwargs)
        if not os.path.isdir(self.get_absolute_path()):
            os.mkdir(self.get_absolute_path(), 0711)

    def delete(self, **kwargs):
        for d in self.documents.all():
            d.delete()
        os.rmdir(self.get_absolute_path())
        super(Category, self).delete()
{'fraction_non_alphanumeric': 0.12809119010819164, 'fraction_numerical': 0.007341576506955177, 'mean_word_length': 3.376162299239222, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '311125', 'n_tokens_mistral': 1631, 'n_tokens_neox': 1559, 'n_words': 374}