-
Notifications
You must be signed in to change notification settings - Fork 213
CommunityLibrarySubmission viewset #5167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CommunityLibrarySubmission viewset #5167
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @Jakoma02! This is looking great! I've left just a couple minor comments, the more important one is the potential 500 error that we can have the update
method of the serializer. Apart from that everything looks great!
@@ -2616,6 +2616,10 @@ class Meta: | |||
name="unique_channel_with_channel_version", | |||
), | |||
] | |||
indexes = [ | |||
# Useful for cursor pagination |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch!
@@ -2655,6 +2659,7 @@ def has_changes(self): | |||
|
|||
class Meta: | |||
indexes = [ | |||
# Useful for cursor pagination |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like we introduced and unwanted change here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right, removed in 6ab6775.
from contentcuration.tests.base import StudioAPITestCase | ||
|
||
|
||
def reverse_with_query( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Loved it!
cursor = more["cursor"] | ||
|
||
response = self.client.get( | ||
reverse_with_query( | ||
"community-library-submission-list", | ||
query={"max_results": 1, "cursor": cursor}, | ||
) | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not super important, but just noting that usually the returned more
object can be used as the new query param, as it already returns the max_results
, and all filters we have applied to the previous request (e.g. if we are filtering by channel, it already returns the channel filter too). So we usually just do something like:
response = self.client.get(
reverse_with_query(
"community-library-submission-list",
query=more
)
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is a neat feature :) Changed in 6ab6775.
return instance | ||
|
||
def update(self, instance, validated_data): | ||
if instance.channel.id != validated_data["channel"].id: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will throw a 500 error for patch operations that don't send the channel
in the request.
I know this is different from what is written in the issue, but another Idea would be to just silently remove the channel from the validated_data
if it exists, and just prevent channel changes updates altogether. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right; thanks for catching this! I forgot that partial updates are also possible. This is fixed in ba77607.
I kept the provided channel in validated_data
so that a more descriptive error is given in case of failure, but I can silently remove it if you like that approach better.
countries = validated_data.pop("countries", []) | ||
instance = super().create(validated_data) | ||
|
||
for country in countries: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(nitpick) I think here we can just do instance.countries.set(countries)
too right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, I have only discovered the set
method later and forgot to use it for create
as well. Changed in 6ab6775.
def get_queryset(self): | ||
base_queryset = CommunityLibrarySubmission.objects.all() | ||
user = self.request.user | ||
queryset = CommunityLibrarySubmission.filter_view_queryset( | ||
base_queryset, user | ||
).order_by("-date_created") | ||
|
||
return queryset | ||
|
||
def get_edit_queryset(self): | ||
base_queryset = CommunityLibrarySubmission.objects.all() | ||
user = self.request.user | ||
queryset = CommunityLibrarySubmission.filter_edit_queryset(base_queryset, user) | ||
|
||
return queryset |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, we don't need to override these methods, we already have them thanks to our BaseValuesViewset from which the ReadOnlyValuesViewset inherits: https://github.com/jakoma02/studio/blob/9e7c0f7fff6f749bc04b8acb8635f848eaa5823d/contentcuration/contentcuration/viewsets/base.py#L581-L595.
So we can define our base queryset directly as a class property. (e.g. https://github.com/jakoma02/studio/blob/9e7c0f7fff6f749bc04b8acb8635f848eaa5823d/contentcuration/contentcuration/viewsets/assessmentitem.py#L247) and we can apply the order_by there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have completely missed that! Thanks, changed in 6ab6775.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great! One more excellent PR! Thanks @Jakoma02!
4a34ccc
into
learningequality:community-channels
Summary
The PR implements a viewset for managing community library submissions.
Detailed changes:
CommunityLibrarySubmissionViewset
date_created
field to support pagination betterCommunityLibrarySubmissionViewset
The functionality was verified by checking that the new tests pass and by briefly manually testing using the browsable API.
References
Resolves #5157
Reviewer guidance
Nothing