<commit_before>from django.db import models
from django.template.defaultfilters import slugify
from django.utils.translation import gettext_lazy as _
from django_better_admin_arrayfield.models.fields import ArrayField
from future.utils import python_2_unicode_compatible


@python_2_unicode_compatible
class SegmentType(models.Model):
    name = models.CharField(_('name'), max_length=255)
    slug = models.SlugField(_('slug'), max_length=100, unique=True)

    is_active = models.BooleanField(
        _('Is active'),
        default=True
    )
    user_editable = models.BooleanField(
        _('Editable in user profile'),
        default=True
    )
    enable_search = models.BooleanField(
        _('Enable search filters.'),
        default=False
    )

    def save(self, **kwargs):
        if not self.slug:
            self.slug = slugify(self.name)

        super(SegmentType, self).save(**kwargs)

    def __str__(self):
        return self.name

    class Meta:
        ordering = ('name',)


@python_2_unicode_compatible
class Segment(models.Model):
    name = models.CharField(_('name'), max_length=255)

    alternate_names = ArrayField(
        models.CharField(max_length=200),
        blank=True
    )
    type = models.ForeignKey(
        SegmentType,
        verbose_name=_('type'),
        related_name='segments',
        on_delete=models.CASCADE
    )

    def save(self, *args, **kwargs):
        if self.name not in self.alternate_names:
            self.alternate_names.append(self.name)

        super(Segment, self).save(*args, **kwargs)

    def __str__(self):
        return u'{}: {}'.format(self.type.name, self.name)

    class Meta:
        ordering = ('name',)<commit_msg>Fix saving segments when alternate names is empty.<commit_after>from django.db import models
from django.template.defaultfilters import slugify
from django.utils.translation import gettext_lazy as _
from django_better_admin_arrayfield.models.fields import ArrayField
from future.utils import python_2_unicode_compatible


@python_2_unicode_compatible
class SegmentType(models.Model):
    name = models.CharField(_('name'), max_length=255)
    slug = models.SlugField(_('slug'), max_length=100, unique=True)

    is_active = models.BooleanField(
        _('Is active'),
        default=True
    )
    user_editable = models.BooleanField(
        _('Editable in user profile'),
        default=True
    )
    enable_search = models.BooleanField(
        _('Enable search filters.'),
        default=False
    )

    def save(self, **kwargs):
        if not self.slug:
            self.slug = slugify(self.name)

        super(SegmentType, self).save(**kwargs)

    def __str__(self):
        return self.name

    class Meta:
        ordering = ('name',)


@python_2_unicode_compatible
class Segment(models.Model):
    name = models.CharField(_('name'), max_length=255)

    alternate_names = ArrayField(
        models.CharField(max_length=200),
        default=list
    )
    type = models.ForeignKey(
        SegmentType,
        verbose_name=_('type'),
        related_name='segments',
        on_delete=models.CASCADE
    )

    def save(self, *args, **kwargs):
        if self.name not in self.alternate_names:
            self.alternate_names.append(self.name)

        super(Segment, self).save(*args, **kwargs)

    def __str__(self):
        return u'{}: {}'.format(self.type.name, self.name)

    class Meta:
        ordering = ('name',)
{'fraction_non_alphanumeric': 0.11149724876918621, 'fraction_numerical': 0.008688097306689836, 'mean_word_length': 2.965556831228473, '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': '334803', 'n_tokens_mistral': 1068, 'n_tokens_neox': 1016, 'n_words': 218}