@@ -247,9 +247,9 @@ def set_organization_field(self):
247247
248248 def get_post_only (self ):
249249 from_entity = self .get_from_entity ()
250- if self .person .liaisonmanagers_set .filter (sdo = from_entity .obj ):
251- return True
252- return False
250+ if is_secretariat ( self .user ) or self . person .sdoauthorizedindividual_set .filter (sdo = from_entity .obj ):
251+ return False
252+ return True
253253
254254 def clean (self ):
255255 if 'send' in self .data .keys () and self .get_post_only ():
@@ -271,15 +271,24 @@ def get_to_entity(self):
271271 return organization
272272
273273 def set_from_field (self ):
274- if is_secretariat (self .user ) or is_sdo_liaison_manager (self .person ):
274+ if is_secretariat (self .user ):
275+ self .fields ['from_field' ].choices = self .hm .get_all_incoming_entities ()
276+ elif is_sdo_liaison_manager (self .person ):
275277 self .fields ['from_field' ].choices = self .hm .get_all_incoming_entities ()
278+ all_entities = []
279+ for i in self .hm .get_entities_for_person (self .person ):
280+ all_entities += i [1 ]
281+ if all_entities :
282+ self .fields ['from_field' ].widget .full_power_on = [i [0 ] for i in all_entities ]
283+ self .fields ['from_field' ].widget .reduced_to_set = ['sdo_%s' % i .sdo .pk for i in self .person .liaisonmanagers_set .all ().distinct ()]
276284 else :
277285 self .fields ['from_field' ].choices = self .hm .get_entities_for_person (self .person )
278286 self .fields ['from_field' ].widget .submitter = unicode (self .person )
279287 self .fieldsets [0 ] = ('From' , ('from_field' , 'replyto' , 'approved' ))
280288
281289 def set_organization_field (self ):
282- if is_sdo_liaison_manager (self .person ):
290+ # If the user is a liaison manager and is nothing more, reduce the To field to his SDOs
291+ if not self .hm .get_entities_for_person (self .person ) and is_sdo_liaison_manager (self .person ):
283292 sdos = [i .sdo for i in self .person .liaisonmanagers_set .all ().distinct ()]
284293 self .fields ['organization' ].choices = [('sdo_%s' % i .pk , i .sdo_name ) for i in sdos ]
285294 else :
@@ -322,6 +331,24 @@ def clean_to_poc(self):
322331 self .check_email (value )
323332 return value
324333
334+ def clean_organization (self ):
335+ to_code = self .cleaned_data .get ('organization' , None )
336+ from_code = self .cleaned_data .get ('from_field' , None )
337+ if not to_code or not from_code :
338+ return to_code
339+ all_entities = []
340+ for i in self .hm .get_entities_for_person (self .person ):
341+ all_entities += i [1 ]
342+ # If the from entity is one in wich the user has full privileges the to entity could be anyone
343+ if from_code in [i [0 ] for i in all_entities ]:
344+ return to_code
345+ sdo_codes = ['sdo_%s' % i .sdo .pk for i in self .person .liaisonmanagers_set .all ().distinct ()]
346+ if to_code in sdo_codes :
347+ return to_code
348+ entity = self .get_to_entity ()
349+ entity_name = entity and entity .name or to_code
350+ raise forms .ValidationError ('You are not allowed to send a liaison to: %s' % entity_name )
351+
325352
326353class EditLiaisonForm (LiaisonForm ):
327354
0 commit comments