Skip to content

Commit

Permalink
Factor out partner group name shortening logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcarson committed Jun 28, 2024
1 parent 0ba39fe commit b839fa0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
21 changes: 12 additions & 9 deletions gregor_django/users/audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,17 @@ def get_study_sites(json_api):
return study_sites_info


def partner_group_short_name_from_full_name(full_name):
short_name_parts = full_name.split(" - ")
if short_name_parts:
short_name = short_name_parts[0]

short_name_max_len = PartnerGroup._meta.get_field("short_name").max_length
if len(short_name) > short_name_max_len:
short_name = short_name[slice(short_name_max_len)]
return short_name


def get_partner_groups(json_api):
partner_groups_endpoint = json_api.endpoint("node/partner_group")
partner_groups_response = partner_groups_endpoint.get()
Expand All @@ -651,15 +662,7 @@ def get_partner_groups(json_api):
full_name = ss.attributes["title"]
# try to figure out short name - try to split on dash
# or just truncate to max len of field

short_name = ss.attributes["title"]
short_name_parts = short_name.split(" - ")
if short_name_parts:
short_name = short_name_parts[0]

short_name_max_len = PartnerGroup._meta.get_field("short_name").max_length
if len(short_name) > short_name_max_len:
short_name = short_name[slice(short_name_max_len)]
short_name = partner_group_short_name_from_full_name(full_name)

node_id = ss.attributes["drupal_internal__nid"]

Expand Down
4 changes: 2 additions & 2 deletions gregor_django/users/tests/test_audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ def test_audit_partner_groups_with_group_update(self):
)
PartnerGroup.objects.create(
drupal_node_id=TEST_PARTNER_GROUP_DATA[1].drupal_internal__nid,
short_name=TEST_PARTNER_GROUP_DATA[1].title,
short_name=audit.partner_group_short_name_from_full_name(TEST_PARTNER_GROUP_DATA[1].title),
full_name=TEST_PARTNER_GROUP_DATA[1].title,
)
self.get_fake_json_api()
Expand Down Expand Up @@ -637,7 +637,7 @@ def test_sync_drupal_data_command_with_issues(self):
)
PartnerGroup.objects.create(
drupal_node_id="999999",
short_name=TEST_PARTNER_GROUP_DATA[0].title,
short_name=audit.partner_group_short_name_from_full_name(TEST_PARTNER_GROUP_DATA[0].title),
full_name=TEST_PARTNER_GROUP_DATA[0].title,
)
new_user = get_user_model().objects.create(username="username2", email="useremail2", name="user fullname2")
Expand Down

0 comments on commit b839fa0

Please sign in to comment.