<commit_before>import requests
import logging

from itertools import chain
from django.db import models
from django.utils.text import slugify

from person.models import Person

from parliament.models import PoliticalParty
from parliament.models import ParliamentMember

from government.models import GovernmentMember

logger = logging.getLogger(__name__)


class Category(models.Model):
    name = models.CharField(max_length=200)
    slug = models.SlugField(max_length=250, default='')

    class Meta:
        abstract = True

    def save(self, *args, **kwargs):
        self.slug = slugify(self.name)
        super().save(*args, **kwargs)


class CategoryDossier(Category):
    def __str__(self):
        return str(self.name)

    class Meta:
        ordering = ['name']
        verbose_name = 'Category (dossier)'
        verbose_name_plural = 'Categories (dossier)'


class CategoryDocument(Category):
    def __str__(self):
        return 'Document category: ' + str(self.name)

    class Meta:
        ordering = ['name']
        verbose_name = 'Category (document)'
        verbose_name_plural = 'Categories (document)'


class Dossier(models.Model):
    dossier_id = models.CharField(max_length=100, blank=True, unique=True)
    categories = models.ManyToManyField(CategoryDossier, blank=True)
    is_active = models.BooleanField(default=True)
    url = models.URLField(blank=True)
    decision = models.CharField(max_length=200, blank=True)

    class Meta:
        ordering = ['-dossier_id']

    def __str__(self):
        return str(self.dossier_id)

    def documents(self):
        return Document.objects.filter(dossier=self)

    def kamerstukken(self):
        return Kamerstuk.objects.filter(document__dossier=self)

    def besluitenlijst_cases(self):
        return BesluitItemCase.objects.filter(related_document_ids__contains=self.dossier_id)

    def start_date(self):
        documents = Document.objects.filter(dossier=self).order_by('date_published')
        if documents.exists():
            return documents[0].date_published
        return None

    def voting(self):
        votings = Voting.objects.filter(dossier=self, is_dossier_voting=True).exclude(result=Voting.AANGEHOUDEN)
        if votings.exists():
            if votings.count() > 1:
                logger.error('more than one dossier voting found for dossier ' + str(self.dossier_id))
            return votings[0]
        return None

    def title(self):
        # TODO: improve performance
        kamerstukken = self.kamerstukken()
        titles = {}
        for stuk in kamerstukken:
            title = stuk.document.title()
            if title in titles:
                titles[title] += 1
            else:
                titles[title] = 1
        max_titles = 0
        title = 'undefined'
        for key, value in titles.items():
            if value > max_titles:
                title = key
                max_titles = value
        return title

    def is_withdrawn(self):
        latest_kamerstuk = self.kamerstukken().order_by('-document__date_published')[0]
        return 'intrekking' in latest_kamerstuk.type_long
<commit_msg>fix error if no kamerstukken exist<commit_after>import requests
import logging

from itertools import chain
from django.db import models
from django.utils.text import slugify

from person.models import Person

from parliament.models import PoliticalParty
from parliament.models import ParliamentMember

from government.models import GovernmentMember

logger = logging.getLogger(__name__)


class Category(models.Model):
    name = models.CharField(max_length=200)
    slug = models.SlugField(max_length=250, default='')

    class Meta:
        abstract = True

    def save(self, *args, **kwargs):
        self.slug = slugify(self.name)
        super().save(*args, **kwargs)


class CategoryDossier(Category):
    def __str__(self):
        return str(self.name)

    class Meta:
        ordering = ['name']
        verbose_name = 'Category (dossier)'
        verbose_name_plural = 'Categories (dossier)'


class CategoryDocument(Category):
    def __str__(self):
        return 'Document category: ' + str(self.name)

    class Meta:
        ordering = ['name']
        verbose_name = 'Category (document)'
        verbose_name_plural = 'Categories (document)'


class Dossier(models.Model):
    dossier_id = models.CharField(max_length=100, blank=True, unique=True)
    categories = models.ManyToManyField(CategoryDossier, blank=True)
    is_active = models.BooleanField(default=True)
    url = models.URLField(blank=True)
    decision = models.CharField(max_length=200, blank=True)

    class Meta:
        ordering = ['-dossier_id']

    def __str__(self):
        return str(self.dossier_id)

    def documents(self):
        return Document.objects.filter(dossier=self)

    def kamerstukken(self):
        return Kamerstuk.objects.filter(document__dossier=self)

    def besluitenlijst_cases(self):
        return BesluitItemCase.objects.filter(related_document_ids__contains=self.dossier_id)

    def start_date(self):
        documents = Document.objects.filter(dossier=self).order_by('date_published')
        if documents.exists():
            return documents[0].date_published
        return None

    def voting(self):
        votings = Voting.objects.filter(dossier=self, is_dossier_voting=True).exclude(result=Voting.AANGEHOUDEN)
        if votings.exists():
            if votings.count() > 1:
                logger.error('more than one dossier voting found for dossier ' + str(self.dossier_id))
            return votings[0]
        return None

    def title(self):
        # TODO: improve performance
        kamerstukken = self.kamerstukken()
        titles = {}
        for stuk in kamerstukken:
            title = stuk.document.title()
            if title in titles:
                titles[title] += 1
            else:
                titles[title] = 1
        max_titles = 0
        title = 'undefined'
        for key, value in titles.items():
            if value > max_titles:
                title = key
                max_titles = value
        return title

    def is_withdrawn(self):
        kamerstukken = self.kamerstukken().order_by('-document__date_published')
        if kamerstukken:
            return 'intrekking' in kamerstukken[0].type_long  # latest kamerstuk
        return False

{'fraction_non_alphanumeric': 0.09408812921436412, 'fraction_numerical': 0.005958914850243061, 'mean_word_length': 2.77396449704142, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '4467602', 'n_tokens_mistral': 1941, 'n_tokens_neox': 1826, 'n_words': 421}