Opened 9 years ago

Closed 6 years ago

#23977 closed New feature (fixed)

Add setdefault() to HttpResponse

Reported by: Robert Coup Owned by: Sergey
Component: HTTP handling Version: dev
Severity: Normal Keywords:
Cc: Дилян Палаузов Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

HttpResponse acts like a dictionary with respect to headers. It's a common middleware pattern to set headers unless they've been explicitly set by the view. eg.

class MyMiddleware(object):
  def process_response(self, request, response):
    if not 'X-Test' in response:
      response['X-Test'] = 12345
    return response

Having a setdefault() implementation would simplify this (not much in the trivial one-header case, but for example CORS middleware where you're setting a number of headers)

class MyMiddleware(object):
  def process_response(self, request, response):
    response.setdefault('X-Test', 12345)
    return response

Change History (6)

comment:1 by Tim Graham, 9 years ago

Triage Stage: UnreviewedAccepted

comment:2 by Sergey, 9 years ago

Owner: changed from nobody to Sergey
Status: newassigned

comment:3 by Tim Graham, 9 years ago

Has patch: set
Triage Stage: AcceptedReady for checkin

comment:4 by Tim Graham <timograham@…>, 9 years ago

Resolution: fixed
Status: assignedclosed

In 059c9ab24c41e1460fd8b7af65ea8d5f80f1aa82:

Fixed #23977 -- Added setdefault() method to HttpResponse

comment:5 by Дилян Палаузов, 6 years ago

Cc: Дилян Палаузов added
Resolution: fixed
Status: closednew

HttpResponseBase.setdefault should return a value in order to be consistent with dict.setdefault and what users expect from a function called setdefault on an objects, that behaves like dict.

Version 0, edited 6 years ago by Дилян Палаузов (next)

comment:6 by Tim Graham, 6 years ago

Resolution: fixed
Status: newclosed

Please open a new ticket rather then reopening an existing one that's been fixed for three years. You can provide patches as attachments rather than as comments.

Note: See TracTickets for help on using tickets.
Back to Top