<commit_before># coding: utf-8
from typing import Union

import six
from django.db.models import Max
from django.db.models.fields import IntegerField

__all__ = ['AutoSequenceField']


class AutoSequenceField(IntegerField):
    """
    AutoSequenceField is an AutoField that is available for non primary keys and can be configured
    with unique_with to have separate sequences based on other model fields.

    It is non editable and unique

    :param start_at: integer: the starting number of the sequence, defaults to 1
    :param unique_with: string or tuple of strings: name or names of attributes
        that this sequence will be unique with

    """

    def __init__(self, start_at: int = 1, unique_with: Union[str, tuple] = None, *args, **kwargs):
        # unique by default
        kwargs['editable'] = False

        # unique_with value can be string or tuple
        self.unique_with = unique_with or ()
        if isinstance(self.unique_with, six.string_types):
            self.unique_with = (self.unique_with,)

        self.start_at = start_at

        super(AutoSequenceField, self).__init__(*args, **kwargs)

    def deconstruct(self):
        name, path, args, kwargs = super(AutoSequenceField, self).deconstruct()
        del kwargs['editable']

        if self.start_at != 1:
            kwargs['start_at'] = self.start_at

        if self.unique_with != ():
            kwargs['unique_with'] = self.unique_with

        return name, path, args, kwargs

    def pre_save(self, instance, add):
        if not add:
            return getattr(instance, self.attname)

        qs = self.model.objects.all()

        if self.unique_with:
            qs = qs.filter(
                **{field: getattr(instance, field) for field in self.unique_with}
            )
        sequence = qs.aggregate(max=Max(self.attname))['max']<commit_msg>ensuring unique=True or unique_with are set<commit_after># coding: utf-8
from typing import Union

from django.db.models import Max
from django.db.models.fields import IntegerField

__all__ = ['AutoSequenceField']


class AutoSequenceField(IntegerField):
    """
    AutoSequenceField is an AutoField that is available for non primary keys and can be configured
    with unique_with to have separate sequences based on other model fields.

    It is non editable and unique

    :param start_at: integer: the starting number of the sequence, defaults to 1
    :param unique_with: string or tuple of strings: name or names of attributes
        that this sequence will be unique with

    """

    def __init__(self, start_at: int = 1, unique_with: Union[str, tuple] = None, *args, **kwargs):
        # unique by default
        kwargs['editable'] = False
        if not unique_with:
            kwargs.setdefault('unique', True)

        # unique_with value can be string or tuple
        self.unique_with = unique_with or ()
        if isinstance(self.unique_with, str):
            self.unique_with = (self.unique_with,)

        self.start_at = start_at

        super(AutoSequenceField, self).__init__(*args, **kwargs)

    def deconstruct(self):
        name, path, args, kwargs = super(AutoSequenceField, self).deconstruct()
        del kwargs['editable']

        if self.start_at != 1:
            kwargs['start_at'] = self.start_at

        if self.unique_with:
            kwargs['unique_with'] = self.unique_with
        else:
            kwargs.setdefault('unique', True)

        return name, path, args, kwargs

    def pre_save(self, instance, add):
        if not add:
            return getattr(instance, self.attname)

        qs = self.model.objects.all()

        if self.unique_with:
            qs = qs.filter(
                **{field: getattr(instance, field) for field in self.unique_with}
            )
        sequence = qs.aggregate(max=Max(self.attname))['max']
{'fraction_non_alphanumeric': 0.09100364014560583, 'fraction_numerical': 0.0020800832033281333, 'mean_word_length': 2.660323501427212, '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': '2720884', 'n_tokens_mistral': 1096, 'n_tokens_neox': 1055, 'n_words': 348}