<commit_before>from django.db import models
from django.db.utils import IntegrityError
from django.utils.translation import ugettext_lazy as _
from django.utils.encoding import python_2_unicode_compatible


@python_2_unicode_compatible
class Preference(models.Model):

    app = models.CharField(_('Application'), max_length=100, null=True, blank=True, db_index=True)
    name = models.CharField(_('Name'), max_length=150)
    text = models.TextField(_('Value'), null=True, blank=True)

    @staticmethod
    def __str__(self):
        return '%s:%s' % (self.app, self.name)

    @classmethod
    def read_prefs(cls, mem_prefs):
        """Initializes preferences entries in DB according to currently discovered prefs."""
        db_prefs = {'%s__%s' % (pref['app'], pref['name']): pref for pref in cls.objects.values().order_by('app', 'name')}

        new_prefs = []
        for app, prefs in mem_prefs.items():
            for pref_name, pref_proxy in prefs.items():
                if not pref_proxy.static:  # Do not add static options to DB.
                    key = '%s__%s' % (app, pref_name)
                    if key in db_prefs:
                        # Entry already exists in DB. Let's get pref value from there.
                        pref_proxy.db_value = db_prefs[key]['text']
                    else:
                        new_prefs.append(cls(app=app, name=pref_name, text=pref_proxy.default))

        if new_prefs:
            try:
                cls.objects.bulk_create(new_prefs)
            except IntegrityError:  # Don't bother with duplicates.
                pass

    @classmethod
    def update_prefs(cls, *args, **kwargs):
        # TODO That could be more efficient.<commit_msg>fixed staticmethod on Preference model __str__.<commit_after>from django.db import models
from django.db.utils import IntegrityError
from django.utils.translation import ugettext_lazy as _
from django.utils.encoding import python_2_unicode_compatible


@python_2_unicode_compatible
class Preference(models.Model):

    app = models.CharField(_('Application'), max_length=100, null=True, blank=True, db_index=True)
    name = models.CharField(_('Name'), max_length=150)
    text = models.TextField(_('Value'), null=True, blank=True)

    def __str__(self):
        return '%s:%s' % (self.app, self.name)

    @classmethod
    def read_prefs(cls, mem_prefs):
        """Initializes preferences entries in DB according to currently discovered prefs."""
        db_prefs = {'%s__%s' % (pref['app'], pref['name']): pref for pref in cls.objects.values().order_by('app', 'name')}

        new_prefs = []
        for app, prefs in mem_prefs.items():
            for pref_name, pref_proxy in prefs.items():
                if not pref_proxy.static:  # Do not add static options to DB.
                    key = '%s__%s' % (app, pref_name)
                    if key in db_prefs:
                        # Entry already exists in DB. Let's get pref value from there.
                        pref_proxy.db_value = db_prefs[key]['text']
                    else:
                        new_prefs.append(cls(app=app, name=pref_name, text=pref_proxy.default))

        if new_prefs:
            try:
                cls.objects.bulk_create(new_prefs)
            except IntegrityError:  # Don't bother with duplicates.
                pass

    @classmethod
    def update_prefs(cls, *args, **kwargs):
        # TODO That could be more efficient.
{'fraction_non_alphanumeric': 0.10666666666666667, 'fraction_numerical': 0.00463768115942029, 'mean_word_length': 2.4788306451612905, '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': '4511305', 'n_tokens_mistral': 1034, 'n_tokens_neox': 999, 'n_words': 267}