Skip to content

Commit

Permalink
Feat: Update create profile method (#2448)
Browse files Browse the repository at this point in the history
* Feat: Update create profile method

* Feat: Update test to use API V2

---------

Co-authored-by: celestemartinez <[email protected]>
  • Loading branch information
carlosmondra and celestemartinez authored Dec 11, 2024
1 parent d5d540f commit 121fe2c
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 93 deletions.
16 changes: 6 additions & 10 deletions openreview/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2095,22 +2095,18 @@ def get_notes_by_ids(self, ids):
response = self.__handle_response(response)
return [Note.from_json(n) for n in response.json()['notes']]

def get_tildeusername(self, first, last, middle = None):
def get_tildeusername(self, fullname):
"""
Gets next possible tilde user name corresponding to the specified first, middle and last name
Gets next possible tilde user name corresponding to the specified full name
:param first: First name of the user
:type first: str
:param last: Last name of the user
:type last: str
:param middle: Middle name of the user
:type middle: str, optional
:param fullname: Full name of the user
:type fullname: str
:return: next possible tilde user name corresponding to the specified first, middle and last name
:return: next possible tilde user name corresponding to the specified full name
:rtype: dict
"""

response = self.session.get(self.tilde_url, params = { 'first': first, 'last': last, 'middle': middle }, headers = self.headers)
response = self.session.get(self.tilde_url, params = { 'fullname': fullname }, headers = self.headers)
response = self.__handle_response(response)
return response.json()

Expand Down
138 changes: 63 additions & 75 deletions openreview/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ def get_invitation(client, id):
print('Can not retrieve invitation', e)
return invitation

def create_profile(client, email, fullname, url='http://no_url', allow_duplicates=False):
def create_profile(client, email, fullname, super_user='openreview.net'):

"""
Given email, first name, last name, and middle name (optional), creates a new profile.
Expand All @@ -352,98 +352,86 @@ def create_profile(client, email, fullname, url='http://no_url', allow_duplicate
:type client: Client
:param email: Preferred e-mail in the Profile
:type email: str
:param first: First name of the user
:type first: str
:param last: Last name of the user
:type last: str
:param middle: Middle name of the user
:type middle: str, optional
:param url: Homepage url
:type url: str, optional
:param allow_duplicates: If a profile with the same name exists, and allow_duplicates is False, an exception is raised. If a profile with the same name exists and allow_duplicates is True, a profile is created with the next largest number (e.g. if ~Michael_Spector1 exists, ~Michael_Spector2 will be created)
:type allow_duplicates: bool, optional
:param fullname: Full name of the user
:type fullname: str
:param super_user: Super user of the system
:type super_user: str
:return: The created Profile
:rtype: Profile
"""

profile = get_profile(client, email)

if not profile:

# validate the name with just first and last names,
# and also with first, middle, and last.
# this is so that we catch more potential collisions;
# let the caller decide what to do with false positives.
if profile:
raise openreview.OpenReviewException('There is already a profile with this email address: {}'.format(email))

username_response = client.get_tildeusername(fullname)
username_response = client.get_tildeusername(fullname)

# the username in each response will end with 1
# if profiles don't exist for those names
username_unclaimed = username_response['username'].endswith('1')
tilde_id = username_response['username']

if username_unclaimed:
profile_exists = False
else:
profile_exists = True

tilde_id = username_response['username']
if (not profile_exists) or allow_duplicates:

tilde_group = openreview.Group(id=tilde_id, signatures=[client.profile.id], signatories=[tilde_id], readers=[tilde_id], writers=[client.profile.id], members=[email])
email_group = openreview.Group(id=email, signatures=[client.profile.id], signatories=[email], readers=[email], writers=[client.profile.id], members=[tilde_id])
profile_content = {
'emails': [email],
'preferredEmail': email,
'names': [
{
'fullname': fullname,
'username': tilde_id
}
],
'homepage': url
tilde_group = openreview.api.Group(id=tilde_id, signatures=[client.profile.id], signatories=[tilde_id], readers=[tilde_id], writers=[client.profile.id], members=[email])
email_group = openreview.api.Group(id=email, signatures=[client.profile.id], signatories=[email], readers=[email], writers=[client.profile.id], members=[tilde_id])
profile_content = {
'emails': [email],
'preferredEmail': email,
'names': [
{
'fullname': fullname,
'username': tilde_id
}
client.post_group(tilde_group)
client.post_group(email_group)

profile = client.post_profile(openreview.Profile(id=tilde_id, content=profile_content, signatures=[tilde_id]))
],
}
client.post_group_edit(
f'{super_user}/-/Username',
signatures=[super_user],
readers=[tilde_id],
writers=[super_user],
group=tilde_group
)
client.post_group_edit(
f'{super_user}/-/Email',
signatures=[super_user],
readers=[tilde_id],
writers=[super_user],
group=email_group
)

return profile
profile = client.post_profile(openreview.Profile(id=tilde_id, content=profile_content, signatures=[tilde_id]))

else:
raise openreview.OpenReviewException(
'Failed to create new profile {tilde_id}: There is already a profile with the name: \"{fullname}\"'.format(
fullname=fullname, tilde_id=tilde_id))
else:
raise openreview.OpenReviewException('There is already a profile with this email address: {}'.format(email))
return profile

def create_authorid_profiles(client, note, print=print):
def create_authorid_profiles(client, note):
# for all submissions get authorids, if in form of email address, try to find associated profile
# if profile doesn't exist, create one
created_profiles = []

if 'authorids' in note.content and 'authors' in note.content:
author_names = [a.replace('*', '') for a in note.content['authors']]
author_emails = [e for e in note.content['authorids']]
if len(author_names) == len(author_emails):
# iterate through authorids and authors at the same time
for (author_id, author_name) in zip(author_emails, author_names):
author_id = author_id.strip()
author_name = author_name.strip()

if '@' in author_id:
try:
profile = create_profile(client=client, email=author_id, fullname=author_name)
created_profiles.append(profile)
print('{}: profile created with id {}'.format(note.id, profile.id))
except openreview.OpenReviewException as e:
print('Error while creating profile for note id {note_id}, author {author_id}, '.format(note_id=note.id, author_id=author_id), e)
else:
print('{}: length mismatch. authors ({}), authorids ({})'.format(
note.id,
len(author_names),
len(author_emails)
))
if not 'authors' in note.content or not 'authorids' in note.content:
return created_profiles

author_names = [a.replace('*', '') for a in note.content['authors']['value']]
author_emails = [e for e in note.content['authorids']['value']]

if len(author_names) != len(author_emails):
print('{}: length mismatch. authors ({}), authorids ({})'.format(
note.id,
len(author_names),
len(author_emails)
))
return created_profiles

# iterate through authorids and authors at the same time
for (author_id, author_name) in zip(author_emails, author_names):
author_id = author_id.strip()
author_name = author_name.strip()

if '@' in author_id:
try:
profile = create_profile(client=client, email=author_id, fullname=author_name)
created_profiles.append(profile)
print('{}: profile created with id {}'.format(note.id, profile.id))
except openreview.OpenReviewException as e:
print('Error while creating profile for note id {note_id}, author {author_id}, '.format(note_id=note.id, author_id=author_id), e)

return created_profiles

Expand Down
16 changes: 8 additions & 8 deletions tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,33 +263,33 @@ def test_get_preferred_name(self, client, test_client):
assert preferred_name, "preferred name not found"
assert preferred_name == 'SomeFirstName User'

def test_create_authorid_profiles(self, client):
def test_create_authorid_profiles(self, openreview_client):
authors = [
'Ada Lovelace',
'Alan Turing',
'Edsger W. Dijkstra',
'Grace Hopper'
'Grace Hopper',
]

authorids = [
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]'
'[email protected]',
]

note = openreview.Note.from_json({
note = openreview.api.Note.from_json({
'id': 'MOCK_NOTE',
'content': {
'authors': authors,
'authorids': authorids
'authors': {'value': authors},
'authorids': {'value': authorids},
}
})

openreview.tools.create_authorid_profiles(client, note)
openreview.tools.create_authorid_profiles(openreview_client, note)

for author, email in zip(authors, authorids):
result = client.search_profiles(term=author)
result = openreview_client.search_profiles(term=author)
assert any([email in p.content['emails'] for p in result])

def test_subdomains(self):
Expand Down

0 comments on commit 121fe2c

Please sign in to comment.