@@ -911,14 +911,90 @@ def can_be_asked_for_upload(self):
911911 @cached_property
912912 def owners (self ):
913913 """
914- Returns the list of users that own this papers (listed as authors and identified as such).
914+ Returns the list of users that own this paper (listed as authors and identified as such).
915915 """
916916 users = []
917917 for a in self .authors :
918918 if a .researcher and a .researcher .user :
919919 users .append (a .researcher .user )
920920 return users
921921
922+ def can_be_claimed_by (self , user ):
923+ """
924+ Is it possible for user to claim this paper?
925+ """
926+ for author in self .authors :
927+ first_name = None
928+ last_name = None
929+ try :
930+ first_name = user .first_name
931+ last_name = user .last_name
932+ except AttributeError :
933+ # anonymous user
934+ return False
935+ if not match_names (author .name .pair ,
936+ (first_name , last_name )):
937+ continue
938+ if author .orcid :
939+ continue
940+ if author .researcher_id :
941+ continue
942+ return True
943+ return False
944+
945+ def unclaim_for (self , user ):
946+ """
947+ Remove all associations between this paper and user
948+ """
949+ try :
950+ user_researcher = Researcher .objects .get (user = user )
951+ except Researcher .DoesNotExist :
952+ user_researcher = None
953+ user_orcid = None
954+ if user_researcher :
955+ user_orcid = user_researcher .orcid
956+ for idx , author in enumerate (self .authors ):
957+ if (author .orcid == user_orcid or author .researcher_id ==
958+ user_researcher .id ):
959+ # remove association between this author and user
960+ self .authors_list [idx ]['orcid' ] = None
961+ self .authors_list [idx ]['researcher_id' ] = None
962+ self .save ()
963+ self .update_index ()
964+ return True
965+ # nothing was done, paper cannot be unclaimed
966+ raise False
967+
968+ def claim_for (self , user ):
969+ """
970+ Associate this paper to the requested user
971+ """
972+ if user in self .owners :
973+ # user already owns the paper
974+ return False
975+ try :
976+ user_researcher = Researcher .objects .get (user = user )
977+ except Researcher .DoesNotExist :
978+ user_researcher = None
979+ user_orcid = None
980+ if user_researcher :
981+ user_orcid = user_researcher .orcid
982+ for idx , author in enumerate (self .authors ):
983+ if not match_names (author .name .pair ,
984+ (user .first_name , user .last_name )):
985+ continue
986+ if author .orcid :
987+ continue
988+ if author .researcher_id :
989+ continue
990+ self .authors_list [idx ]['orcid' ] = user_orcid
991+ self .authors_list [idx ]['researcher_id' ] = user_researcher .id
992+ self .save ()
993+ self .update_index ()
994+ return True
995+ # paper cannot be claimed by user
996+ raise ValueError
997+
922998 def is_owned_by (self , user , flexible = False ):
923999 """
9241000 Is this user one of the owners of that paper?
0 commit comments