Skip to content

Commit

Permalink
Cleaned up logic
Browse files Browse the repository at this point in the history
  • Loading branch information
wes-otf committed Jan 22, 2025
1 parent 4c4d778 commit c59dcdf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
28 changes: 24 additions & 4 deletions hypha/apply/activity/services.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from django.contrib.contenttypes.models import ContentType
from django.db.models import OuterRef, Subquery
from django.db.models import OuterRef, Q, Subquery
from django.db.models.functions import JSONObject
from django.utils import timezone

from hypha.apply.funds.models.submissions import ApplicationSubmission
from hypha.apply.projects.models.project import Project
from hypha.apply.todo.models import Task

from .models import Activity
Expand Down Expand Up @@ -42,16 +44,34 @@ def get_related_activities_for_user(obj, user):
ApplicationSubmission and Project.
Args:
obj: instance of a model class
obj: instance of either an [`ApplicationSubmission`][hypha.apply.funds.models.submissions.ApplicationSubmission] or [`Project`][hypha.apply.projects.models.project.Project].
user: user who these actions are visible to.
Returns:
[`Activity`][hypha.apply.activity.models.Activity] queryset
"""
related_query = type(obj).activities.rel.related_query_name
proj_content_type = ContentType.objects.get_for_model(Project)
app_content_type = ContentType.objects.get_for_model(ApplicationSubmission)

# Determine if the provided object is an ApplicationSubmission or Project, then pull
# the related it's related Project/ApplicationSubmission activites if attribute it exists
if isinstance(obj, ApplicationSubmission):
source_filter = Q(source_object_id=obj.id, source_content_type=app_content_type)
if hasattr(obj, "project") and obj.project:
source_filter = source_filter | Q(
source_object_id=obj.project.id, source_content_type=proj_content_type
)
else:
source_filter = Q(
source_object_id=obj.id, source_content_type=proj_content_type
)
if hasattr(obj, "submission") and obj.submission:
source_filter = source_filter | Q(
source_object_id=obj.submission.id, source_content_type=app_content_type
)

queryset = (
Activity.objects.filter(**{related_query: obj})
Activity.objects.filter(source_filter)
.exclude(current=False)
.select_related("user")
.prefetch_related(
Expand Down
24 changes: 6 additions & 18 deletions hypha/apply/activity/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ def partial_comments(request, content_type: str, pk: int):
return render(request, "activity/include/activity_list.html", {})

qs = services.get_related_activities_for_user(obj, request.user)
if hasattr(obj, "project") and obj.project:
qs = qs | services.get_related_activities_for_user(obj.project, request.user)
page = Paginator(qs, per_page=10, orphans=5).page(request.GET.get("page", 1))

ctx = {
Expand Down Expand Up @@ -103,26 +101,16 @@ def get_context_data(self, **kwargs):
# Do not prefetch on the related_object__author as the models
# are not homogeneous and this will fail
user = self.request.user
if isinstance(self.object, Project):
project_obj = self.object
application_obj = self.object.submission
else:
project_obj = None
if hasattr(self.object, "project"):
project_obj = self.object.project
application_obj = self.object

activities = services.get_related_activities_for_user(
application_obj, self.request.user
self.object, self.request.user
)

if project_obj is not None:
project_activities = services.get_related_activities_for_user(
project_obj, self.request.user
)
activities = activities | project_activities

# Comments for both projects and applications exist under the original application
if isinstance(self.object, ApplicationSubmission):
application_obj = self.object
else:
application_obj = self.object.submission

comments_count = services.get_comment_count(application_obj, user)

extra = {"activities": activities, "comments_count": comments_count}
Expand Down

0 comments on commit c59dcdf

Please sign in to comment.