Skip to content

Commit

Permalink
Merge 89e5d9b into f471249
Browse files Browse the repository at this point in the history
  • Loading branch information
jwhitlock committed Feb 17, 2017
2 parents f471249 + 89e5d9b commit 1d5355e
Show file tree
Hide file tree
Showing 19 changed files with 439 additions and 13 deletions.
26 changes: 22 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
*.pyc
*pip*
*.egg-info
dist
# Cherry-picked from:
# https://github.com/github/gitignore/blob/master/Python.gitignore

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# Distribution / packacking
*.egg-info/
build/
dist/

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
30 changes: 30 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
sudo: no
language: python
cache: pip
matrix:
include:
- python: "2.7"
env: TOXENV=py27-1.8
- python: "3.5"
env: TOXENV=py34-1.8
- python: "2.7"
env: TOXENV=py27-1.9
- python: "3.5"
env: TOXENV=py35-1.9
- python: "2.7"
env: TOXENV=py27-1.10
- python: "3.5"
env: TOXENV=py35-1.10
- python: "2.7"
env: TOXENV=py27-1.11
- python: "3.6"
env: TOXENV=py36-1.11
- python: "3.6"
env: TOXENV=py36-master
allow_failures:
- env: TOXENV=py36-master
install:
- pip install coveralls tox
script:
- tox
after_success: coveralls
12 changes: 12 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Release History
---------------

v0.2.0 - 2017-02-17
~~~~~~~~~~~~~~~~~~~
* Supported Django versions: 1.8, 1.9, 1.10, and 1.11
* Supported Python versions: 2.7, 3.3, 3.4. 3.5, 3.6
* Add "DNT" to Vary header in response (eillarra)

v0.1.0 - 2011-02-16
~~~~~~~~~~~~~~~~~~~
* Initial Release
9 changes: 9 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
include HISTORY.rst
include LICENSE
include Makefile
include README.rst
include manage.py
include requirements.dev.txt
include tox.ini

recursive-include testapp *.py
recursive-include testapp/templates *.html
recursive-include tests *.py
63 changes: 63 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
export DJANGO_SETTINGS_MODULE = testapp.settings
export PYTHONPATH := $(shell pwd)
.PHONY: help clean coverage coveragehtml develop lint qa qa-all release sdist test test-all

help:
@echo "clean - remove all artifacts"
@echo "coverage - check code coverage"
@echo "coveragehtml - display code coverage in browser"
@echo "develop - install development requirements"
@echo "lint - check style with flake8"
@echo "qa - run linters and test coverage"
@echo "qa-all - run QA plus tox and packaging"
@echo "release - package and upload a release"
@echo "sdist - package"
@echo "test - run tests"
@echo "test-all - run tests on every Python version with tox"
@echo "test-release - upload a release to the test PyPI server"

clean:
git clean -Xfd

develop:
pip install -r requirements.dev.txt

lint:
flake8 .

test:
django-admin test

test-all:
tox --skip_missing_interpreters

coverage: clean
coverage erase
coverage run --branch --source=dnt `which django-admin` test

coveragehtml: coverage
coverage html
python -m webbrowser file://$(CURDIR)/htmlcov/index.html

qa: lint coveragehtml

qa-all: qa sdist test-all

sdist:
python setup.py sdist bdist_wheel
ls -l dist
check-manifest
pyroma dist/`ls -t dist | grep tar.gz | head -n1`

release: clean sdist
twine register dist/*.tar.gz
twine register dist/*.whl
twine upload dist/*
python -m webbrowser -n https://pypi.python.org/pypi/django-dnt

# Add [test] section to ~/.pypirc, https://testpypi.python.org/pypi
test-release: clean sdist
twine register --repository test dist/*.tar.gz
twine register --repository test dist/*.whl
twine upload --repository test dist/*
python -m webbrowser -n https://testpypi.python.org/pypi/django-dnt
17 changes: 16 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,24 @@
Django-DNT
==========

.. image:: http://img.shields.io/travis/mozilla/django-dnt/master.svg
:alt: The status of Travis continuous integration tests
:target: https://travis-ci.org/mozilla/django-dnt

.. image:: https://img.shields.io/coveralls/mozilla/django-dnt/master.svg
:target: https://coveralls.io/r/mozilla/django-dnt
:alt: The code coverage

.. image:: https://img.shields.io/pypi/v/django-dnt.svg
:alt: The PyPI package
:target: https://pypi.python.org/pypi/django-dnt

.. Omit badges from docs
Do Not Track offers an easy way to pay attention to the ``DNT`` HTTP header. If
users are sending ``DNT: 1``, ``DoNotTrackMiddleware`` will set ``request.DNT =
True``, else it will set ``request.DNT = False``.

Just add ``dnt.middleware.DoNotTrackMiddleware`` to your ``MIDDLEWARE_CLASSES``
and you're good to go.
(Django 1.9 and earlier) or ``MIDDLEWARE`` (Django 1.10 and later) and you're
good to go.
6 changes: 6 additions & 0 deletions dnt/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""
Django Middleware for DNT (Do Not Track) HTTP header.
https://en.wikipedia.org/wiki/Do_Not_Track
"""
VERSION = '0.2.0'
14 changes: 11 additions & 3 deletions dnt/middleware.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
from django.utils.cache import patch_vary_headers

try:
# Added in Django 1.10
from django.utils.deprecation import MiddlewareMixin
except ImportError:
_base_class = object # pragma: no cover
else:
_base_class = MiddlewareMixin # pragma: no cover


class DoNotTrackMiddleware(_base_class):

class DoNotTrackMiddleware(object):

def process_request(self, request):
"""
Sets request.DNT to True or False based on the presence of the DNT HTTP header.
Sets flag request.DNT based on DNT HTTP header.
"""
if 'HTTP_DNT' in request.META and request.META['HTTP_DNT'] == '1':
request.DNT = True
Expand Down
9 changes: 9 additions & 0 deletions requirements.dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Testing and development requirements
Django
check-manifest
coverage
flake8
pyroma
tox
twine
wheel
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[bdist_wheel]
universal=1
69 changes: 64 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,70 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Packaging setup for django-dnt."""

from setuptools import setup

description = 'Make Django requests aware of the DNT header'


def read_file(path):
contents = open(path).read()
if hasattr(contents, 'decode'):
return contents.decode('utf8') # Python2 bytes to unicode
else:
return contents # Python3 reads unicode


def long_description():
"""Create a PyPI long description from docs."""
readme = read_file('README.rst')
body_tag = ".. Omit badges from docs"
try:
readme_body_start = readme.index(body_tag)
except ValueError:
readme_body = readme
else:
# Omit the badges and reconstruct the title
readme_text = readme[readme_body_start + len(body_tag) + 1:]
readme_body = """\
%(title_mark)s
%(title)s
%(title_mark)s
%(readme_text)s
""" % {
'title_mark': '=' * len(description),
'title': description,
'readme_text': readme_text,
}

try:
history = read_file('HISTORY.rst')
except IOError:
history = ''

long_description = """\
%(readme)s
%(history)s
""" % {
'readme': readme_body,
'history': history
}
return long_description


setup(
name='django-dnt',
version='0.1.0',
description='Make Django requests aware of the DNT header.',
long_description=open('README.rst').read(),
version='0.2.0',
description=description + '.',
long_description=long_description(),
author='James Socol',
author_email='james@mozilla.com',
url='http://github.com/mozilla/django-dnt',
license='BSD',
packages=['dnt'],
include_package_data=True,
package_data = { '': ['README.rst'] },
zip_safe=False,
keywords='django-dnt dnt do not track',
classifiers=[
'Development Status :: 4 - Beta',
'Environment :: Web Environment',
Expand All @@ -23,5 +75,12 @@
'Operating System :: OS Independent',
'Programming Language :: Python',
'Topic :: Software Development :: Libraries :: Python Modules',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
]
)
File renamed without changes.
68 changes: 68 additions & 0 deletions testapp/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import django


SECRET_KEY = "dnt-tests"
DEBUG = True
ALLOWED_HOSTS = []

INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',

'testapp',
)

if django.VERSION[:2] < (1, 10):
# Django 1.9 and earlier
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
'dnt.middleware.DoNotTrackMiddleware',
)
else:
# Django 1.10 and later
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'dnt.middleware.DoNotTrackMiddleware',
]

ROOT_URLCONF = 'testapp.urls'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': ':memory:'
}
}
9 changes: 9 additions & 0 deletions testapp/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Do Not Track</title>
</head>
<body>
<p>Do Not Track: <code class="dnt">{{ request.DNT }}</code></p>
</body>
</html>
7 changes: 7 additions & 0 deletions testapp/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.conf.urls import url
from django.views.generic import TemplateView


urlpatterns = [
url(r'^$', TemplateView.as_view(template_name="index.html"), name="index"),
]
Empty file added tests/__init__.py
Empty file.

0 comments on commit 1d5355e

Please sign in to comment.