<commit_before>import uuid
from os import urandom
import hashlib
from jsonfield import JSONField

from django.db import models
from base.models import ObjectBaseClass
from django.utils.translation import ugettext_lazy as _
from django.conf import settings

from persons.models import Person
from competences.models import Competence
from modules.models import Module
from disciplines.models import Discipline, Semester



class LearningPlan(ObjectBaseClass):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    uni_displayableTitle = models.CharField(_("Версия"), max_length=32, blank=True, null=True)
    uni_number = models.CharField(_("Номер УП"), max_length=32, blank=True, null=True)
    uni_active = models.CharField(_("Текущая версия"), max_length=32, blank=True, null=True)
    uni_title = models.CharField(_("Название"), max_length=32, blank=True, null=True)
    uni_stage = models.BooleanField(_("План утверждён"), default=True)
    uni_loadTimeType = models.CharField(_("Единица измерения нагрузки"), max_length=32, blank=True, null=True)
    uni_html = models.TextField(blank=True, null=True)

    def __str__(self):
        return self.uni_number

    class Meta:
        verbose_name = 'учебный план'
        verbose_name_plural = 'учебные планы'


class Program(ObjectBaseClass):

    LEVELS = (
        ("b", _("бакалавриат")),
        ("m", _("магистратура")),
        ("s", _("специалитет")),
    )

    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    level = models.CharField(_("Уровень программы"), max_length=1, choices=LEVELS, default="b")
    title = models.CharField(_('Наименование образовательной программы'), blank=False, max_length=256, default=_(''))
    training_direction = models.CharField(_("Направление подготовки"), blank=False, max_length=256, default=_(''))
    competences = models.ManyToManyField(Competence, blank=True)
    chief = models.ForeignKey(Person, verbose_name=_('Руководитель образовательной программы'), blank=True, null=True)
    learning_plans = models.ManyToManyField("LearningPlan", blank=True)

    class Meta:
        verbose_name = 'программа'
        verbose_name_plural = 'программы'

    def __str__(self):
        return self.title

    def get_choice_groups(self):
        return [choice_group.id for choice_group in ChoiceGroup.objects.filter(program__id=self.id)]

    def get_modules(self):
        return

    def get_all_disciplines(self):
        pms = ProgramModules.objects.filter(program=self, status="p", archived=False).values_list("module__id", flat=True)
        return Discipline.objects.filter(module__id__in=pms, status="p", archived=False)

    def get_competences_diagram(self):
        response = {}
        for target in TrainingTarget.objects.filter(program=self, status="p", archived=False).order_by("number"):
            response[target.title] = []
            for competence in ProgramCompetence.objects.filter(program=self):
                labors = [pm.module.get_labor() for pm in
                          ProgramModules.objects.filter(program=self, competence=competence, status="p", archived=False,
                                                        module__id__in=target.get_mandatory_modules_id())]
                response[target.title].append(
                    ([competence.title, competence.color, sum([0 if not labor else labor for labor in labors])]))

        return response



class TrainingTarget(ObjectBaseClass):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    title = models.CharField(_('Наименование образовательной цели'), blank=False, max_length=256, default=_(''))
    program = models.ForeignKey("Program", blank=True, null=True)
    number = models.IntegerField(_("Порядковый номер цели"))

    class Meta:
        verbose_name = 'образовательная цель'
        verbose_name_plural = 'образовательные цели'

    def __str__(self):
        return self.title

    def get_modules_id(self):
        return TargetModules.objects.filter(target=self, archived=False, status="p").values_list(
            'program_module__module__id', flat=True)

    def get_mandatory_modules_id(self):
        return TargetModules.objects.filter(target=self, archived=False, status="p", choice_group=False).values_list(
            'program_module__module__id', flat=True)

    def get_choice_modules_id(self):
        return TargetModules.objects.filter(target=self, archived=False, status="p", choice_group=True).values_list(
            'program_module__module__id', flat=True)

    
class ProgramCompetence(ObjectBaseClass):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    title = models.CharField(_('Наименование компетенции'), blank=False, max_length=2048, default=_(''))
    program = models.ForeignKey("Program", blank=True, null=True)
    number = models.IntegerField(_("Номер компетенции"))
    color = models.CharField(_("Цвет"), max_length=32, blank=True, null=True, default="green")

    class Meta:
        verbose_name = 'компетенция программы'
        verbose_name_plural = 'компетенции программы'

    def __str__(self):
        return self.title

    def get_labor(self):
        response = [pm.module.get_labor() for pm in
                    ProgramModules.objects.filter(program=self.program, competence=self, status="p", archived=False)]
        return sum([0 if not labor else labor for labor in response])


class ProgramModules(ObjectBaseClass):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    program = models.ForeignKey("Program")
    module = models.ForeignKey("modules.Module")
    choice_group = models.ForeignKey("ChoiceGroup", blank=True, null=True)
    competence = models.ForeignKey(ProgramCompetence, blank=True, null=True)
    semester = models.PositiveIntegerField(blank=True, null=True)

    def get_all_disciplines(self):
        return Discipline.objects.filter(module=self.module)

    def get_all_discipline_ids(self):
        return [discipline.id for discipline in Discipline.objects.filter(module=self.module)]

    def get_all_discipline_custom(self):
        q = [[s for s in Semester.objects.filter(discipline=discipline, program=self.program)] for discipline in Discipline.objects.filter(module=self.module, archived=False, status="p").order_by("period")]
        for_delete = []
        for sem in q:
            title = None
            trsemester = 0
            for s in sem:
                if s.term.title == title and s.training_semester == trsemester:
                    for_delete.append(s)
                title = s.term.title
                trsemester = s.training_semester

        [s.delete() for s in for_delete]

        return [{"id": discipline.id,
                 "title": discipline.title,
                 "description": discipline.description,
                 "labor": discipline.labor,
                 "form": discipline.get_form_display(),
                 "semester": discipline.period,
                 "default_semester": [[str(self.program), s.id, s.term.title, s.training_semester] for s in Semester.objects.filter(discipline=discipline, program=self.program)]
                 } for discipline in Discipline.objects.filter(module=self.module, archived=False, status="p").order_by("period")]<commit_msg>default semester#19<commit_after>import uuid
from os import urandom
import hashlib
from jsonfield import JSONField

from django.db import models
from base.models import ObjectBaseClass
from django.utils.translation import ugettext_lazy as _
from django.conf import settings

from persons.models import Person
from competences.models import Competence
from modules.models import Module
from disciplines.models import Discipline, Semester



class LearningPlan(ObjectBaseClass):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    uni_displayableTitle = models.CharField(_("Версия"), max_length=32, blank=True, null=True)
    uni_number = models.CharField(_("Номер УП"), max_length=32, blank=True, null=True)
    uni_active = models.CharField(_("Текущая версия"), max_length=32, blank=True, null=True)
    uni_title = models.CharField(_("Название"), max_length=32, blank=True, null=True)
    uni_stage = models.BooleanField(_("План утверждён"), default=True)
    uni_loadTimeType = models.CharField(_("Единица измерения нагрузки"), max_length=32, blank=True, null=True)
    uni_html = models.TextField(blank=True, null=True)

    def __str__(self):
        return self.uni_number

    class Meta:
        verbose_name = 'учебный план'
        verbose_name_plural = 'учебные планы'


class Program(ObjectBaseClass):

    LEVELS = (
        ("b", _("бакалавриат")),
        ("m", _("магистратура")),
        ("s", _("специалитет")),
    )

    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    level = models.CharField(_("Уровень программы"), max_length=1, choices=LEVELS, default="b")
    title = models.CharField(_('Наименование образовательной программы'), blank=False, max_length=256, default=_(''))
    training_direction = models.CharField(_("Направление подготовки"), blank=False, max_length=256, default=_(''))
    competences = models.ManyToManyField(Competence, blank=True)
    chief = models.ForeignKey(Person, verbose_name=_('Руководитель образовательной программы'), blank=True, null=True)
    learning_plans = models.ManyToManyField("LearningPlan", blank=True)

    class Meta:
        verbose_name = 'программа'
        verbose_name_plural = 'программы'

    def __str__(self):
        return self.title

    def get_choice_groups(self):
        return [choice_group.id for choice_group in ChoiceGroup.objects.filter(program__id=self.id)]

    def get_modules(self):
        return

    def get_all_disciplines(self):
        pms = ProgramModules.objects.filter(program=self, status="p", archived=False).values_list("module__id", flat=True)
        return Discipline.objects.filter(module__id__in=pms, status="p", archived=False)

    def get_competences_diagram(self):
        response = {}
        for target in TrainingTarget.objects.filter(program=self, status="p", archived=False).order_by("number"):
            response[target.title] = []
            for competence in ProgramCompetence.objects.filter(program=self):
                labors = [pm.module.get_labor() for pm in
                          ProgramModules.objects.filter(program=self, competence=competence, status="p", archived=False,
                                                        module__id__in=target.get_mandatory_modules_id())]
                response[target.title].append(
                    ([competence.title, competence.color, sum([0 if not labor else labor for labor in labors])]))

        return response



class TrainingTarget(ObjectBaseClass):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    title = models.CharField(_('Наименование образовательной цели'), blank=False, max_length=256, default=_(''))
    program = models.ForeignKey("Program", blank=True, null=True)
    number = models.IntegerField(_("Порядковый номер цели"))

    class Meta:
        verbose_name = 'образовательная цель'
        verbose_name_plural = 'образовательные цели'

    def __str__(self):
        return self.title

    def get_modules_id(self):
        return TargetModules.objects.filter(target=self, archived=False, status="p").values_list(
            'program_module__module__id', flat=True)

    def get_mandatory_modules_id(self):
        return TargetModules.objects.filter(target=self, archived=False, status="p", choice_group=False).values_list(
            'program_module__module__id', flat=True)

    def get_choice_modules_id(self):
        return TargetModules.objects.filter(target=self, archived=False, status="p", choice_group=True).values_list(
            'program_module__module__id', flat=True)

    
class ProgramCompetence(ObjectBaseClass):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    title = models.CharField(_('Наименование компетенции'), blank=False, max_length=2048, default=_(''))
    program = models.ForeignKey("Program", blank=True, null=True)
    number = models.IntegerField(_("Номер компетенции"))
    color = models.CharField(_("Цвет"), max_length=32, blank=True, null=True, default="green")

    class Meta:
        verbose_name = 'компетенция программы'
        verbose_name_plural = 'компетенции программы'

    def __str__(self):
        return self.title

    def get_labor(self):
        response = [pm.module.get_labor() for pm in
                    ProgramModules.objects.filter(program=self.program, competence=self, status="p", archived=False)]
        return sum([0 if not labor else labor for labor in response])


class ProgramModules(ObjectBaseClass):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    program = models.ForeignKey("Program")
    module = models.ForeignKey("modules.Module")
    choice_group = models.ForeignKey("ChoiceGroup", blank=True, null=True)
    competence = models.ForeignKey(ProgramCompetence, blank=True, null=True)
    semester = models.PositiveIntegerField(blank=True, null=True)

    def get_all_disciplines(self):
        return Discipline.objects.filter(module=self.module)

    def get_all_discipline_ids(self):
        return [discipline.id for discipline in Discipline.objects.filter(module=self.module)]

    def get_all_discipline_custom(self):
        q = [[s for s in Semester.objects.filter(discipline=discipline, program=self.program)] for discipline in Discipline.objects.filter(module=self.module, archived=False, status="p").order_by("period")]
        for_delete = []
        for sem in q:
            title = None
            trsemester = 0
            for s in sem:
                if s.term.title == title and s.training_semester == trsemester:
                    for_delete.append(s)
                title = s.term.title
                trsemester = s.training_semester

        print(len(for_delete), ["; ".join([s.term.title, s.training_semester]) for s in for_delete])

        return [{"id": discipline.id,
                 "title": discipline.title,
                 "description": discipline.description,
                 "labor": discipline.labor,
                 "form": discipline.get_form_display(),
                 "semester": discipline.period,
                 "default_semester": [[str(self.program), s.id, s.term.title, s.training_semester] for s in Semester.objects.filter(discipline=discipline, program=self.program)]
                 } for discipline in Discipline.objects.filter(module=self.module, archived=False, status="p").order_by("period")]
{'fraction_non_alphanumeric': 0.11346414073071719, 'fraction_numerical': 0.004736129905277402, 'mean_word_length': 3.665719696969697, 'pattern_counts': {'":': 14, '<': 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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1542984', 'n_tokens_mistral': 4528, 'n_tokens_neox': 4500, 'n_words': 882}