Skip to content

Commit

Permalink
refactor: move logic to CodeMetaSchema
Browse files Browse the repository at this point in the history
  • Loading branch information
alee committed Sep 5, 2024
1 parent fd131d6 commit 92a5290
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 49 deletions.
6 changes: 1 addition & 5 deletions django/core/view_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,6 @@ def get_search_queryset(
criteria.update(tags__name__in=[t.lower() for t in tags])
operator = 'and'
"""

"""
Build text search query
"""
if query:
Query.get(query).add_hit()

Expand All @@ -120,6 +116,7 @@ def get_search_queryset(
# filters, query = parse_query_string(query, operator="and")
# criteria.update(filters)

# generate search query from text
query = build_search_query(query)
else:
query = MATCH_ALL
Expand Down Expand Up @@ -176,7 +173,6 @@ def retrieve_with_perms(self, request, *args, **kwargs):


def add_user_retrieve_perms(instance, data, user):
print(user.get_all_permissions())
data["has_change_perm"] = user.has_perm(
f"{instance._meta.app_label}.change_{instance._meta.model_name}",
instance,
Expand Down
76 changes: 41 additions & 35 deletions django/library/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,14 @@ def to_affiliation_string(cls, afl):
@property
def codemeta_affiliation(self):
"""
FIXME: move to CodeMeta class
For now codemeta affiliations appear to be a single https://schema.org/Organization
"""
if self.json_affiliations:
return self.to_codemeta_affiliation(self.json_affiliations[0])
return CodeMetaSchema.convert_affiliation(self.json_affiliations[0])

@property
def primary_affiliation(self):
return self.json_affiliations[0] if self.json_affiliations else {}

@property
def primary_affiliation_name(self):
Expand All @@ -204,20 +207,6 @@ def primary_affiliation_name(self):
def primary_json_affiliation_name(self):
return self.json_affiliations[0]["name"] if self.json_affiliations else ""

def to_codemeta_affiliation(self, affiliation):
# FIXME: move to CodeMeta class
if affiliation:
return {
# FIXME: may switch to https://schema.org/ResearchOrganization at some point
"@type": "Organization",
"@id": affiliation.get("ror_id"),
"name": affiliation.get("name"),
"url": affiliation.get("url"),
"identifier": affiliation.get("ror_id"),
"sameAs": affiliation.get("ror_id"),
}
return {}

@staticmethod
def from_user(user):
"""
Expand Down Expand Up @@ -274,21 +263,8 @@ def member_profile_url(self):
def get_markdown_link(self):
return f"[{self.get_full_name()}]({self.member_profile_url})"

# FIXME: move to CodeMeta transformer class
def to_codemeta(self):
codemeta = {
"@type": "Person",
# FIXME: Contributor should proxy to User / MemberProfile fields if User is available and given_name and family_name are not set
"givenName": self.given_name,
"familyName": self.family_name,
}
if self.orcid_url:
codemeta["@id"] = self.orcid_url
if self.json_affiliations:
codemeta["affiliation"] = self.codemeta_affiliation
if self.email:
codemeta["email"] = self.email
return codemeta
return CodeMetaSchema.convert_contributor(self)

def get_aggregated_search_fields(self):
return " ".join(
Expand Down Expand Up @@ -2596,7 +2572,7 @@ def convert_platforms(self):
def license_url(self):
if self.license:
return self.license.url
return "DEFAULT LICENSE - https://opensource.org/licenses/MIT"
return "DEFAULT LICENSE: https://opensource.org/licenses/MIT"

@property
def descriptions(self):
Expand Down Expand Up @@ -2673,10 +2649,8 @@ def convert(cls, codebase_release: CodebaseRelease):
common_metadata = codebase_release.common_metadata
metadata = {
**cls.INITIAL_METADATA,
# FIXME: inherit fields from common_metadata, look into how to do this
# better, snake_case to camelCase of variable names is also an issue
**common_metadata.to_dict(),
"@id": common_metadata.permanent_url,
"name": common_metadata.name,
"copyrightYear": common_metadata.copyright_year,
"dateCreated": common_metadata.date_created.isoformat(),
"dateModified": common_metadata.date_modified.isoformat(),
Expand Down Expand Up @@ -2717,10 +2691,42 @@ def to_creative_work(cls, text):
@classmethod
def convert_authors(cls, common_metadata: CommonMetadata):
return [
author.contributor.to_codemeta()
cls.convert_contributor(author.contributor)
for author in common_metadata.release_contributor_authors
]

@classmethod
def convert_ror_affiliation(cls, affiliation: dict):
if affiliation:
return {
# FIXME: may switch to https://schema.org/ResearchOrganization at some point
"@type": "Organization",
"@id": affiliation.get("ror_id"),
"name": affiliation.get("name"),
"url": affiliation.get("url"),
"identifier": affiliation.get("ror_id"),
"sameAs": affiliation.get("ror_id"),
}
return {}

@classmethod
def convert_contributor(cls, contributor: Contributor):
codemeta = {
"@type": "Person",
# FIXME: Contributor should proxy to User / MemberProfile fields if User is available and given_name and family_name are not set
"givenName": contributor.given_name,
"familyName": contributor.family_name,
}
if contributor.orcid_url:
codemeta["@id"] = contributor.orcid_url
if contributor.json_affiliations:
codemeta["affiliation"] = cls.convert_ror_affiliation(
contributor.primary_affiliation
)
if contributor.email:
codemeta["email"] = contributor.email
return codemeta

@classmethod
def convert_target_product(cls, common_metadata: CommonMetadata):
target_product = {
Expand Down
10 changes: 1 addition & 9 deletions django/library/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,20 +414,12 @@ def test_list_files(self):
self.assertEqual(
response.status_code,
expected_status_code,
msg="{} {}".format(repr(user), response.data),
msg=f"{user} {response.data}",
)

def test_delete_file(self):
path_to_foo = pathlib.Path("foo.txt")
api = self.codebase_release.get_fs_api()
print(self.codebase_release)
print(
"CodebaseRelease perm %s"
% self.submitter.has_perm(
"library.delete_codebaserelease", self.codebase_release
)
)

# Unpublished codebase release permissions
response = self.client.delete(
api.get_absolute_url(
Expand Down

0 comments on commit 92a5290

Please sign in to comment.