Add new header mixin #204

Merged
merged 5 commits into from Feb 1, 2017

Projects

None yet

3 participants

@fladi
Contributor
fladi commented Dec 18, 2015

This allows to set headers either by defining them in a view attribute or by overriding a view method. Come with tests and documentation. Maybe you want to include this in braces.

@kennethlove kennethlove and 1 other commented on an outdated diff Feb 17, 2016
braces/views/_other.py
+ ``headers`` attribute or by overriding the ``get_headers()`` method.
+ """
+ headers = {}
+
+ def get_headers(self, request):
+ return self.headers
+
+ def dispatch(self, request, *args, **kwargs):
+ """
+ Override this method to customize the way additional headers are
+ retrieved. It is mandatory that the returned value supports the
+ ``.items()`` method.
+ """
+ response = super(HeaderMixin, self).dispatch(request, *args, **kwargs)
+ for key, value in self.get_headers(request).items():
+ response[key] = value
@kennethlove
kennethlove Feb 17, 2016 Member

Seems like this would be better handled with HttpResponse's .setdefault() method so that you can ensure you don't overwrite any existing headers.

@fladi
fladi Jun 1, 2016 Contributor

setdefault is only available on Django 1.8 and later. I'll add a check to prevent existing headers from being overwritten.

@auvipy

please fix merge error

@kennethlove
Member

So, year and some later, I'd like to include this in the next release of Braces. Can you fix the conflicts?

@fladi
Contributor
fladi commented Jan 23, 2017

Rebased on current master. Thanks for considering inclusion.

@kennethlove kennethlove merged commit fff300d into brack3t:master Feb 1, 2017

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment