Skip to content

Commit

Permalink
Merge pull request #1635 from ResearchHub/mock-rep-v2-in-author-profile
Browse files Browse the repository at this point in the history
Mock rep v2 in author profile
  • Loading branch information
koutst committed Jun 18, 2024
2 parents 60b7244 + 8e93e06 commit 60ed22e
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
64 changes: 64 additions & 0 deletions src/user/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,8 @@ def get_coauthor(self, coauthor):
class DynamicAuthorProfileSerializer(DynamicModelFieldSerializer):
institutions = SerializerMethodField()
coauthors = SerializerMethodField()
reputation = SerializerMethodField()
reputation_list = SerializerMethodField()
activity_by_year = SerializerMethodField()
works_count = SerializerMethodField()
citation_count = SerializerMethodField()
Expand Down Expand Up @@ -1128,6 +1130,68 @@ def get_open_access_pct(self, author):
/ total_paper_count
)

def get_reputation(self, author):
return {
"hub_id": 1,
"hub_name": "Cellular and Molecular Neuroscience",
"hub_slug": "slug-1",
"score": 198200,
"bins": [[0, 1000], [1000, 10000], [10000, 100000], [100000, 1000000]],
}

def get_reputation_list(self, author):
return [
{
"hub_id": 1,
"hub_name": "Cellular and Molecular Neuroscience",
"hub_slug": "slug-1",
"score": 198200,
"bins": [[0, 1000], [1000, 10000], [10000, 100000], [100000, 1000000]],
},
{
"hub_id": 2,
"hub_name": "Molecular Biology",
"hub_slug": "slug-2",
"score": 175000,
"bins": [[0, 1000], [1000, 10000], [10000, 100000], [100000, 1000000]],
},
{
"hub_id": 3,
"hub_name": "Cognitive Neuroscience",
"hub_slug": "slug-3",
"score": 65000,
"bins": [[0, 1000], [1000, 10000], [10000, 100000], [100000, 1000000]],
},
{
"hub_id": 4,
"hub_name": "Ophthalmology",
"hub_slug": "slug-4",
"score": 12200,
"bins": [[0, 1000], [1000, 10000], [10000, 100000], [100000, 1000000]],
},
{
"hub_id": 5,
"hub_name": "Endocrine and Autonomic Systems",
"hub_slug": "slug-5",
"score": 10000,
"bins": [[0, 1000], [1000, 10000], [10000, 100000], [100000, 1000000]],
},
{
"hub_id": 6,
"hub_name": "Artificial Intelligence",
"hub_slug": "slug-6",
"score": 1050,
"bins": [[0, 1000], [1000, 10000], [10000, 100000], [100000, 1000000]],
},
{
"hub_id": 7,
"hub_name": "Biomedical Engineering",
"hub_slug": "slug-7",
"score": 330,
"bins": [[0, 1000], [1000, 10000], [10000, 100000], [100000, 1000000]],
},
]

def get_institutions(self, author):
context = self.context
_context_fields = context.get("author_profile::get_institutions", {})
Expand Down
29 changes: 29 additions & 0 deletions src/user/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,35 @@ def test_set_has_seen_first_coin_modal(self):
user.refresh_from_db()
self.assertTrue(user.has_seen_first_coin_modal)

@patch.object(OpenAlex, "get_authors")
def test_get_author_profile_reputation(self, mock_get_authors):
from paper.models import Paper

works = None
with open("./paper/tests/openalex_works.json", "r") as file:
response = json.load(file)
works = response.get("results")

with open("./paper/tests/openalex_authors.json", "r") as file:
mock_data = json.load(file)
mock_get_authors.return_value = (mock_data["results"], None)

process_openalex_works(works)

dois = [work.get("doi") for work in works]
dois = [doi.replace("https://doi.org/", "") for doi in dois]

papers = Paper.objects.filter(doi__in=dois)
first_author = papers.first().authors.first()

url = f"/api/author/{first_author.id}/profile/"
response = self.client.get(
url,
)

self.assertGreater(response.data["reputation"]["score"], 0)
self.assertGreater(len(response.data["reputation_list"]), 0)

@patch.object(OpenAlex, "get_authors")
def test_author_overview(self, mock_get_authors):
from paper.models import Paper
Expand Down
20 changes: 20 additions & 0 deletions src/user/views/author_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,24 @@ def profile(self, request, pk=None):
"institution",
]
},
"author_profile::get_reputation": {
"_include_fields": [
"hub_id",
"hub_name",
"hub_slug",
"score",
"bins",
]
},
"author_profile::get_reputation_list": {
"_include_fields": [
"hub_id",
"hub_name",
"hub_slug",
"score",
"bins",
]
},
"author_profile::activity_by_year": {
"_include_fields": [
"year",
Expand Down Expand Up @@ -191,6 +209,8 @@ def profile(self, request, pk=None):
"created_date",
"country_code",
"coauthors",
"reputation",
"reputation_list",
"summary_stats",
"activity_by_year",
"open_access_pct",
Expand Down

0 comments on commit 60ed22e

Please sign in to comment.