@@ -373,3 +373,63 @@ def form_valid(self, form):
373373
374374 def get_success_url (self ):
375375 return reverse ('institutions:register_metrics_admin' , kwargs = {'institution_id' : self .kwargs ['institution_id' ]})
376+
377+
378+ class InstitutionAffiliationBaseView (PermissionRequiredMixin , ListView ):
379+ permission_required = 'osf.change_institution'
380+ template_name = 'institutions/edit_affiliations.html'
381+ raise_exception = True
382+
383+ def get_queryset (self ):
384+ return Institution .objects .get (id = self .kwargs ['institution_id' ])
385+
386+ def get_context_data (self , ** kwargs ):
387+ institution = Institution .objects .get (id = self .kwargs ['institution_id' ])
388+ context = super ().get_context_data (** kwargs )
389+ context ['institution' ] = institution
390+ context ['affiliations' ] = institution .get_institution_users ()
391+ return context
392+
393+
394+ class InstitutionListAndAddAffiliation (InstitutionAffiliationBaseView ):
395+
396+ def get_permission_required (self ):
397+ if self .request .method == 'GET' :
398+ return ('osf.view_institution' ,)
399+ return (self .permission_required ,)
400+
401+ def post (self , request , * args , ** kwargs ):
402+ institution = Institution .objects .get (id = self .kwargs ['institution_id' ])
403+ data = dict (request .POST )
404+ del data ['csrfmiddlewaretoken' ] # just to remove the key from the form dict
405+
406+ target_user = OSFUser .load (data ['add-affiliation-form' ][0 ])
407+ if target_user is None :
408+ messages .error (request , f'User for guid: { data ["add-affiliation-form" ][0 ]} could not be found' )
409+ return redirect ('institutions:affiliations' , institution_id = institution .id )
410+
411+ target_user .add_or_update_affiliated_institution (institution )
412+
413+ messages .success (request , f'The following user was successfully added: { target_user .fullname } ({ target_user .username } )' )
414+
415+ return redirect ('institutions:affiliations' , institution_id = institution .id )
416+
417+
418+ class InstitutionRemoveAffiliation (InstitutionAffiliationBaseView ):
419+
420+ def post (self , request , * args , ** kwargs ):
421+ institution = Institution .objects .get (id = self .kwargs ['institution_id' ])
422+ data = dict (request .POST )
423+ del data ['csrfmiddlewaretoken' ] # just to remove the key from the form dict
424+
425+ to_be_removed = list (data .keys ())
426+ removed_affiliations = [user .replace ('User-' , '' ) for user in to_be_removed if 'User-' in user ]
427+ affiliated_users = OSFUser .objects .filter (id__in = removed_affiliations )
428+ for user in affiliated_users :
429+ user .remove_affiliated_institution (institution ._id )
430+
431+ if affiliated_users :
432+ users_names = ' ,' .join (affiliated_users .values_list ('fullname' , flat = True ))
433+ messages .success (request , f'The following users were successfully removed: { users_names } ' )
434+
435+ return redirect ('institutions:affiliations' , institution_id = institution .id )
0 commit comments