Commit f50f37d7 authored by Hervé  MENAGER's avatar Hervé MENAGER
Browse files

refactor queries to select contributors in a dedicated ModelManager

parent 08f22173
Pipeline #72763 failed with stage
in 10 minutes and 14 seconds
......@@ -2134,7 +2134,31 @@ def post_save_taskresult(sender, instance, created, *args, **kwargs):
User = get_user_model()
class ContributorManager(models.Manager):
"""
Model manager for the `Contributor` class
retrieves users which have a social account (i.e. an ORCID in iPPI-DB)
and have more contributed
"""
def get_queryset(self):
return (
super()
.get_queryset()
.annotate(
contributions_count=Count(
"contribution", filter=Q(contribution__validated=True)
)
)
.filter(socialaccount__isnull=False, contributions_count__gt=0)
)
class Contributor(User):
objects = ContributorManager()
class Meta:
proxy = True
......@@ -2254,6 +2278,6 @@ class Contributor(User):
Return last modification date for the contributor
based on latest contribution date
"""
return Contribution.objects.filter(
contributor=self, validated=True
).aggregate(Max("created_at"))["created_at__max"]
return Contribution.objects.filter(contributor=self, validated=True).aggregate(
Max("created_at")
)["created_at__max"]
......@@ -293,13 +293,6 @@ class ContributorListView(ListView):
model = Contributor
template_name = "about-contributors.html"
def get_queryset(self) -> QuerySet[Contributor]:
qs = super().get_queryset()
qs = qs.annotate(contributions_count=Count("contribution")).filter(
socialaccount__isnull=False, contributions_count__gt=0
)
return qs
class ContributorDetailView(DetailView):
"""
......@@ -309,10 +302,3 @@ class ContributorDetailView(DetailView):
model = Contributor
template_name = "about-contributor-card.html"
def get_queryset(self):
qs = super().get_queryset()
qs = qs.annotate(contributions_count=Count("contribution")).filter(
socialaccount__isnull=False, contributions_count__gt=0
)
return qs
......@@ -15,8 +15,6 @@ class IppiDbContributorsSitemap(Sitemap):
def items(self):
return (
Contributor.objects.all()
.annotate(contributions_count=Count("contribution"))
.filter(socialaccount__isnull=False, contributions_count__gt=0)
)
def lastmod(self, obj):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment