Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit d2ce70ad38f8996b376f05cde265ff3319f95fde David Cramer committed Aug 23, 2009
Showing with 181 additions and 0 deletions.
  1. +4 −0 .gitignore
  2. +9 −0 LICENSE
  3. +2 −0 MANIFEST.in
  4. +68 −0 README.rst
  5. +1 −0 django_notices/__init__.py
  6. +15 −0 django_notices/constants.py
  7. +5 −0 django_notices/context_processors.py
  8. +57 −0 django_notices/helpers.py
  9. +5 −0 django_notices/middleware.py
  10. +15 −0 setup.py
@@ -0,0 +1,4 @@
+build/
+dist/
+*.pyc
+django_notices.egg-info/
@@ -0,0 +1,9 @@
+Copyright (c) 2009, David Cramer <dcramer@gmail.com>
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+
+* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+* 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.
@@ -0,0 +1,2 @@
+include setup.py README.rst MANIFEST.in
+global-exclude *~
@@ -0,0 +1,68 @@
+This is a layer that functions much like the Django ORM does except it works on top of the Sphinx (http://www.sphinxsearch.com) full-text search engine.
+
+Please Note: You will need to create your own sphinx indexes and install sphinx on your server to use this app.
+
+*There will no longer be release packages available. Please use SVN to checkout the latest trunk version, as it should always be stable and current.*
+
+Installation
+------------
+
+To install the latest stable version::
+
+ sudo easy_install django-notices
+
+To install the latest development version (updated quite often)::
+
+ git clone git://github.com/dcramer/django-notices.git
+ cd django_notices
+ sudo python setup.py install
+
+Usage
+-----
+
+The easiest way to use django-notices, is simply by adding it to your settings.py::
+
+ INSTALLED_APPS = (
+ '...',
+ 'django_notices',
+ )
+
+ MIDDLEWARE_CLASSES = (
+ 'django_notices.middleware.NoticeMiddleware',
+ )
+
+ TEMPLATE_CONTEXT_PROCESSORS = (
+ 'django_notices.context_processors.notices',
+ )
+
+Once you've done this you'll now have access to two important pieces of the notice system, the ``notices`` context variable, and ``request.notices``.
+
+The first thing you're going to want to do, is add a handler for ``notices`` within your templates::
+
+ <div id="notices">
+ <ul>
+ {% for notice in request.notices %}
+ <li class="notice notice-{{ notice.level_label }}">{{ notice.message|escape }}</li>
+ {% endfor %}
+ </ul>
+ </div>
+
+What this does, is on each page load, displays each active notice and clears it from the session.
+
+Now once you can display your notices, you'll need to begin adding them. This is also made very ::
+
+ def my_view(request):
+ request.notices.warn('This is a warning')
+
+There are several methods available in the built-in handler::
+
+ NoticeHandler.warn
+ NoticeHandler.error
+ NoticeHandler.info
+ NoticeHandler.debug
+ NoticeHandler.success
+
+By default, DEBUG level notices are not shown. To change this, you can adjust the ``NOTICE_LEVEL`` setting in your ``settings.py``::
+
+ from django_notices import DEBUG
+ NOTICE_LEVEL = DEBUG
@@ -0,0 +1 @@
+from constants import *
@@ -0,0 +1,15 @@
+DEBUG = 10
+INFO = 20
+NOTICE = INFO
+WARNING = 30
+ERROR = 40
+SUCCESS = 10
+
+LEVELS = {
+ SUCCESS: 'success',
+ NOTICE: 'info',
+ WARNING: 'warning',
+ ERROR: 'error,'
+ INFO: 'info',
+ DEBUG: 'debug',
+}
@@ -0,0 +1,5 @@
+def notices(request):
+ """Add the ``notices`` variable to the context."""
+ return {
+ 'notices': NoticeHandler(request.session)
+ }
@@ -0,0 +1,57 @@
+from django.conf import settings
+from constants import *
+
+CURRENT_LEVEL = getattr(settings, 'NOTICE_LEVEL', INFO)
+
+class Notice(object):
+ def __init__(self, level, message):
+ self.level, self.message = level, message
+
+ @property
+ def level_label(self):
+ """Typically used for CSS class names."""
+ return LEVELS.get(self.level, LEVELS[NOTICE])
+
+class NoticeHandler(object):
+ def __init__(self, session):
+ self.session = session
+
+ def __iter__(self):
+ """Return each notice and removes it from session."""
+ messages = self.session.get('_messages')
+ if messages:
+ for n in messages:
+ if n.level >= CURRENT_LEVEL:
+ yield n
+ del self.session['_messages']
+
+ def add(self, message, level=NOTICE):
+ self.session.setdefault('_messages', []).append(Notice(level, message))
+
+ def get(self):
+ """Retrieves current notices.
+
+ Use ``get_and_clear()`` if you wish to also clear the notices."""
+ return self.session.get('_messages', [])
+
+ def get_and_clear(self):
+ """Retrieves current notices, and clears them from the session."""
+ return self.session.pop('_messages', [])
+
+ # Wrapper functions for add
+
+ def warn(self, message):
+ self.add(message, WARN)
+
+ def error(self, message):
+ self.add(message, ERROR)
+
+ def notice(self, message):
+ self.add(message, NOTICE)
+ info = notice
+
+ def debug(self, message):
+ self.add(message, DEBUG)
+
+ def success(self, message):
+ self.add(message, SUCCESS)
@@ -0,0 +1,5 @@
+from helpers import NoticeHandler
+
+class NoticeMiddleware(object):
+ def process_request(self, request):
+ request.notices = NoticeHandler(request.session)
@@ -0,0 +1,15 @@
+#!/usr/bin/env python
+
+from setuptools import setup, find_packages
+
+setup(
+ name='django-notices',
+ version='0.1',
+ author='David Cramer',
+ author_email='dcramer@gmail.com',
+ url='http://github.com/dcramer/django-notices',
+ install_requires=['django'],
+ description = 'A message notification system for Django.',
+ packages=find_packages(),
+ include_package_data=True,
+)

0 comments on commit d2ce70a

Please sign in to comment.