@@ -313,7 +313,7 @@ def __init__(self, *args, **kwargs):
313313
314314 def clean_primary_email (self ):
315315 email = self .cleaned_data ['primary_email' ]
316- nominees = Nominee .objects .get_by_nomcom (self .nomcom ).filter (email__address = email )
316+ nominees = Nominee .objects .get_by_nomcom (self .nomcom ).not_duplicated (). filter (email__address = email )
317317 if not nominees :
318318 msg = "Does not exist a nomiee with this email"
319319 self ._errors ["primary_email" ] = self .error_class ([msg ])
@@ -807,8 +807,7 @@ def save(self, commit=True):
807807 nominee = nominee ,
808808 comments = feedback ,
809809 nominator_email = nominator_email ,
810- user = self .user ,
811- )
810+ user = self .user )
812811 return feedback
813812 else :
814813 feedback .save ()
@@ -827,3 +826,45 @@ class FullFeedbackFormSet(forms.models.BaseModelFormSet):
827826 form = MutableFeedbackForm
828827 can_order = False
829828 can_delete = False
829+
830+
831+ class EditNomineeForm (forms .ModelForm ):
832+
833+ nominee_email = forms .EmailField (label = "Nominee email" ,
834+ widget = forms .TextInput (attrs = {'size' : '40' }))
835+
836+ def __init__ (self , * args , ** kwargs ):
837+ super (EditNomineeForm , self ).__init__ (* args , ** kwargs )
838+ self .fields ['nominee_email' ].initial = self .instance .email .address
839+
840+ def save (self , commit = True ):
841+ nominee = super (EditNomineeForm , self ).save (commit = False )
842+ nominee_email = self .cleaned_data .get ("nominee_email" )
843+ if nominee_email != nominee .email .address :
844+ # create a new nominee with the new email
845+ new_email , created_email = Email .objects .get_or_create (address = nominee_email )
846+ new_email .person = nominee .email .person
847+ new_email .save ()
848+
849+ # Chage emails between nominees
850+ old_email = nominee .email
851+ nominee .email = new_email
852+ nominee .save ()
853+ new_nominee = Nominee .objects .create (email = old_email , nomcom = nominee .nomcom )
854+
855+ # new nominees point to old nominee
856+ new_nominee .duplicated = nominee
857+ new_nominee .save ()
858+
859+ return nominee
860+
861+ class Meta :
862+ model = Nominee
863+ fields = ('nominee_email' ,)
864+
865+ def clean_nominee_email (self ):
866+ nominee_email = self .cleaned_data ['nominee_email' ]
867+ nominees = Nominee .objects .exclude (email__address = self .instance .email .address ).filter (email__address = nominee_email )
868+ if nominees :
869+ raise forms .ValidationError ('This emails already does exists in another nominee, please go to merge form' )
870+ return nominee_email
0 commit comments