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

Fixed #24139 -- Evaluate reason_phrase on demand based on status_code. #3903

Closed
wants to merge 1 commit into from
Closed

Fixed #24139 -- Evaluate reason_phrase on demand based on status_code. #3903

wants to merge 1 commit into from

Conversation

jdufresne
Copy link
Member

No description provided.

@shaib
Copy link
Member

shaib commented Jan 12, 2015

buildbot, test this please.

@@ -141,6 +137,16 @@ def charset(self):
def charset(self, value):
self._charset = value

@property
def reason_phrase(self):
if self._reason_phrase is not None:
Copy link
Member

Choose a reason for hiding this comment

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

You don't set self._reason_phrase anywhere in here if the lookup fails. I don't think that's intended?

Copy link
Contributor

Choose a reason for hiding this comment

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

It makes sense to me that you would leave it unset unless self.reason_phase explicitly sets it.

Copy link
Member

Choose a reason for hiding this comment

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

Can we have that as a comment then, please.

@jdufresne
Copy link
Member Author

Can we have that as a comment then, please.

Added comment as suggested. Let me know if you had something else in mind.

@jdufresne
Copy link
Member Author

Rebased after the merge of #3902 and resolved conflicts.


Unless :attr:`reason_phrase` is explicitly set, modifying the value of
``status_code`` outside the constructor will also modify the value of
`reason_phrase`.
Copy link
Contributor

Choose a reason for hiding this comment

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

:attr:reason_phrase or reason_phrase

@jdufresne
Copy link
Member Author

Perhaps we can use a similar approach for status_code and status attributes, too.

Do you mind expanding on what you're requesting?

Are you saying status_code should not be a class attribute on HttpResponseBase? The other classes in this file rely on this behavior: HttpResponseRedirect, HttpResponseNotFound, etc.

Or are you saying the __init__() kwarg status should be renamed to status_code?

Sorry, but I'm not sure precisely what "status attributes" is referring to.

I'm happy to cleanup whatever needs it, I'm just not sure exactly what you're asking me to change.

I suppose one potential fix would be to fix the Content-Type header to evaluate based on the current value of self._charset. Is this along the lines of what you mean?

Version in master (if charset changes outside the constructor, the Content-Type header is wrong):

    def __init__(self, content_type=None, status=None, reason=None, charset=None):
        ...
        self._charset = charset
        if content_type is None:
            content_type = '%s; charset=%s' % (settings.DEFAULT_CONTENT_TYPE,
                                               self.charset)

@berkerpeksag
Copy link
Contributor

Sorry for being unclear.

Or are you saying the init() kwarg status should be renamed to status_code?

That's what I meant to say. However, adding a new status_code keyword argument would be better for backward compatibility.

@jdufresne
Copy link
Member Author

That's what I meant to say. However, adding a new status_code keyword argument would be better for backward compatibility.

Thanks. Will do.

@jdufresne
Copy link
Member Author

I believe I have implemented what you had in mind. Let me know how you feel about the results.

self.assertEqual(response['content-type'], 'application/json')
self.assertEqual(response.status_code, 504)

def test_args(self):
response = TemplateResponse(self.factory.get('/'), '', {},
'application/json', None, None, None, 504)
Copy link
Member

Choose a reason for hiding this comment

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

This raises an error:

Traceback (most recent call last):
  File "/home/jenkins/workspace/pull-requests-trusty/database/mysql/label/trusty-pr/python/python2.7/tests/template_tests/test_response.py", line 265, in test_args
    'application/json', None, None, None, 504)
  File "/home/jenkins/workspace/pull-requests-trusty/database/mysql/label/trusty-pr/python/python2.7/django/template/response.py", line 201, in __init__
    RemovedInDjango20Warning, stacklevel=2)
RemovedInDjango20Warning: The current_app argument of TemplateResponse is deprecated. Set the current_app attribute of its request instead.

@timgraham
Copy link
Member

This change seems to have gotten a bit unwieldy as far as the scope of the original ticket goes. Is there really a need to deprecate status in favor of status_code? I'd try to keep solving this ticket more minimal unless all these changes are really necessary to do together.

@jdufresne
Copy link
Member Author

This change seems to have gotten a bit unwieldy as far as the scope of the original ticket goes. Is there really a need to deprecate status in favor of status_code? I'd try to keep solving this ticket more minimal unless all these changes are really necessary to do together.

I think I agree. In my opinion, there is no pressing need to deprecate status in favor of status_code (although I agree it is a nice to have for consistency). Shall I discard the 2nd commit? If still preferred I can move the 2nd commit to a different PR.

I originally went down this path at the request of @berkerpeksag. As a core contributor and Django Fellow I interpret his review as authoritative.

If there is disagreement about the 2nd commit, please let me know how to proceed.

@jdufresne
Copy link
Member Author

OK, I've dropped the rename kwargs commit. If that is still desirable I can move it to another ticket. This will change will simply cover the goals of the original ticket.

@timgraham
Copy link
Member

merged in d861f95, thanks!

@timgraham timgraham closed this Mar 13, 2015
@jdufresne jdufresne deleted the lazy-reason-phrase branch March 13, 2016 23:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
6 participants