import collections

from django import forms
from django.core.exceptions import ValidationError
from django.utils.translation import ngettext

from meinberlin.apps.users.models import User

from . import models


class OrganisationForm(forms.ModelForm):

    class Meta:
        model = models.Organisation
        fields = '__all__'

    def clean(self):
        groups = self.cleaned_data.get('groups')
        group_list = groups.values_list('id', flat=True)
        group_users = User.objects\
            .filter(groups__in=group_list)\
            .values_list('email', flat=True)
        duplicates = [item for item, count
                      in collections.Counter(group_users).items()
                      if count > 1]
        if duplicates:
            count = len(duplicates)
            message = ngettext(
                '%(duplicates)s is member of several '
                'groups in that organisation.',
                '%(duplicates)s are member of several '
                'groups in that organisation.',
                count) % {
                'duplicates': ', '.join(duplicates)
            }
            raise ValidationError(message)
        return self.cleaned_data

@@ -38,16 +38,61 @@
 t forms%0A
+from django.contrib.auth.models import Group%0A
 from dja
@@ -181,16 +181,109 @@
 ettext%0A%0A
+from adhocracy4.projects.models import Project%0Afrom meinberlin.apps.plans.models import Plan%0A
 from mei
@@ -536,16 +536,670 @@
 roups')%0A
+        old_groups = self._get_old_groups(groups)%0A        duplicates = self._check_user_twice(groups)%0A        if duplicates:%0A            raise ValidationError(%0A                self._get_duplicate_error_message(duplicates))%0A        self._delete_from_old_groups(old_groups)%0A        return self.cleaned_data%0A%0A    def _get_old_groups(self, groups):%0A        instance_groups = self.instance.groups.all()%0A        if instance_groups:%0A            form_groups = groups%0A            return instance_groups.exclude(id__in=%5B%0A                group.id for group in form_groups%0A            %5D)%0A        return Group.objects.none()%0A%0A    def _check_user_twice(self, groups):%0A
         
@@ -1281,16 +1281,17 @@
 .objects
+ 
 %5C%0A      
@@ -1326,16 +1326,17 @@
 up_list)
+ 
 %5C%0A      
@@ -1519,34 +1519,38 @@
 nt %3E 1%5D%0A        
-if
+return
  duplicates:%0A   
@@ -1536,38 +1536,90 @@
 eturn duplicates
-:
+%0A
 %0A    
+def _get_duplicate_error_message(self, duplicates):%0A
         count = 
@@ -1646,21 +1646,14 @@
     
-    message =
+return
  nge
@@ -1659,20 +1659,16 @@
 ettext(%0A
-    
         
@@ -1702,36 +1702,32 @@
 er of several '%0A
-    
             'gro
@@ -1758,36 +1758,32 @@
 .',%0A            
-    
 '%25(duplicates)s 
@@ -1810,36 +1810,32 @@
 l '%0A            
-    
 'groups in that 
@@ -1858,28 +1858,24 @@
             
- 
-   
 count) %25 %7B%0A 
@@ -1872,33 +1872,16 @@
 unt) %25 %7B
-%0A                
 'duplica
@@ -1911,95 +1911,356 @@
 tes)
+%7D%0A
 %0A    
-        %7D%0A            raise ValidationError(message)%0A        return self.cleaned_data
+def _delete_from_old_groups(self, old_groups):%0A        if old_groups:%0A            group_ids = old_groups.values_list('id', flat=True)%0A            plans = Plan.objects.filter(group_id__in=group_ids)%0A            projects = Project.objects.filter(group_id__in=group_ids)%0A            plans.update(group=None)%0A            projects.update(group=None)
 %0A

delete group information when group is removed from org