<commit_before># -*- coding: utf-8 -*-

from django.utils.translation import ugettext_lazy as _
from django.db import models
from django.conf import settings
from django.core.cache import cache

import inspect
import copy
import time

from app.utils import has_property, get_property, set_property


class ModelWithRight(object):
    """A basic class for a model with right. Mainly implement the can(RIGHT, user) function and helper functions"""

    class MetaRights(object):
        linked_user_property = 'user'
        linked_unit_property = 'unit'

        rights = {}

        @classmethod
        def rights_update(cls, new_rights):
            cls.rights = copy.deepcopy(cls.rights)
            cls.rights.update(new_rights)

    @classmethod
    def static_rights_can(cls, right, user, unit_to_link=None, year_to_link=None):

        dummy = cls()

        if unit_to_link and hasattr(dummy.MetaRights, 'linked_unit_property') and dummy.MetaRights.linked_unit_property:
            if hasattr(dummy, 'MetaData') and hasattr(dummy.MetaData, 'costcenterlinked') and dummy.MetaData.costcenterlinked and unit_to_link.costcenter_set.first():
                setattr(dummy, 'costcenter', unit_to_link.costcenter_set.first())
            set_property(dummy, dummy.MetaRights.linked_unit_property, unit_to_link)

        if unit_to_link and hasattr(dummy, 'generic_set_dummy_unit'):
            dummy.generic_set_dummy_unit(unit_to_link)

        from accounting_core.utils import AccountingYearLinked

        if year_to_link and isinstance(dummy, AccountingYearLinked):
            dummy.accounting_year = year_to_link

        return dummy.rights_can(right, user)

    def rights_expire(self):
        """Mark cache as invalid"""
        cache_key_last = 'right~last_%s.%s_%s' % (inspect.getmodule(self).__name__, self.__class__.__name__, self.pk or 'DUMMY')
        cached_last = time.time()
        cache.set(cache_key_last, cached_last)
<commit_msg>Hotfix: Costcenter for unit without one<commit_after># -*- coding: utf-8 -*-

from django.utils.translation import ugettext_lazy as _
from django.db import models
from django.conf import settings
from django.core.cache import cache

import inspect
import copy
import time

from app.utils import has_property, get_property, set_property


class ModelWithRight(object):
    """A basic class for a model with right. Mainly implement the can(RIGHT, user) function and helper functions"""

    class MetaRights(object):
        linked_user_property = 'user'
        linked_unit_property = 'unit'

        rights = {}

        @classmethod
        def rights_update(cls, new_rights):
            cls.rights = copy.deepcopy(cls.rights)
            cls.rights.update(new_rights)

    @classmethod
    def static_rights_can(cls, right, user, unit_to_link=None, year_to_link=None):

        dummy = cls()

        if unit_to_link and hasattr(dummy.MetaRights, 'linked_unit_property') and dummy.MetaRights.linked_unit_property:
            if hasattr(dummy, 'MetaData') and hasattr(dummy.MetaData, 'costcenterlinked') and dummy.MetaData.costcenterlinked:
                if unit_to_link.costcenter_set.first():
                    setattr(dummy, 'costcenter', unit_to_link.costcenter_set.first())
                else:  # No costcenter, set a dummy one
                    from accounting_core.models import CostCenter
                    setattr(dummy, 'costcenter', CostCenter())
            set_property(dummy, dummy.MetaRights.linked_unit_property, unit_to_link)

        if unit_to_link and hasattr(dummy, 'generic_set_dummy_unit'):
            dummy.generic_set_dummy_unit(unit_to_link)

        from accounting_core.utils import AccountingYearLinked

        if year_to_link and isinstance(dummy, AccountingYearLinked):
            dummy.accounting_year = year_to_link

        return dummy.rights_can(right, user)

    def rights_expire(self):
        """Mark cache as invalid"""
        cache_key_last = 'right~last_%s.%s_%s' % (inspect.getmodule(self).__name__, self.__class__.__name__, self.pk or 'DUMMY')
        cached_last = time.time()
        cache.set(cache_key_last, cached_last)

{'fraction_non_alphanumeric': 0.08834178131788559, 'fraction_numerical': 0.0004827419744146754, 'mean_word_length': 3.294300518134715, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '627162', 'n_tokens_mistral': 1239, 'n_tokens_neox': 1200, 'n_words': 287}