Skip to content
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

fix: ORA response with attached file (for master) #33677

Merged

Conversation

DmytroAlipov
Copy link
Contributor

Description

The details are described in this discussion.

Now everything works correctly:

Знімок екрана 2023-11-08 о 14 10 40

Do not pay attention to the fact that the screenshot was taken from a local instance. It has been tested on the production server, and everything works just as correctly.

@openedx-webhooks
Copy link

Thanks for the pull request, @DmytroAlipov! Please note that it may take us up to several weeks or months to complete a review and merge your PR.

Feel free to add as much of the following information to the ticket as you can:

  • supporting documentation
  • Open edX discussion forum threads
  • timeline information ("this must be merged by XX date", and why that is)
  • partner information ("this is a course on edx.org")
  • any other information that can help Product understand the context for the PR

All technical communication about the code itself will be done via the GitHub pull request interface. As a reminder, our process documentation is here.

Please let us know once your PR is ready for our review and all tests are green.

@@ -196,6 +197,22 @@ class UploadedFileSerializer(serializers.Serializer):
name = serializers.CharField()
size = serializers.IntegerField()

def to_representation(self, instance):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible instead to change the serializer of downloadURL? I'm thinking something along the lines of: downloadUrl = AbsoluteURLField(source="download_url")

Not sure if my proposition is better, or even possible at all.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean something like this?

class AbsoluteURLField(serializers.URLField):
    def to_representation(self, value):
        if value and not value.startswith(("http://", "https://")):
            return f"{settings.LMS_ROOT_URL}{value}"
        return value

class UploadedFileSerializer(serializers.Serializer):
    downloadUrl = AbsoluteURLField(source="download_url")
    ...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could use the existing openedx.core.lib.api.fields.AbsoluteURLField but that requires passing in the view's request as context (see docs), and this isn't compatible with how UploadedFileSerializer is constructed.

So I think it's ok to keep this change as-is.

Copy link
Member

@mariajgrimaldi mariajgrimaldi Jan 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if you considered using SerializerMethodField instead of overriding to_representation. I tried this and it worked:

class UploadedFileSerializer(serializers.Serializer):
    """Serializer for a file uploaded as a part of a response"""

    downloadUrl = serializers.SerializerMethodField(method_name="get_download_url")
    description = serializers.CharField()
    name = serializers.CharField()
    size = serializers.IntegerField()

    def get_download_url(self, obj):
        """
        Get the representation for SerializerMethodField `downloadUrl`
        """
        return urljoin(settings.LMS_ROOT_URL, obj["download_url"])

So, I'm curious about why this is the chosen approach. I want to be sure I'm not missing something. Thanks!

Comment on lines 212 to 213
full_download_url = f"{settings.LMS_ROOT_URL}{download_url}"
data["downloadUrl"] = full_download_url
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
full_download_url = f"{settings.LMS_ROOT_URL}{download_url}"
data["downloadUrl"] = full_download_url
data["downloadUrl"] = f"{settings.LMS_ROOT_URL}{download_url}"
  1. I think we could get rid of the full_download_url variable because it is used only once.

  2. Also, consider using urljoin for the absolute URL construction:

data["downloadUrl"] = urljoin(settings.LMS_ROOT_URL, download_url)

But be careful with expected URL parts (processing different cases with leading/trailing slashes (/))

You can also use urljoin with two absolute URLs, so we could probably get rid of not download_url.startswith(("http://", "https://")) check. But I suspect that it will be less readable. You can check some examples here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried different options and they all work. I selected urljoin(settings.LMS_ROOT_URL, download_url). The nuances of use should not be a problem for us.

@DmytroAlipov DmytroAlipov force-pushed the fix-ora-uploaded-response branch from 0232e82 to 8636958 Compare November 12, 2023 20:18
Copy link
Contributor

@pomegranited pomegranited left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 @DmytroAlipov thank you for submitting this fix! It works perfectly.

I'm sorry it's taken so long to get reviewed. Unfortunately I'm not a core contributor for edx-platform, but I'll ping some people that are to see if we can get this and your backport PRs merged soon.

FYI @pkulkark @navinkarkera @farhaanbukhsh @MaferMazu @mariajgrimaldi

  • I tested this on my devstack by following the instructions to enable frontend-app-ora-grading and view files uploaded to an ORA problem.
  • I read through the code
  • I checked for accessibility issues
  • Includes documentation
  • User-facing strings are extracted for translation

@@ -196,6 +197,22 @@ class UploadedFileSerializer(serializers.Serializer):
name = serializers.CharField()
size = serializers.IntegerField()

def to_representation(self, instance):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could use the existing openedx.core.lib.api.fields.AbsoluteURLField but that requires passing in the view's request as context (see docs), and this isn't compatible with how UploadedFileSerializer is constructed.

So I think it's ok to keep this change as-is.

@pomegranited
Copy link
Contributor

FYI @mphilbrick211

@mariajgrimaldi
Copy link
Member

mariajgrimaldi commented Jan 22, 2024

I'll review this one once I have a chance. Thanks @pomegranited for the ping :). After merging this one, I'll review the backport PRs as part of the BTR. Thanks folks!

@mariajgrimaldi mariajgrimaldi self-requested a review January 22, 2024 13:43
@DmytroAlipov
Copy link
Contributor Author

DmytroAlipov commented Jan 29, 2024

I wonder if you considered using SerializerMethodField instead of overriding to_representation. I tried this and it worked:

class UploadedFileSerializer(serializers.Serializer):
    """Serializer for a file uploaded as a part of a response"""

    downloadUrl = serializers.SerializerMethodField(method_name="get_download_url")
    description = serializers.CharField()
    name = serializers.CharField()
    size = serializers.IntegerField()

    def get_download_url(self, obj):
        """
        Get the representation for SerializerMethodField `downloadUrl`
        """
        return urljoin(settings.LMS_ROOT_URL, obj["download_url"])

So, I'm curious about why this is the chosen approach. I want to be sure I'm not missing something. Thanks!

Hi @mariajgrimaldi!
Both options do not have very significant advantages over each other, neither in the context of memory nor in the context of execution time. My option using a URLField with a source argument may be slightly faster than the SerializerMethodField since it doesn't require an additional get_download_url method to be called.

But in practice, this difference will be so small that it is almost unnoticeable.
Therefore, it seems to me that the choice depends on our inner perfectionism :))

@DmytroAlipov
Copy link
Contributor Author

@mariajgrimaldi
I will redo this shortly as you suggested.

@farhaanbukhsh
Copy link
Member

Sorry @pomegranited I was off when you pinged. @mariajgrimaldi Thank you for reviewing it and let me know if I can help you with the review. :)

@mariajgrimaldi
Copy link
Member

mariajgrimaldi commented Feb 1, 2024

@DmytroAlipov: thanks for the detailed response! The implementation looks good to me; don't worry about changing it. Could you check the failing tests?

@DmytroAlipov
Copy link
Contributor Author

@mariajgrimaldi I'm currently busy with other tasks, but I plan to deal with this PR early next week.

@mariajgrimaldi
Copy link
Member

@DmytroAlipov: don't worry. Please let me know when it's ready so we can merge :)

@DmytroAlipov DmytroAlipov force-pushed the fix-ora-uploaded-response branch from 0839d45 to 70fa522 Compare February 5, 2024 18:17
After clicking the "View all responses" button in the
ORA Staff Grading App, an error page appears for ORAs
with file uploads.
@DmytroAlipov DmytroAlipov force-pushed the fix-ora-uploaded-response branch from 70fa522 to bb68e09 Compare February 5, 2024 18:28
@DmytroAlipov
Copy link
Contributor Author

DmytroAlipov commented Feb 5, 2024

Hi @mariajgrimaldi!
Finally, I got to this PR🤓 I did as you suggested. Everything worked, but I had to slightly tweak a few old tests. When this PR is merged, I will update all backports and ping you!

@mariajgrimaldi mariajgrimaldi merged commit d65841a into openedx:master Feb 6, 2024
46 checks passed
@openedx-webhooks
Copy link

@DmytroAlipov 🎉 Your pull request was merged! Please take a moment to answer a two question survey so we can improve your experience in the future.

@mariajgrimaldi
Copy link
Member

Thank you so much @DmytroAlipov!

@edx-pipeline-bot
Copy link
Contributor

2U Release Notice: This PR has been deployed to the edX staging environment in preparation for a release to production.

@edx-pipeline-bot
Copy link
Contributor

2U Release Notice: This PR has been deployed to the edX production environment.

@DmytroAlipov DmytroAlipov deleted the fix-ora-uploaded-response branch February 6, 2024 20:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
open-source-contribution PR author is not from Axim or 2U
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

8 participants