<commit_before>from crispy_forms.layout import Div
from django import forms
from geotrek.common.forms import CommonForm
from geotrek.outdoor.models import Site, Practice, SitePractice


class SiteForm(CommonForm):
    practices = forms.ModelMultipleChoiceField(
        queryset=Practice.objects.all(),
        widget=forms.CheckboxSelectMultiple(),
        required=False
    )

    geomfields = ['geom']

    fieldslayout = [
        Div(
            'structure',
            'name',
            'description',
            'practices',
            'eid',
        )
    ]

    class Meta:
        fields = ['structure', 'name', 'description', 'geom', 'practices', 'eid']
        model = Site

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['practices'].initial = self.instance.site_practices.values_list('practice', flat=True)

    def save(self, commit=True):
        site = super().save(commit=commit)
        if commit:
            for practice in Practice.objects.all():
                if practice in self.cleaned_data['practices']:
                    SitePractice.objects.get_or_create(site=site, practice=practice)
                else:
                    SitePractice.objects.filter(site=site, practice=practice).delete()
            site.save()
        return site<commit_msg>Multiselect to choose site practices<commit_after>from crispy_forms.layout import Div
from django import forms
from geotrek.common.forms import CommonForm
from geotrek.outdoor.models import Site, Practice, SitePractice


class SiteForm(CommonForm):
    practices = forms.ModelMultipleChoiceField(
        queryset=Practice.objects.all(),
        required=False
    )

    geomfields = ['geom']

    fieldslayout = [
        Div(
            'structure',
            'name',
            'practices',
            'description',
            'eid',
        )
    ]

    class Meta:
        fields = ['structure', 'name', 'description', 'geom', 'practices', 'eid']
        model = Site

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['practices'].initial = self.instance.site_practices.values_list('practice', flat=True)

    def save(self, commit=True):
        site = super().save(commit=commit)
        if commit:
            for practice in Practice.objects.all():
                if practice in self.cleaned_data['practices']:
                    SitePractice.objects.get_or_create(site=site, practice=practice)
                else:
                    SitePractice.objects.filter(site=site, practice=practice).delete()
            site.save()
        return site
{'fraction_non_alphanumeric': 0.11373873873873874, 'fraction_numerical': 0.0, 'mean_word_length': 2.3270911360799, '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': '2609445', 'n_tokens_mistral': 766, 'n_tokens_neox': 733, 'n_words': 156}