-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Load the activities tab of submission and project on demand (#3330)
The activities tab can have a lot of activities as it’s unpaginated and also has an N+1 query to fetch the related objects. This PR loads the data only after the tab is clicked. I makes use of the htmx and alpine to observe the window-hash change. Notes: - The communication tab with it's markdown editor, requires some custom initialization logic, which breaks with the htmx loaded content. So it' not included in this PR. - On a submission with 5 activities the number of SQL queries reduced from 168 to 148. Related #3328
- Loading branch information
Showing
9 changed files
with
124 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from .models import Activity | ||
|
||
|
||
def get_related_actions_for_user(obj, user): | ||
"""Return Activity objects related to an object, esp. useful with | ||
ApplicationSubmission and Project. | ||
Args: | ||
obj: instance of a model class | ||
user: user who these actions are visible to. | ||
Returns: | ||
`Activity` queryset | ||
""" | ||
related_query = type(obj).activities.rel.related_query_name | ||
|
||
return ( | ||
Activity.actions.filter(**{related_query: obj}) | ||
.select_related('user') | ||
.prefetch_related( | ||
'related_object', | ||
) | ||
.visible_to(user) | ||
) | ||
|
||
|
||
def get_related_comments_for_user(obj, user): | ||
"""Return comments/communications related to an object, esp. useful with | ||
ApplicationSubmission and Project. | ||
Args: | ||
obj: instance of a model class | ||
user: user who these actions are visible to. | ||
Returns: | ||
`Activity` queryset | ||
""" | ||
related_query = type(obj).activities.rel.related_query_name | ||
|
||
return ( | ||
Activity.comments.filter(**{related_query: obj}) | ||
.select_related('user') | ||
.prefetch_related( | ||
'related_object', | ||
) | ||
.visible_to(user) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from django.contrib.auth.decorators import login_required | ||
from django.shortcuts import get_object_or_404, render | ||
from django.views.decorators.http import require_GET | ||
|
||
from hypha.apply.activity.services import ( | ||
get_related_actions_for_user, | ||
) | ||
from hypha.apply.funds.permissions import has_permission | ||
|
||
from .models import ApplicationSubmission | ||
|
||
|
||
@login_required | ||
@require_GET | ||
def partial_submission_activities(request, pk): | ||
submission = get_object_or_404(ApplicationSubmission, pk=pk) | ||
has_permission( | ||
'submission_view', request.user, object=submission, raise_exception=True | ||
) | ||
ctx = {'actions': get_related_actions_for_user(submission, request.user)} | ||
return render(request, 'activity/include/action_list.html', ctx) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from django.contrib.auth.decorators import login_required | ||
from django.shortcuts import get_object_or_404, render | ||
from django.views.decorators.http import require_GET | ||
|
||
from hypha.apply.activity.services import ( | ||
get_related_actions_for_user, | ||
) | ||
|
||
from ..models.project import Project | ||
|
||
|
||
@login_required | ||
@require_GET | ||
def partial_project_activities(request, pk): | ||
project = get_object_or_404(Project, pk=pk) | ||
ctx = { | ||
'actions': get_related_actions_for_user(project, request.user) | ||
} | ||
return render(request, 'activity/include/action_list.html', ctx) |