<commit_before>from django.db import models
from .fields import HashField
from .utils import hashit


class HashManager(models.Manager):
    _hash_field_name = None

    @property
    def hash_field_name(self):
        if self._hash_field_name is None:
        
            for field in self.model._meta.fields:
                if isinstance(field, HashField):
                    self._hash_field_name = field.name

        return self._hash_field_name

    def hash_keys(self, **kwargs):
        return list(self.filter(**kwargs).values_list(self.hash_field_name, flat=True))

    def update_or_create(self, keys, defaults=None, return_object=False, **kwargs):
        """
        Looks up an object with the given kwargs, updating one with defaults
        if it exists, otherwise creates a new one.
        Returns a tuple (object, created), where created is a boolean
        specifying whether an object was created.
        """
        defaults = defaults or {}
        hashed_params = []

        for field in self.model._meta.fields:
            if isinstance(field, HashField):
                populate_from = getattr(field, 'populate_from', [])
                for p in populate_from:
                    if p in kwargs:
                        hashed_params.append(str(kwargs.get(p, None)))

        hash_key = hashit(''.join(hashed_params))

        if hash_key in keys:
            obj = self.filter(**{self.hash_field_name: hash_key}).update(**defaults)
            created = False
        else:
            obj = self.model(**dict(kwargs.items() + defaults.items())).save()
            created = True
<commit_msg>last updated date<commit_after>from django.db import models
from .fields import HashField
from .utils import hashit


class HashManager(models.Manager):
    _hash_field_name = None

    @property
    def hash_field_name(self):
        if self._hash_field_name is None:

            for field in self.model._meta.fields:
                if isinstance(field, HashField):
                    self._hash_field_name = field.name

        return self._hash_field_name

    def hash_keys(self, **kwargs):
        return list(self.filter(**kwargs).values_list(self.hash_field_name, flat=True))

    def update_or_create(self, keys, defaults=None, return_object=False, **kwargs):
        """
        Looks up an object with the given kwargs, updating one with defaults
        if it exists, otherwise creates a new one.
        Returns a tuple (object, created), where created is a boolean
        specifying whether an object was created.
        """
        defaults = defaults or {}
        hashed_params = []

        for field in self.model._meta.fields:
            if isinstance(field, HashField):
                populate_from = getattr(field, 'populate_from', [])
                for p in populate_from:
                    if p in kwargs:
                        hashed_params.append(str(kwargs.get(p, None)))
            from django.db.models import DateTimeField

        hash_key = hashit(''.join(hashed_params))

        if hash_key in keys:
            for field in self.model._meta.fields:
                if isinstance(field, DateTimeField):
                    auto_now = getattr(field, 'auto_now', None)
                    if auto_now:
                        import datetime

                        defaults[field.name] = datetime.datetime.now()
            obj = self.filter(**{self.hash_field_name: hash_key}).update(**defaults)
            created = False
        else:
            obj = self.model(**dict(kwargs.items() + defaults.items())).save()
            created = True

{'fraction_non_alphanumeric': 0.09113504556752279, 'fraction_numerical': 0.0, 'mean_word_length': 2.0825531914893616, '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': '3786001', 'n_tokens_mistral': 980, 'n_tokens_neox': 938, 'n_words': 269}