Browse files

Initial commit.

  • Loading branch information...
0 parents commit 5640e91ef11cc5439638a7aee32aab18d2275927 @meshy meshy committed Nov 15, 2012
Showing with 146 additions and 0 deletions.
  1. +8 −0 CHANGELOG.md
  2. +23 −0 LICENSE
  3. +2 −0 MANIFEST.in
  4. +8 −0 Makefile
  5. +41 −0 README.rst
  6. +2 −0 cookie_message/__init__.py
  7. +42 −0 cookie_message/middleware.py
  8. +1 −0 cookie_message/models.py
  9. +1 −0 cookie_message/templates/cookies/message.html
  10. +18 −0 setup.py
8 CHANGELOG.md
@@ -0,0 +1,8 @@
+Changelog for django-cookie-message
+============================
+
+v0.1
+----
+
+* Create project.
+* Add middleware and tests.
23 LICENSE
@@ -0,0 +1,23 @@
+Copyright (c) 2012, Incuna Ltd
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification,
+are permitted provided that the following conditions are met:
+
+ 1. Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
+ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2 MANIFEST.in
@@ -0,0 +1,2 @@
+include README.rst
+recursive-include * *.html
8 Makefile
@@ -0,0 +1,8 @@
+SHELL := /bin/bash
+
+help:
+ @echo "Usage:"
+ @echo " make release | Release to pypi."
+
+release:
+ python setup.py register sdist upload
41 README.rst
@@ -0,0 +1,41 @@
+django-cookie-message
+=====================
+
+A simple middleware that uses the django messages framework to add a notification regarding the EU cookie directive.
+
+If it can't detect a cookie about cookies, sets it and displays a message.
+
+
+Installation
+------------
+
+Install from pypi. ::
+
+ pip install django-cookie-message
+
+In your settings: ::
+
+* Ensure the django messages framework is installed.
+* Add ``'cookie_message'`` to ``INSTALLED_APPS``.
+* Add ``'cookie_message.middleware.CookieMessageMiddleware'`` to ``MIDDLEWARE_CLASSES``.
+
+Configuration
+-------------
+
+**The message** can be changed by overriding the ``cookies/message.py`` template.
+
+**The cookie** can be changed by defining ``COOKIE_MESSAGE_KWARGS`` in your
+``settings.py``. The default is: ::
+
+ COOKIE_MESSAGE_KWARGS = {
+ 'key': 'cookie_message',
+ 'value': '',
+ 'max_age': 365 * 24 * 60 * 60, # One year.
+ 'expires': None,
+ 'path': '/',
+ 'domain': None,
+ 'secure': None,
+ 'httponly': False,
+ }
+
+These are the arguments sent to `set_cookie() <https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpResponse.set_cookie>`_.
2 cookie_message/__init__.py
@@ -0,0 +1,2 @@
+__author__ = 'Incuna Ltd'
+__version__ = '0.1'
42 cookie_message/middleware.py
@@ -0,0 +1,42 @@
+from django.conf import settings
+from django.contrib import messages
+from django.template.loader import render_to_string
+
+
+COOKIE_MESSAGE_KWARGS = {
+ 'key': 'cookie_message',
+ 'value': '',
+ 'max_age': 365 * 24 * 60 * 60,
+ 'expires': None,
+ 'path': '/',
+ 'domain': None,
+ 'secure': None,
+ 'httponly': False,
+}
+COOKIE_MESSAGE_KWARGS.update(getattr(settings, 'COOKIE_MESSAGE_KWARGS', {}))
+
+
+class CookieMessageMiddleware(object):
+ template = 'cookies/message.html'
+
+ def process_view(self, request, *args, **kwargs):
+ # Add message on request
+ if not self.has_cookie(request):
+ messages.info(request, self.get_message(request))
+
+ def process_template_response(self, request, response):
+ # Add cookie on response
+ if not self.has_cookie(request):
+ response.set_cookie(**COOKIE_MESSAGE_KWARGS)
+ return response
+
+ def has_cookie(self, request):
+ if not hasattr(request, 'COOKIES'):
+ return False
+ return COOKIE_MESSAGE_KWARGS['key'] in request.COOKIES
+
+ def get_context(self, request):
+ return {'request': request}
+
+ def get_message(self, request):
+ return render_to_string(self.template, self.get_context(request))
1 cookie_message/models.py
@@ -0,0 +1 @@
+# Models file required by django.
1 cookie_message/templates/cookies/message.html
@@ -0,0 +1 @@
+We use cookies to give you a better experience. You can change your browser's cookie settings at any time. To learn more about cookies, please visit <a href="http://allaboutcookies.org">allaboutcookies.org</a>.
18 setup.py
@@ -0,0 +1,18 @@
+from setuptools import setup, find_packages
+
+import cookie_message
+
+
+setup(
+ name='django-cookie-message',
+ packages=find_packages(),
+ include_package_data=True,
+ version=cookie_message.__version__,
+ description='',
+ long_description=open('README.rst').read(),
+ author=cookie_message.__author__,
+ author_email='admin@incuna.com',
+ url='https://github.com/incuna/django-cookie-message/',
+ install_requires=[],
+ zip_safe=False,
+)

0 comments on commit 5640e91

Please sign in to comment.