from crispy_forms.layout import Div
from geotrek.common.forms import CommonForm
from geotrek.outdoor.models import Site


class SiteForm(CommonForm):
    geomfields = ['geom']

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

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

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

Add multiple choice of practices to Site form
Add multiple choice of practices to Site form

  from crispy_forms.layout import Div
+ from django import forms
  from geotrek.common.forms import CommonForm
- from geotrek.outdoor.models import Site
+ 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', 'eid']
+         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
+ 
Add multiple choice of practices to Site form
## Code Before:
from crispy_forms.layout import Div
from geotrek.common.forms import CommonForm
from geotrek.outdoor.models import Site


class SiteForm(CommonForm):
    geomfields = ['geom']

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

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

## Instruction:
Add multiple choice of practices to Site form
## Code 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(),
        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
