<commit_before>from django import forms
from django.contrib import admin
from django.utils.translation import ugettext as _
from django.forms.models import BaseInlineFormSet

from imagekit.admin import AdminThumbnail

from real_estate.admin import MultiuploadInlinesContainerMixin

from .forms import (
    standart_formfield_overrides,
    NewBuildingForm,
    ResidentalComplexForm
)
from .models import (
    ResidentalComplex,
    NewBuilding,
    Builder,
    NewApartment,
    ResidentalComplexImage,
    TypeOfComplex,
    ResidentalComplexCharacteristic,
    ResidentalComplexFeature,
)

admin.site.register(Builder)
admin.site.register(TypeOfComplex)
admin.site.register(ResidentalComplexCharacteristic)


class NewApartmentInline(admin.StackedInline):
    model = NewApartment
    extra = 0
    exclude = ['description']
    formfield_overrides = standart_formfield_overrides

    fields = (
        'thumbnail',
        'layout',
        'total_area',
        'interior_decoration',
        'price',
        'celling_height',
        'is_active',
        'rooms',
        'floor',
        'section',
        'kitchen_area',
        'balcony_area',
        'buildings',
    )

    readonly_fields = ['thumbnail']
    thumbnail = AdminThumbnail(
        image_field='layout_small',
        template='admin/display_link_thumbnail.html',
    )
    thumbnail.short_description = _('миниатюра')

    # if it will be registred then show link
    show_change_link = True

    def formfield_for_manytomany(self, db_field, request, **kwargs):
        if db_field.name == "buildings":
            try:
                parent_obj_id = request.resolver_match.args[0]
                kwargs["queryset"] = NewBuilding.objects.filter(
                    residental_complex=parent_obj_id)
            except IndexError:
                pass
        return super(NewApartmentInline, self).formfield_for_foreignkey(
            db_field, request, **kwargs)


class BuildingInlineFormSet(BaseInlineFormSet):
    def clean(self):
        '''Checks if there are no two Forms
        with the same values at unique_together'''
        if any(self.errors):
            return

        unique_together_values = []
        unique_together = NewBuilding._meta.unique_together

        def checkIfUnique(_form, unique_names):
            output = []
            for val_name in unique_names:
                if type(val_name) in (tuple, list):
                    return checkIfUnique(_form, val_name)
                value = form.cleaned_data[val_name]
                output.append(value)

            if output in unique_together_values:
                if 'name' in unique_names:
                    form.add_error(
                        None, _('Поля "Имя дома" \
                            в данном жк должны быть уникальны')
                    )
                else:
                    form.add_error(None, _(
                        'Совокупность полей "Улица", "Номер дома" \
                        и "Корпус" должна быть уникальна для всех домов'))
            else:
                unique_together_values.append(output)

        for form in self.forms:
            if form.cleaned_data:
                checkIfUnique(form, unique_together)


class BuildingInline(admin.StackedInline):
    form = NewBuildingForm
    formset = BuildingInlineFormSet
    model = NewBuilding
    extra = 0
    formfield_overrides = standart_formfield_overrides
    fields = ('name',
              'street',
              'building',
              'building_block',
              'coordinates',
              'is_active',
              'building_type',
              'number_of_storeys',
              'date_of_start_of_construction',
              'date_of_construction',
              'building_permit',
              'project_declarations'
              )


class ResidentalComplexImageForm(forms.ModelForm):
    class Meta:
        model = ResidentalComplexImage
        widgets = {
            'image': forms.widgets.ClearableFileInput(attrs={'multiple': True})
        }
        fields = '__all__'


class ResidentalComplexImageInline(admin.TabularInline):
    model = ResidentalComplexImage
    form = ResidentalComplexImageForm
    extra = 0
    min_num = 0


class ResidentalComplexFeatureInline(admin.TabularInline):
    model = ResidentalComplexFeature
    extra = 0
    min_num = 0
    formfield_overrides = standart_formfield_overrides


@admin.register(ResidentalComplex)
class ResidentalComplexAdmin(
    MultiuploadInlinesContainerMixin,
    admin.ModelAdmin
):
    related_inline_form = ResidentalComplexImageForm
    related_inline_fk = 'residental_complex'
    form = ResidentalComplexForm
    inlines = [ResidentalComplexFeatureInline,
               BuildingInline,
               NewApartmentInline,
               ResidentalComplexImageInline,
               ]
    list_display = ('name', 'is_active')
    list_editable = ('is_active',)
    list_filter = ['is_active']
    search_fields = ['name']
    filter_horizontal = ['characteristics']
    fieldsets = (
        (None, {
            'fields': ('type_of_complex',
                       'name',
                       'neighbourhood',
                       'is_active',
                       'is_popular',
                       'builder',
                       'description',
                       'characteristics',
                       'number_of_buildings',
                       'number_of_flats')
        }),
        (_('МЕДИА'), {
            'fields': ('front_image', 'video_link', 'presentation'),
        }),
    )

    def get_inline_instances(self, request, obj=None):
        """Override to exclude NewApartmentInline
        from newly added forms"""
        from copy import deepcopy

        original_inlines = deepcopy(self.inlines)
        if not obj:
            self.inlines.pop(self.inlines.index(NewApartmentInline))

        answer = super().get_inline_instances(request, obj=None)
        self.inlines = original_inlines
        return answer
<commit_msg>Make collapse all inlines and fieldsets for RC in new_buildings.admin<commit_after>from django import forms
from django.contrib import admin
from django.utils.translation import ugettext as _
from django.forms.models import BaseInlineFormSet

from imagekit.admin import AdminThumbnail

from real_estate.admin import MultiuploadInlinesContainerMixin

from .forms import (
    standart_formfield_overrides,
    NewBuildingForm,
    ResidentalComplexForm
)
from .models import (
    ResidentalComplex,
    NewBuilding,
    Builder,
    NewApartment,
    ResidentalComplexImage,
    TypeOfComplex,
    ResidentalComplexCharacteristic,
    ResidentalComplexFeature,
)

admin.site.register(Builder)
admin.site.register(TypeOfComplex)
admin.site.register(ResidentalComplexCharacteristic)


class NewApartmentInline(admin.StackedInline):
    model = NewApartment
    extra = 0
    exclude = ['description']
    formfield_overrides = standart_formfield_overrides
    classes = ['collapse', ]

    fields = (
        'thumbnail',
        'layout',
        'total_area',
        'interior_decoration',
        'price',
        'celling_height',
        'is_active',
        'rooms',
        'floor',
        'section',
        'kitchen_area',
        'balcony_area',
        'buildings',
    )

    readonly_fields = ['thumbnail']
    thumbnail = AdminThumbnail(
        image_field='layout_small',
        template='admin/display_link_thumbnail.html',
    )
    thumbnail.short_description = _('миниатюра')

    # if it will be registred then show link
    show_change_link = True

    def formfield_for_manytomany(self, db_field, request, **kwargs):
        if db_field.name == "buildings":
            try:
                parent_obj_id = request.resolver_match.args[0]
                kwargs["queryset"] = NewBuilding.objects.filter(
                    residental_complex=parent_obj_id)
            except IndexError:
                pass
        return super(NewApartmentInline, self).formfield_for_foreignkey(
            db_field, request, **kwargs)


class BuildingInlineFormSet(BaseInlineFormSet):
    def clean(self):
        '''Checks if there are no two Forms
        with the same values at unique_together'''
        if any(self.errors):
            return

        unique_together_values = []
        unique_together = NewBuilding._meta.unique_together

        def checkIfUnique(_form, unique_names):
            output = []
            for val_name in unique_names:
                if type(val_name) in (tuple, list):
                    return checkIfUnique(_form, val_name)
                value = form.cleaned_data[val_name]
                output.append(value)

            if output in unique_together_values:
                if 'name' in unique_names:
                    form.add_error(
                        None, _('Поля "<NAME>" \
                            в данном жк должны быть уникальны')
                    )
                else:
                    form.add_error(None, _(
                        'Совокупность полей "Улица", "Номер дома" \
                        и "Корпус" должна быть уникальна для всех домов'))
            else:
                unique_together_values.append(output)

        for form in self.forms:
            if form.cleaned_data:
                checkIfUnique(form, unique_together)


class BuildingInline(admin.StackedInline):
    form = NewBuildingForm
    formset = BuildingInlineFormSet
    model = NewBuilding
    extra = 0
    formfield_overrides = standart_formfield_overrides
    fields = ('name',
              'street',
              'building',
              'building_block',
              'coordinates',
              'is_active',
              'building_type',
              'number_of_storeys',
              'date_of_start_of_construction',
              'date_of_construction',
              'building_permit',
              'project_declarations'
              )
    classes = ['collapse', ]


class ResidentalComplexImageForm(forms.ModelForm):
    class Meta:
        model = ResidentalComplexImage
        widgets = {
            'image': forms.widgets.ClearableFileInput(attrs={'multiple': True})
        }
        fields = '__all__'


class ResidentalComplexImageInline(admin.TabularInline):
    model = ResidentalComplexImage
    form = ResidentalComplexImageForm
    extra = 0
    min_num = 0
    classes = ['collapse', ]


class ResidentalComplexFeatureInline(admin.TabularInline):
    model = ResidentalComplexFeature
    extra = 0
    min_num = 0
    formfield_overrides = standart_formfield_overrides
    classes = ['collapse', ]


@admin.register(ResidentalComplex)
class ResidentalComplexAdmin(
    MultiuploadInlinesContainerMixin,
    admin.ModelAdmin
):
    related_inline_form = ResidentalComplexImageForm
    related_inline_fk = 'residental_complex'
    form = ResidentalComplexForm
    inlines = [ResidentalComplexFeatureInline,
               BuildingInline,
               NewApartmentInline,
               ResidentalComplexImageInline,
               ]
    list_display = ('name', 'is_active')
    list_editable = ('is_active',)
    list_filter = ['is_active']
    search_fields = ['name']
    filter_horizontal = ['characteristics']
    fieldsets = (
        (None, {
            'fields': ('type_of_complex',
                       'name',
                       'neighbourhood',
                       'is_active',
                       'is_popular',
                       'builder',
                       'description',
                       'characteristics',
                       'number_of_buildings',
                       'number_of_flats')
        }),
        (_('МЕДИА'), {
            'classes': ['collapse', ],
            'fields': ('front_image', 'video_link', 'presentation'),
        }),
    )

    def get_inline_instances(self, request, obj=None):
        """Override to exclude NewApartmentInline
        from newly added forms"""
        from copy import deepcopy

        original_inlines = deepcopy(self.inlines)
        if not obj:
            self.inlines.pop(self.inlines.index(NewApartmentInline))

        answer = super().get_inline_instances(request, obj=None)
        self.inlines = original_inlines
        return answer

{'fraction_non_alphanumeric': 0.08228311244816652, 'fraction_numerical': 0.0011383039271485487, 'mean_word_length': 2.0819343522926586, 'pattern_counts': {'":': 2, '<': 4, '<?xml version=': 0, '>': 4, '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': '7474945', 'n_tokens_mistral': 3410, 'n_tokens_neox': 3340, 'n_words': 672}