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

Add check to call render on the response if it has a render method #11762

Merged
merged 3 commits into from
Feb 22, 2024

Conversation

KshitijThareja
Copy link
Contributor

Summary

This PR fixes the error : 'HttpResponseNotAllowed' object has no attribute 'render' when render method is called on response without checking if render attribute exists or is callable.

References

Closes #10506


Testing checklist

  • Contributor has fully tested the PR manually
  • If there are any front-end changes, before/after screenshots are included
  • Critical user journeys are covered by Gherkin stories
  • Critical and brittle code paths are covered by unit tests

PR process

  • PR has the correct target branch and milestone
  • PR has 'needs review' or 'work-in-progress' label
  • If PR is ready for review, a reviewer has been added. (Don't use 'Assignees')
  • If this is an important user-facing change, PR or related issue has a 'changelog' label
  • If this includes an internal dependency change, a link to the diff is provided

Reviewer checklist

  • Automated test coverage is satisfactory
  • PR is fully functional
  • PR has been tested for accessibility regressions
  • External dependency files were updated if necessary (yarn and pip)
  • Documentation is updated
  • Contributor is in AUTHORS.md

@github-actions github-actions bot added DEV: backend Python, databases, networking, filesystem... SIZE: very small labels Jan 22, 2024
@MisRob MisRob added the TODO: needs review Waiting for review label Jan 23, 2024
@@ -339,7 +339,8 @@ def cache_no_user_data(view_func):
_response = local()

def render_and_cache(response, cache_key):
response.render()
if hasattr(response, "render") and callable(response.render):
response.render()
etag = hashlib.md5(
kolibri_version.encode("utf-8") + str(response.content).encode("utf-8")
Copy link
Member

Choose a reason for hiding this comment

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

For response objects without a render function, is content guaranteed to be defined? Or will it just be null?

I wonder if it might be better to not cache an etag in this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For such a case, we can check if content is defined for response object or not, after checking for the render method. This way, we'll only cache the etag if both the attributes are defined for a particular response object.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can check if response.content is defined and not null and then cache the etag, else we can return None for render_and_cache function. Should I proceed with these changes?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, I think that seems right.

Copy link
Member

Choose a reason for hiding this comment

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

I did a double check, and Django accepts the etag generating function returning None in the case that an etag does not exist for the resource, which seems right: https://docs.djangoproject.com/en/1.11/topics/conditional-view-processing/#the-condition-decorator

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the confirmation :)
I'll move forward with the changes in that case. Will make a commit in some time 👍🏻

@rtibbles rtibbles self-assigned this Jan 24, 2024
Copy link
Member

@rtibbles rtibbles left a comment

Choose a reason for hiding this comment

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

Sorry, I had forgotten to follow up here. This looks like the right set of changes. We are going to hold off merging for now, as the 0.16.0 release is imminent and we want to avoid merging anything other than critical bug fixes.

@KshitijThareja
Copy link
Contributor Author

Sure @rtibbles. Thanks for reviewing this PR
Will be glad to see the changes included in the next patch of 0.16.0 :)

@bjester bjester merged commit 9dbe675 into learningequality:release-v0.16.x Feb 22, 2024
34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DEV: backend Python, databases, networking, filesystem... SIZE: small SIZE: very small TODO: needs review Waiting for review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Only call render on the response if it has a render method
4 participants