<commit_before>import re

from django.core.exceptions import FieldError
from django.db.models import SlugField
from django.utils.crypto import get_random_string


class RandomSlugField(SlugField):

    """ RandomSlugField

    Generates a random ascii based slug eg. www.example.com/kEwD58P

    By default sets editable=False, blank=True, and unique=True.

    Requires arguments:

        length
            Specifies the length of the generated slug. (integer)

    Optional arguments:

        exclude_upper
            Boolean to exclude uppercase characters. (default=False)

        exclude_lower
            Boolean to exclude lowercase characters. (default=False)

        exclude_digits
            Boolean to exclude digits. (default=False)

        exclude_vowels
            Boolean to exclude vowels. (default=False)

    Inspired by django-extensions AutoSlugField:
    http://pythonhosted.org/django-extensions/
    """

    def __init__(self, length=None, exclude_upper=False, exclude_lower=False,
                 exclude_digits=False, exclude_vowels=False, *args, **kwargs):
        kwargs.setdefault('blank', True)
        kwargs.setdefault('editable', False)
        kwargs.setdefault('unique', True)

        if length is None:
            raise ValueError("Missing 'length' argument.")
        elif exclude_lower and exclude_upper and exclude_digits:
            raise ValueError("Cannot exclude all valid characters.")
        else:
            self.length = length
            self.exclude_upper = exclude_upper
            self.exclude_lower = exclude_lower
            self.exclude_digits = exclude_digits
            self.exclude_vowels = exclude_vowels
        self.chars = self.generate_charset(exclude_upper=self.exclude_upper,
                                           exclude_lower=self.exclude_lower,
                                           exclude_digits=self.exclude_digits,
                                           exclude_vowels=self.exclude_vowels)
        kwargs.setdefault('max_length', self.length)
        if kwargs['max_length'] < self.length:
            raise ValueError("'max_length' must be equal to or greater than "
                             "'length'.")

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

    def generate_charset(self, exclude_upper, exclude_lower, exclude_digits,
                         exclude_vowels):
        chars = ('abcdefghijklmnopqrstuvwxyz'
                 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
        if exclude_upper:
            chars = chars.replace('ABCDEFGHIJKLMNOPQRSTUVWXYZ', '')
        if exclude_lower:
            chars = chars.replace('abcdefghijklmnopqrstuvwxyz', '')
        if exclude_digits:
            chars = chars.replace('0123456789', '')
        if exclude_vowels:
            chars = re.sub(r'[aeiouAEIOU]', '', chars)
        return chars

    def generate_slug(self, model_instance):
        queryset = model_instance.__class__._default_manager.all()

        if queryset.count() >= len(self.chars)**self.length:
            raise FieldError("No available slugs remaining.")

        slug = get_random_string(self.length, self.chars)

        # Exclude the current model instance from the queryset used in
        # finding next valid slug.
        if model_instance.pk:
            queryset = queryset.exclude(pk=model_instance.pk)

        # Form a kwarg dict used to impliment any unique_together
        # contraints.
        kwargs = {}
        for params in model_instance._meta.unique_together:
            if self.attname in params:
                for param in params:
                    kwargs[param] = getattr(model_instance, param, None)
        kwargs[self.attname] = slug
<commit_msg>moved charset generation to __init__<commit_after>import re

from django.core.exceptions import FieldError
from django.db.models import SlugField
from django.utils.crypto import get_random_string


class RandomSlugField(SlugField):

    """ RandomSlugField

    Generates a random ascii based slug eg. www.example.com/kEwD58P

    By default sets editable=False, blank=True, and unique=True.

    Required arguments:

        length
            Specifies the length of the generated slug. (integer)

    Optional arguments:

        exclude_upper
            Boolean to exclude uppercase characters. (default=False)

        exclude_lower
            Boolean to exclude lowercase characters. (default=False)

        exclude_digits
            Boolean to exclude digits. (default=False)

        exclude_vowels
            Boolean to exclude vowels. (default=False)

    Inspired by django-extensions AutoSlugField:
    http://pythonhosted.org/django-extensions/
    """

    def __init__(self, length=None, exclude_upper=False, exclude_lower=False,
                 exclude_digits=False, exclude_vowels=False, *args, **kwargs):
        kwargs.setdefault('blank', True)
        kwargs.setdefault('editable', False)
        kwargs.setdefault('unique', True)

        if length is None:
            raise ValueError("Missing 'length' argument.")
        elif exclude_lower and exclude_upper and exclude_digits:
            raise ValueError("Cannot exclude all valid characters.")
        else:
            self.length = length
            self.exclude_upper = exclude_upper
            self.exclude_lower = exclude_lower
            self.exclude_digits = exclude_digits
            self.exclude_vowels = exclude_vowels

        self.chars = ('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
                      '0123456789')
        if self.exclude_upper:
            self.chars = self.chars.replace('ABCDEFGHIJKLMNOPQRSTUVWXYZ', '')
        if self.exclude_lower:
            self.chars = self.chars.replace('abcdefghijklmnopqrstuvwxyz', '')
        if self.exclude_digits:
            self.chars = self.chars.replace('0123456789', '')
        if exclude_vowels:
            self.chars = re.sub(r'[aeiouAEIOU]', '', self.chars)

        kwargs.setdefault('max_length', self.length)
        if kwargs['max_length'] < self.length:
            raise ValueError("'max_length' must be equal to or greater than "
                             "'length'.")

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

    def generate_slug(self, model_instance):
        queryset = model_instance.__class__._default_manager.all()

        if queryset.count() >= len(self.chars)**self.length:
            raise FieldError("No available slugs remaining.")

        slug = get_random_string(self.length, self.chars)

        # Exclude the current model instance from the queryset used in
        # finding next valid slug.
        if model_instance.pk:
            queryset = queryset.exclude(pk=model_instance.pk)

        # Form a kwarg dict used to impliment any unique_together
        # contraints.
        kwargs = {}
        for params in model_instance._meta.unique_together:
            if self.attname in params:
                for param in params:
                    kwargs[param] = getattr(model_instance, param, None)
        kwargs[self.attname] = slug

{'fraction_non_alphanumeric': 0.08125616110406984, 'fraction_numerical': 0.006196310378819884, 'mean_word_length': 2.3389750822755055, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '208298', 'n_tokens_mistral': 2014, 'n_tokens_neox': 1885, 'n_words': 463}