├── AUTHORS ├── LICENSE ├── MANIFEST.in ├── README.rst ├── admin_notifications ├── __init__.py ├── templates │ └── admin_error_notifications.html └── templatetags │ ├── __init__.py │ └── notification_tag.py └── setup.py /AUTHORS: -------------------------------------------------------------------------------- 1 | Andy Baker -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2009, ixxy.co.uk 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 5 | 6 | * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 7 | * 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. 8 | * Neither the name of ixxy.co.uk nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 9 | 10 | 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. -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include AUTHORS 2 | include LICENSE 3 | include README.rst 4 | recursive-include admin_notifications/templates * -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | django-admin-notifications 2 | ========================== 3 | 4 | A simple app to allow apps to register notifications that can be displayed in the admin via a template tag 5 | 6 | 7 | Installation 8 | ------------ 9 | 10 | Add 'admin_notifications' to INSTALLED_APPS 11 | 12 | 13 | Basic usage 14 | ----------- 15 | 16 | 17 | Customize whichever of your admin templates you which to display the notifications thus. 18 | 19 | load the template tags at the start of the template:: 20 | 21 | {% load notification_tag %} 22 | 23 | add the tag to your template. I customised the admin index template and placed the tag before the 'content-main' DIV like thus:: 24 | 25 | {% block content %} 26 | 27 | {% error_notifications %} 28 |
29 | 30 | in urls.py:: 31 | 32 | import admin_notifications 33 | admin_notifications.autodiscover() 34 | 35 | This looks in each app for a file called 'notifications.py and registers it if it exists. 36 | 37 | 38 | Notifications files can contain as many functions as you like. Each one should return a string which can contain HTML and each one needs to be registered using admin_notifications.register 39 | 40 | If you return an empty string then no notification is shown. 41 | 42 | Notifications functions are called every time the admin template is rendered so avoid doing heavy calculations in the notification. 43 | 44 | An example notifications.py might look like this:: 45 | 46 | import admin_notifications 47 | from models import Url 48 | def notification(): 49 | broken_links = Url.objects.filter(status=False).count() 50 | if broken_links: 51 | return "You have %s broken link%s.
You can view or fix them using the Link Manager." % (broken_links, "s" if broken_links>1 else "") 52 | else: 53 | return '' 54 | 55 | admin_notifications.register(notification) 56 | -------------------------------------------------------------------------------- /admin_notifications/__init__.py: -------------------------------------------------------------------------------- 1 | import imp 2 | import os 3 | 4 | _error_registry = [] 5 | _alert_registry = [] 6 | _app_block_registry = [] 7 | 8 | def register(notifications): 9 | if type(notifications).__name__=='function': 10 | # for compatibility we assume that registering a single function is an error notification 11 | _error_registry.append(notifications) 12 | else: 13 | switch = { 14 | 'errors': _error_registry, 15 | 'alerts': _alert_registry, 16 | 'app_blocks': _app_block_registry, 17 | } 18 | for ntype in notifications.keys(): 19 | action = switch[ntype] 20 | for entry in notifications[ntype]: 21 | action.append(entry) 22 | 23 | def autodiscover(): 24 | from django.conf import settings 25 | for app in settings.INSTALLED_APPS: 26 | try: 27 | app_path = __import__(app, {}, {}, [app.split('.')[-1]]).__path__ 28 | except AttributeError: 29 | continue 30 | try: 31 | imp.find_module('notifications', app_path) 32 | except ImportError: 33 | continue 34 | __import__("%s.notifications" % app) 35 | 36 | 37 | -------------------------------------------------------------------------------- /admin_notifications/templates/admin_error_notifications.html: -------------------------------------------------------------------------------- 1 | {% if notifications %} 2 |
3 | {% for notification in notifications %} 4 |

{{notification|safe}}

5 | {% endfor %} 6 |
7 | {% endif %} -------------------------------------------------------------------------------- /admin_notifications/templatetags/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/andybak/django-admin-notifications/3d34adab86d24d32603e4b5730ecae63242fa8ca/admin_notifications/templatetags/__init__.py -------------------------------------------------------------------------------- /admin_notifications/templatetags/notification_tag.py: -------------------------------------------------------------------------------- 1 | from django import template 2 | 3 | register = template.Library() 4 | 5 | @register.inclusion_tag('admin_error_notifications.html') 6 | def error_notifications(): 7 | from admin_notifications import _error_registry 8 | notifications = [x for x in [x() for x in _error_registry] if x] 9 | return { 10 | 'notifications': notifications, 11 | } 12 | 13 | 14 | @register.simple_tag 15 | def app_block(app): 16 | from admin_notifications import _app_block_registry 17 | html = u'' 18 | for tuple in _app_block_registry: 19 | if tuple[0].lower()==app['name'].lower(): 20 | html += u'\n'.join(["%s" % x() for x in tuple[1:]]) 21 | return html -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | import os 2 | from distutils.core import setup 3 | 4 | def read(fname): 5 | return open(os.path.join(os.path.dirname(__file__), fname)).read() 6 | 7 | def package_data(package): 8 | package_data = [] 9 | for dirpath, dirnames, filenames in os.walk( 10 | os.path.join(os.path.dirname(__file__), package)): 11 | for i, dirname in enumerate(dirnames): 12 | if dirname.startswith('.'): del dirnames[i] 13 | if '__init__.py' in filenames: 14 | continue 15 | elif filenames: 16 | for f in filenames: 17 | package_data.append( 18 | os.path.join(dirpath[len(package)+len(os.sep):], f)) 19 | return {package: package_data} 20 | 21 | setup( 22 | name='django-admin-notifications', 23 | version='0.6.3', 24 | description="A simple app to allow apps to register notifications " 25 | "that can be displayed in the admin via a template tag.", 26 | long_description=read('README.rst'), 27 | author='Andy Baker', 28 | author_email='andy@andybak.net', 29 | license='BSD', 30 | url='http://github.com/andybak/django-admin-notifications/', 31 | packages=[ 32 | 'admin_notifications', 33 | ], 34 | package_data=package_data('admin_notifications'), 35 | classifiers=[ 36 | 'Development Status :: 4 - Beta', 37 | 'Environment :: Web Environment', 38 | 'Intended Audience :: Developers', 39 | 'License :: OSI Approved :: BSD License', 40 | 'Operating System :: OS Independent', 41 | 'Programming Language :: Python', 42 | 'Framework :: Django', 43 | ], 44 | ) --------------------------------------------------------------------------------