[Django] #35631: Include quality value when parsing Accept header

21 views
Skip to first unread message

Django

unread,
Jul 25, 2024, 10:54:01 AM7/25/24
to django-...@googlegroups.com
#35631: Include quality value when parsing Accept header
-----------------------------------------+-----------------------------
Reporter: Jake Howard | Owner: Jake Howard
Type: New feature | Status: assigned
Component: HTTP handling | Version: 5.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+-----------------------------
Requests have an `accepts` method for identifying the mimetype the user
can receive. However, when multiple formats are provided, a
[https://developer.mozilla.org/en-US/docs/Glossary/Quality_values "quality
value"] can be added to show the preferences on format. Handling this
value currently needs to be done by hand.

I propose:

1. Documenting the existence of `Request.accepted_types`
2. Extract the quality value, and store it as `MediaType.quality`
3. Sort `Request.accepted_types` by quality value

It would be nice to have an API to answer "I can return these mime types,
which would you prefer?", which I guess could be handled manually for now.
--
Ticket URL: <https://code.djangoproject.com/ticket/35631>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jul 26, 2024, 3:36:26 AM7/26/24
to django-...@googlegroups.com
#35631: Include quality value when parsing Accept header
-------------------------------+---------------------------------------
Reporter: Jake Howard | Owner: Jake Howard
Type: New feature | Status: assigned
Component: HTTP handling | Version: 5.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+---------------------------------------
Changes (by Carlton Gibson):

* stage: Unreviewed => Accepted

Comment:

I think step 3 should read "Sort Request.accepted_types by quality value
and specificity", e.g. `text/*;q=0.8,text/html;q=0.8` should be sorted
`text/html` first.

This is probably a sensible proposal. I suspect most people are not
sending the quality value, so `1` is assumed and it has no effect.
Nonetheless why not allow it?

We'd need a new helper API on HttpRequest taking a set of mime types to
match against, since the specificity question (`text/*`) makes it slightly
more complex than just returning the first match in accepted_types from
the set.

DRF has implementations of this logic that can be used for reference:

* [https://github.com/encode/django-rest-
framework/blob/8e304e1adbb0f99f91a15ed3abd379104bba3b89/rest_framework/negotiation.py#L53-L76
rest_framework/negotiation.py#L53-L76]
* [https://github.com/encode/django-rest-
framework/blob/8e304e1adbb0f99f91a15ed3abd379104bba3b89/rest_framework/utils/mediatypes.py#L27-L81
rest_framework/utils/mediatypes.py#L27-L81]
--
Ticket URL: <https://code.djangoproject.com/ticket/35631#comment:1>

Django

unread,
Jul 26, 2024, 9:46:46 AM7/26/24
to django-...@googlegroups.com
#35631: Include quality value when parsing Accept header
-------------------------------+---------------------------------------
Reporter: Jake Howard | Owner: Jake Howard
Type: New feature | Status: assigned
Component: HTTP handling | Version: 5.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+---------------------------------------
Changes (by Jake Howard):

* has_patch: 0 => 1

Comment:

I've implemented the core of this feature here:
[https://github.com/django/django/pull/35631 PR], sans docs.

I'd appreciate a review, particularly around what API surface to expose.
--
Ticket URL: <https://code.djangoproject.com/ticket/35631#comment:2>

Django

unread,
Aug 7, 2024, 5:49:59 AM8/7/24
to django-...@googlegroups.com
#35631: Include quality value when parsing Accept header
-------------------------------+---------------------------------------
Reporter: Jake Howard | Owner: Jake Howard
Type: New feature | Status: assigned
Component: HTTP handling | Version: 5.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+---------------------------------------
Comment (by Jake Howard):

[https://github.com/django/django/pull/18415 PR] now contains docs -
review away!
--
Ticket URL: <https://code.djangoproject.com/ticket/35631#comment:3>

Django

unread,
Aug 13, 2024, 5:04:13 AM8/13/24
to django-...@googlegroups.com
#35631: Include quality value when parsing Accept header
-------------------------------+---------------------------------------
Reporter: Jake Howard | Owner: Jake Howard
Type: New feature | Status: assigned
Component: HTTP handling | Version: 5.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------+---------------------------------------
Changes (by Sarah Boyce):

* needs_better_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/35631#comment:4>

Django

unread,
Aug 15, 2024, 10:46:34 AM8/15/24
to django-...@googlegroups.com
#35631: Include quality value when parsing Accept header
-------------------------------+---------------------------------------
Reporter: Jake Howard | Owner: Jake Howard
Type: New feature | Status: assigned
Component: HTTP handling | Version: 5.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+---------------------------------------
Changes (by Jake Howard):

* needs_better_patch: 1 => 0

--
Ticket URL: <https://code.djangoproject.com/ticket/35631#comment:5>

Django

unread,
Aug 19, 2024, 4:25:27 AM8/19/24
to django-...@googlegroups.com
#35631: Include quality value when parsing Accept header
-------------------------------+---------------------------------------
Reporter: Jake Howard | Owner: Jake Howard
Type: New feature | Status: assigned
Component: HTTP handling | Version: 5.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------+---------------------------------------
Changes (by Sarah Boyce):

* needs_better_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/35631#comment:6>

Django

unread,
Aug 20, 2024, 11:49:00 AM8/20/24
to django-...@googlegroups.com
#35631: Include quality value when parsing Accept header
-------------------------------+---------------------------------------
Reporter: Jake Howard | Owner: Jake Howard
Type: New feature | Status: assigned
Component: HTTP handling | Version: 5.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+---------------------------------------
Changes (by Jake Howard):

* needs_better_patch: 1 => 0

--
Ticket URL: <https://code.djangoproject.com/ticket/35631#comment:7>

Django

unread,
Sep 2, 2024, 7:37:22 AM9/2/24
to django-...@googlegroups.com
#35631: Include quality value when parsing Accept header
-------------------------------+---------------------------------------
Reporter: Jake Howard | Owner: Jake Howard
Type: New feature | Status: assigned
Component: HTTP handling | Version: 5.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------+---------------------------------------
Changes (by Sarah Boyce):

* needs_better_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/35631#comment:8>

Django

unread,
Sep 3, 2024, 10:53:20 AM9/3/24
to django-...@googlegroups.com
#35631: Include quality value when parsing Accept header
-------------------------------+---------------------------------------
Reporter: Jake Howard | Owner: Jake Howard
Type: New feature | Status: assigned
Component: HTTP handling | Version: 5.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+---------------------------------------
Changes (by Jake Howard):

* needs_better_patch: 1 => 0

--
Ticket URL: <https://code.djangoproject.com/ticket/35631#comment:9>

Django

unread,
Sep 4, 2024, 2:52:11 AM9/4/24
to django-...@googlegroups.com
#35631: Include quality value when parsing Accept header
-------------------------------+---------------------------------------
Reporter: Jake Howard | Owner: Jake Howard
Type: New feature | Status: assigned
Component: HTTP handling | Version: 5.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------+---------------------------------------
Changes (by Sarah Boyce):

* needs_better_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/35631#comment:10>

Django

unread,
Sep 9, 2024, 4:12:49 AM9/9/24
to django-...@googlegroups.com
#35631: Include quality value when parsing Accept header
-------------------------------------+-------------------------------------
Reporter: Jake Howard | Owner: Jake
| Howard
Type: New feature | Status: assigned
Component: HTTP handling | Version: 5.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* needs_better_patch: 1 => 0
* stage: Accepted => Ready for checkin

--
Ticket URL: <https://code.djangoproject.com/ticket/35631#comment:11>

Django

unread,
Sep 9, 2024, 6:02:27 AM9/9/24
to django-...@googlegroups.com
#35631: Include quality value when parsing Accept header
-------------------------------------+-------------------------------------
Reporter: Jake Howard | Owner: Jake
| Howard
Type: New feature | Status: closed
Component: HTTP handling | Version: 5.0
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce <42296566+sarahboyce@…>):

* resolution: => fixed
* status: assigned => closed

Comment:

In [changeset:"e161bd4657177f0e723a14a6e414884363b31a5d" e161bd46]:
{{{#!CommitTicketReference repository=""
revision="e161bd4657177f0e723a14a6e414884363b31a5d"
Fixed #35631 -- Added HttpRequest.get_preferred_type().
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35631#comment:12>
Reply all
Reply to author
Forward
0 new messages