<commit_before>from django.contrib import admin
from django.utils.translation import ugettext
from django import newforms as forms
from django.contrib.contenttypes import generic

from ella.ellaadmin import widgets

class ListingInlineFormset(generic.GenericInlineFormset):
    def clean (self):
        if not self.cleaned_data or not self.instance:
            return self.cleaned_data

        obj = self.instance
        cat = obj.category

        main = None
        for d in self.cleaned_data:
            if d['category'] == cat:
                main = d
            elif d['hidden']:
                raise forms.ValidationError, ugettext('Only main listing can be hidden.')
        if main is None:
            raise forms.ValidationError, ugettext('If an object has a listing, it must have a listing in its main category.')

        if main['publish_from'] != min([ d['publish_from'] for d in self.cleaned_data]):
            raise forms.ValidationError, ugettext('No listing can start sooner than main listing')

        return self.cleaned_data

class ListingInlineOptions(generic.GenericTabularInline):
    @property
    def model(self):
        from ella.core.models import Listing
        return Listing
    extra = 2
    ct_field_name = 'target_ct'
    id_field_name = 'target_id'
    formset = ListingInlineFormset
    def formfield_for_dbfield(self, db_field, **kwargs):
        if db_field.name == 'category':
            kwargs['widget'] = widgets.ListingCategoryWidget
        return super(self.__class__, self).formfield_for_dbfield(db_field, **kwargs)


class ListingOptions(admin.ModelAdmin):<commit_msg>Added slug validation for uniqueness<commit_after>from django.contrib import admin
from django.utils.translation import ugettext
from django import newforms as forms
from django.contrib.contenttypes import generic

from ella.ellaadmin import widgets

class ListingInlineFormset(generic.GenericInlineFormset):
    def clean (self):
        if not self.cleaned_data or not self.instance:
            return self.cleaned_data

        obj = self.instance
        cat = obj.category

        main = None
        err = None
        for i, d in enumerate(self.cleaned_data):
            if d['category'] == cat:
                main = d
                qset = obj.__class__._default_manager.filter(slug=obj.slug, category=obj.category_id)
                if obj._get_pk_val():
                    qset = qset.exclude(pk=obj._get_pk_val())

                for o in qset:
                    if o.main_listing and o.main_listing.publish_from.date() == d['publish_from'].date():
                        raise forms.ValidationError(ugettext('There is already an object published in category %(category)s with slug %(slug)s on %(date)s') % {
                                    'slug' : obj.slug,
                                    'category' : obj.category,
                                    'date' : d['publish_from'].date(),
})

            elif d['hidden']:
                raise forms.ValidationError, ugettext('Only main listing can be hidden.')
        if main is None:
            raise forms.ValidationError, ugettext('If an object has a listing, it must have a listing in its main category.')

        if main['publish_from'] != min([ d['publish_from'] for d in self.cleaned_data]):
            raise forms.ValidationError, ugettext('No listing can start sooner than main listing')

        return self.cleaned_data

class ListingInlineOptions(generic.GenericTabularInline):
    @property
    def model(self):
        from ella.core.models import Listing
        return Listing
    extra = 2
    ct_field_name = 'target_ct'
    id_field_name = 'target_id'
    formset = ListingInlineFormset
    def formfield_for_dbfield(self, db_field, **kwargs):
        if db_field.name == 'category':
            kwargs['widget'] = widgets.ListingCategoryWidget
        return super(self.__class__, self).formfield_for_dbfield(db_field, **kwargs)


class ListingOptions(admin.ModelAdmin):
{'fraction_non_alphanumeric': 0.08587119321018473, 'fraction_numerical': 0.0004992511233150275, 'mean_word_length': 2.5397526501766783, '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': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5539953', 'n_tokens_mistral': 1109, 'n_tokens_neox': 1085, 'n_words': 313}