PKS~G.Idjango_accept_header/header.py# -*- coding: utf-8 -*- # Copyright (c) 2015, Rhys Elsmore # # Permission to use, copy, modify, and/or distribute this software for any # purpose with or without fee is hereby granted, provided that the above # copyright notice and this permission notice appear in all copies. # # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. from __future__ import absolute_import import re from collections import OrderedDict as kv class MediaType(object): def __init__(self, media_type, q=None, params=None): (self._mediatype, self._subtype) = media_type.split('/') self._quality = q or 1.0 self._params = params or {} def __repr__(self): return ''.format(self=self) def __str__(self): if len(self.params) > 0: p = '; '.join(['{key}={value}'.format(key=k, value=v) for k, v in self.params.items()]) return '%s; q=%s; %s' % (self.mimetype, self.q, p) else: return '%s; q=%s' % (self.mimetype, self.q) def __getitem__(self, key): return self.params.get(key) def __eq__(self, other): if isinstance(other, MediaType): return self.mimetype == other.mimetype return self.mimetype == other def __ne__(self, other): return not (self == other) def __lt__(self, other): return self._compare(other) == -1 def __gt__(self, other): return self._compare(other) > -1 def matches(self, other_type): other = MediaType(other_type) if self.all_types: return True if self.mediatype == other.mediatype and self.all_subtypes: return True if self.mediatype == other.mediatype and self.subtype == other.subtype: return True return False @property def mimetype(self): return '{mediatype}/{subtype}'.format( mediatype=self._mediatype, subtype=self._subtype ) @property def mediatype(self): return self._mediatype @property def subtype(self): return self._subtype @property def quality(self): return self._quality @property def q(self): return self.quality @property def params(self): return self._params @property def all_types(self): return self.mediatype == '*' and self.subtype == '*' @property def all_subtypes(self): return self.subtype == '*' def _compare(self, other): if self.quality > other.quality: return -1 elif self.quality < other.quality: return 1 elif (not self.all_subtypes and other.all_subtypes) or\ (not self.all_types and other.all_types): return -1 elif (self.all_subtypes and not other.all_subtypes) or\ (self.all_types and not other.all_types): return 1 elif len(self.params) > len(other.params): return -1 elif len(self.params) == len(other.params): return 0 else: return 1 def parse(value): results = [] if not value: return results value = re.sub(r'^Accept\:\s', '', value) for media_range in [m.strip() for m in value.split(',') if m]: parts = media_range.split(";") media_type = parts.pop(0).strip() params = [] q = 1.0 for part in parts: (key, value) = part.lstrip().split("=", 1) key = key.strip() value = value.strip().strip('"\'') if key == "q": try: q = float(value) except ValueError: pass else: params.append((key, value)) results.append( MediaType(media_type, q, kv(params)) ) results.sort() return results PK0V~G&xtt"django_accept_header/middleware.py# -*- coding: utf-8 -*- # Copyright (c) 2015, Michael Fladischer # # Permission to use, copy, modify, and/or distribute this software for any # purpose with or without fee is hereby granted, provided that the above # copyright notice and this permission notice appear in all copies. # # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. from __future__ import absolute_import from .header import parse class AcceptMiddleware(object): def process_request(self, request): acc = parse(request.META.get('HTTP_ACCEPT')) setattr(request, 'accepted_types', acc) request.accepts = lambda mt: any([ma.matches(mt) for ma in acc]) return None PKgIBHw django_accept_header/__init__.py__version__ = "0.3.2" PKlIBHy9$ $ 4django_accept_header-0.3.2.dist-info/DESCRIPTION.rst===================== Django Accept Headers ===================== .. list-table:: :stub-columns: 1 * - docs - |docs| * - tests - | |travis| |requires| | |codecov| | * - package - |version| |downloads| |wheel| |supported-versions| |supported-implementations| .. |docs| image:: https://readthedocs.org/projects/django-accept-header/badge/?style=flat :target: https://readthedocs.org/projects/django-accept-header :alt: Documentation Status .. |travis| image:: https://travis-ci.org/fladi/django-accept-header.svg?branch=master :alt: Travis-CI Build Status :target: https://travis-ci.org/fladi/django-accept-header .. |requires| image:: https://requires.io/github/fladi/django-accept-header/requirements.svg?branch=master :alt: Requirements Status :target: https://requires.io/github/fladi/django-accept-header/requirements/?branch=master .. |codecov| image:: https://codecov.io/github/fladi/django-accept-header/coverage.svg?branch=master :alt: Coverage Status :target: https://codecov.io/github/fladi/django-accept-header .. |version| image:: https://img.shields.io/pypi/v/django-accept-header.svg?style=flat :alt: PyPI Package latest release :target: https://pypi.python.org/pypi/django-accept-header .. |downloads| image:: https://img.shields.io/pypi/dm/django-accept-header.svg?style=flat :alt: PyPI Package monthly downloads :target: https://pypi.python.org/pypi/django-accept-header .. |wheel| image:: https://img.shields.io/pypi/wheel/django-accept-header.svg?style=flat :alt: PyPI Wheel :target: https://pypi.python.org/pypi/django-accept-header .. |supported-versions| image:: https://img.shields.io/pypi/pyversions/django-accept-header.svg?style=flat :alt: Supported versions :target: https://pypi.python.org/pypi/django-accept-header .. |supported-implementations| image:: https://img.shields.io/pypi/implementation/django-accept-header.svg?style=flat :alt: Supported implementations :target: https://pypi.python.org/pypi/django-accept-header A Django middleware that inspects the HTTP Acept headers sent by browsers. It adds a new method to each `request` instance called `accepts(str)` which can be used to determine if a certain mimetype is accepted by the user agent that issued the request. Installation ============ :: pip install django-accept-header Usage ===== First add the middleware to your `settings.py` file:: MIDDLEWARE_CLASSES = ( # ... 'django_accept_header.middleware.AcceptMiddleware', ) To check if the `text/plain` mimetype is accepted by the user agent:: def some_view(request): if request.accepts('text/plain'): # do something The ordered list of accepted mimetypes can also be used:: def some_view(request): for media_type in request.accepted_types: # do something See the full documentation for how to use the media types please see the full documentation. Documentation ============= https://django-accept-header.readthedocs.org/ Development =========== To run the all tests run:: tox 0.3.0 / 2016-02-02 ================== * Change package name to django-accept-header. * Make python3.5 the default version used for tests. 0.1.0 / 2015-11-26 ================== * First release on PyPI. PKlIBHRS  2django_accept_header-0.3.2.dist-info/metadata.json{"classifiers": ["Development Status :: 4 - Beta", "Framework :: Django", "Framework :: Django :: 1.8", "Intended Audience :: Developers", "License :: OSI Approved :: ISC License (ISCL)", "Programming Language :: Python", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: Implementation :: CPython", "Programming Language :: Python :: Implementation :: PyPy", "Topic :: Utilities"], "extensions": {"python.details": {"contacts": [{"email": "michael@fladi.at", "name": "Michael Fladischer", "role": "author"}], "document_names": {"description": "DESCRIPTION.rst"}, "project_urls": {"Home": "https://github.com/fladi/django-accept-header"}}}, "extras": ["rst", "testing"], "generator": "bdist_wheel (0.26.0)", "keywords": ["middleware", "header", "accept", "mimetype"], "license": "BSD", "metadata_version": "2.0", "name": "django-accept-header", "run_requires": [{"requires": ["Django"]}, {"extra": "rst", "requires": ["docutils (>=0.11)"]}, {"extra": "testing", "requires": ["pytest"]}], "summary": "A Django middleware that inspects the HTTP Accept headers sent by browsers.", "test_requires": [{"requires": ["pytest"]}], "version": "0.3.2"}PKaG~ //-django_accept_header-0.3.2.dist-info/pbr.json{"git_version": "2145eeb", "is_release": false}PKlIBH,|2django_accept_header-0.3.2.dist-info/top_level.txtdjango_accept_header PKlIBHndnn*django_accept_header-0.3.2.dist-info/WHEELWheel-Version: 1.0 Generator: bdist_wheel (0.26.0) Root-Is-Purelib: true Tag: py2-none-any Tag: py3-none-any PKlIBHr8-django_accept_header-0.3.2.dist-info/METADATAMetadata-Version: 2.0 Name: django-accept-header Version: 0.3.2 Summary: A Django middleware that inspects the HTTP Accept headers sent by browsers. Home-page: https://github.com/fladi/django-accept-header Author: Michael Fladischer Author-email: michael@fladi.at License: BSD Keywords: middleware,header,accept,mimetype Platform: UNKNOWN Classifier: Development Status :: 4 - Beta Classifier: Framework :: Django Classifier: Framework :: Django :: 1.8 Classifier: Intended Audience :: Developers Classifier: License :: OSI Approved :: ISC License (ISCL) Classifier: Programming Language :: Python Classifier: Programming Language :: Python :: 2.7 Classifier: Programming Language :: Python :: 3 Classifier: Programming Language :: Python :: 3.4 Classifier: Programming Language :: Python :: 3.5 Classifier: Programming Language :: Python :: Implementation :: CPython Classifier: Programming Language :: Python :: Implementation :: PyPy Classifier: Topic :: Utilities Requires-Dist: Django Provides-Extra: rst Requires-Dist: docutils (>=0.11); extra == 'rst' Provides-Extra: testing Requires-Dist: pytest; extra == 'testing' ===================== Django Accept Headers ===================== .. list-table:: :stub-columns: 1 * - docs - |docs| * - tests - | |travis| |requires| | |codecov| | * - package - |version| |downloads| |wheel| |supported-versions| |supported-implementations| .. |docs| image:: https://readthedocs.org/projects/django-accept-header/badge/?style=flat :target: https://readthedocs.org/projects/django-accept-header :alt: Documentation Status .. |travis| image:: https://travis-ci.org/fladi/django-accept-header.svg?branch=master :alt: Travis-CI Build Status :target: https://travis-ci.org/fladi/django-accept-header .. |requires| image:: https://requires.io/github/fladi/django-accept-header/requirements.svg?branch=master :alt: Requirements Status :target: https://requires.io/github/fladi/django-accept-header/requirements/?branch=master .. |codecov| image:: https://codecov.io/github/fladi/django-accept-header/coverage.svg?branch=master :alt: Coverage Status :target: https://codecov.io/github/fladi/django-accept-header .. |version| image:: https://img.shields.io/pypi/v/django-accept-header.svg?style=flat :alt: PyPI Package latest release :target: https://pypi.python.org/pypi/django-accept-header .. |downloads| image:: https://img.shields.io/pypi/dm/django-accept-header.svg?style=flat :alt: PyPI Package monthly downloads :target: https://pypi.python.org/pypi/django-accept-header .. |wheel| image:: https://img.shields.io/pypi/wheel/django-accept-header.svg?style=flat :alt: PyPI Wheel :target: https://pypi.python.org/pypi/django-accept-header .. |supported-versions| image:: https://img.shields.io/pypi/pyversions/django-accept-header.svg?style=flat :alt: Supported versions :target: https://pypi.python.org/pypi/django-accept-header .. |supported-implementations| image:: https://img.shields.io/pypi/implementation/django-accept-header.svg?style=flat :alt: Supported implementations :target: https://pypi.python.org/pypi/django-accept-header A Django middleware that inspects the HTTP Acept headers sent by browsers. It adds a new method to each `request` instance called `accepts(str)` which can be used to determine if a certain mimetype is accepted by the user agent that issued the request. Installation ============ :: pip install django-accept-header Usage ===== First add the middleware to your `settings.py` file:: MIDDLEWARE_CLASSES = ( # ... 'django_accept_header.middleware.AcceptMiddleware', ) To check if the `text/plain` mimetype is accepted by the user agent:: def some_view(request): if request.accepts('text/plain'): # do something The ordered list of accepted mimetypes can also be used:: def some_view(request): for media_type in request.accepted_types: # do something See the full documentation for how to use the media types please see the full documentation. Documentation ============= https://django-accept-header.readthedocs.org/ Development =========== To run the all tests run:: tox 0.3.0 / 2016-02-02 ================== * Change package name to django-accept-header. * Make python3.5 the default version used for tests. 0.1.0 / 2015-11-26 ================== * First release on PyPI. PKlIBHC_+django_accept_header-0.3.2.dist-info/RECORDdjango_accept_header/__init__.py,sha256=vNiWJ14r_cw5t_7UDqDQIVZvladKFGyHH2avsLpN7Vg,22 django_accept_header/header.py,sha256=Yws1hcmeyzb4bjkoqez8gPVG4kt1RkaBDaIRAC-kjwE,4355 django_accept_header/middleware.py,sha256=bIe_JIcmzlaSzM7t_lTRcd_XvcDOmFp54m8mzGBR88c,1140 django_accept_header-0.3.2.dist-info/DESCRIPTION.rst,sha256=HFM4Umr8uG308guOTeUcYF_o731k39apZYwEmFGUCvc,3364 django_accept_header-0.3.2.dist-info/METADATA,sha256=ue1vqr6llqhnVuS362GK1VaSXMKPN_PSKGsEw0nQV_g,4490 django_accept_header-0.3.2.dist-info/RECORD,, django_accept_header-0.3.2.dist-info/WHEEL,sha256=GrqQvamwgBV4nLoJe0vhYRSWzWsx7xjlt74FT0SWYfE,110 django_accept_header-0.3.2.dist-info/metadata.json,sha256=LtVWHSdeQkhAFBURdswpYD1TH7BFDphc1-3eIL2aw-w,1289 django_accept_header-0.3.2.dist-info/pbr.json,sha256=VB2ccbcoidZkLJtSwIN8BS17jcz7fTCXh3GA7tFTalA,47 django_accept_header-0.3.2.dist-info/top_level.txt,sha256=wxa9qQysSbNY2mpEoqdEMIPZGyb30hHQLXyLxXjeLh0,21 PKS~G.Idjango_accept_header/header.pyPK0V~G&xtt"?django_accept_header/middleware.pyPKgIBHw django_accept_header/__init__.pyPKlIBHy9$ $ 4Gdjango_accept_header-0.3.2.dist-info/DESCRIPTION.rstPKlIBHRS  2#django_accept_header-0.3.2.dist-info/metadata.jsonPKaG~ //-)django_accept_header-0.3.2.dist-info/pbr.jsonPKlIBH,|2)django_accept_header-0.3.2.dist-info/top_level.txtPKlIBHndnn*)django_accept_header-0.3.2.dist-info/WHEELPKlIBHr8-*django_accept_header-0.3.2.dist-info/METADATAPKlIBHC_+<django_accept_header-0.3.2.dist-info/RECORDPK sw@