├── drealtime ├── templatetags │ ├── __init__.py │ └── drealtimetags.py ├── middleware.py └── __init__.py ├── dist ├── django-realtime-1.1.tar.gz └── django-realtime-1.1.macosx-10.5-intel.tar.gz ├── setup.py └── README.md /drealtime/templatetags/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /dist/django-realtime-1.1.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anishmenon/django-realtime/HEAD/dist/django-realtime-1.1.tar.gz -------------------------------------------------------------------------------- /dist/django-realtime-1.1.macosx-10.5-intel.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anishmenon/django-realtime/HEAD/dist/django-realtime-1.1.macosx-10.5-intel.tar.gz -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup, find_packages 2 | 3 | setup( 4 | name='django-realtime', 5 | version='1.1', 6 | author='Anish menon', 7 | author_email='anish@inzane.in', 8 | description='an iShout.js client for Django', 9 | url='https://bitbucket.org/inzane/django-realtime', 10 | license='MIT', 11 | packages=find_packages(), 12 | classifiers = [ 13 | 'Development Status :: 3 - Alpha', 14 | 'Environment :: Web Environment', 15 | 'Intended Audience :: Developers', 16 | 'License :: OSI Approved :: MIT License', 17 | 'Operating System :: OS Independent', 18 | 'Programming Language :: Python', 19 | 'Framework :: Django' 20 | ] 21 | ) 22 | -------------------------------------------------------------------------------- /drealtime/templatetags/drealtimetags.py: -------------------------------------------------------------------------------- 1 | from django.conf import settings 2 | from django.template import Library 3 | register = Library() 4 | 5 | def _determine_base_path(): 6 | client = getattr(settings, 'ISHOUT_CLIENT_ADDR', 'localhost:5500') 7 | secure = getattr(settings, 'ISHOUT_HTTPS', False) 8 | proto = 'http' 9 | if secure: 10 | proto = 'https' 11 | 12 | return '%s://%s' % (proto, client) 13 | 14 | @register.simple_tag 15 | def ishout_socketio_path(): 16 | return '%s/%s' %(_determine_base_path(), 'socket.io/socket.io.js') 17 | 18 | @register.simple_tag 19 | def ishout_js_path(): 20 | return '%s/%s' %(_determine_base_path(), 'client/ishout.client.js') 21 | 22 | 23 | @register.simple_tag 24 | def ishout_js(): 25 | """ 26 | returns the needed HTML tags for including ishout.js and its dependency 27 | (socket.io) in the client's browser. make sure that `ISHOUT_CLIENT_ADDR` 28 | is in the settings.py file and is reachable from the internet, as 29 | it will be used as the domain name. if you are using ishout.js over 30 | SSL, be sure to set `ISHOUT_HTTPS` to True. 31 | """ 32 | path = _determine_base_path() 33 | return """ 34 | 35 | 36 | """ % (path, path) -------------------------------------------------------------------------------- /drealtime/middleware.py: -------------------------------------------------------------------------------- 1 | import datetime 2 | from django.conf import settings 3 | from drealtime import iShoutClient 4 | 5 | ishout_client = iShoutClient() 6 | ishout_cookie_name = 'ishoutToken' 7 | 8 | class iShoutCookieMiddleware(object): 9 | """ 10 | call the iShout.js API interface and get a token 11 | for the currently logged-in user. 12 | set the token received from the API as a cookie. 13 | 14 | Put this before `AuthenticationMiddleware`. 15 | """ 16 | def get_token(self, request): 17 | """ 18 | use the HTTP client to get a token from the iShout.js server, 19 | for the currently logged in user. 20 | """ 21 | res = ishout_client.get_token(request.user.pk) 22 | return res 23 | 24 | def has_ishout_cookie(self, request): 25 | cookie = request.COOKIES.get(ishout_cookie_name) 26 | if cookie: 27 | return True 28 | return False 29 | 30 | def determine_path(self, request): 31 | # Use the same path as the session cookie. 32 | return settings.SESSION_COOKIE_PATH 33 | 34 | def determine_domain(self, request): 35 | # Use the same domain as the session cookie. 36 | return settings.SESSION_COOKIE_DOMAIN 37 | 38 | def set_ishout_cookie(self, request, response): 39 | cookie_path = self.determine_path(request) 40 | cookie_domain = self.determine_domain(request) 41 | ishout_cookie_value = self.get_token(request) 42 | 43 | # calculate expiry 44 | cookie_age = datetime.timedelta(seconds=settings.SESSION_COOKIE_AGE) 45 | 46 | utc_date = datetime.datetime.utcnow() 47 | cookie_date_str = '%a, %d-%b-%Y %H:%M:%S GMT' 48 | expires = datetime.datetime.strftime( 49 | utc_date + cookie_age, cookie_date_str 50 | ) 51 | 52 | # Set the cookie. use the same path, domain and expiry 53 | # as the cookie set for the session. 54 | response.set_cookie( 55 | ishout_cookie_name, 56 | ishout_cookie_value, 57 | max_age=settings.SESSION_COOKIE_AGE, 58 | expires=expires, 59 | path=cookie_path, 60 | domain=cookie_domain 61 | ) 62 | return response 63 | 64 | def process_response(self, request, response): 65 | # We only use it for authenticated users 66 | if not hasattr(request, 'user'): 67 | return response 68 | 69 | if not request.user.is_authenticated() and \ 70 | ishout_cookie_name in request.COOKIES: 71 | # If there is no authenticated user attached to this request, 72 | # but the ishout.js token cookie is still present, delete it. 73 | # This is usually called on logout. 74 | path = self.determine_path(request) 75 | domain = self.determine_domain(request) 76 | response.delete_cookie( 77 | ishout_cookie_name, path=path, domain=domain 78 | ) 79 | return response 80 | 81 | # skip unauthenticated users 82 | if not request.user.is_authenticated(): 83 | return response 84 | 85 | # Check if we have the cookie already set: 86 | if self.has_ishout_cookie(request): 87 | return response 88 | 89 | # If not, set it. 90 | self.set_ishout_cookie(request, response) 91 | return response 92 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Django-Realtime: an iShout.js client for Django. 2 | 3 | iShout.js is a node.js + socket.io server used for adding real-time notifications to your existing web application. 4 | This project adds easy iShout.js integration for Django, thus enabling you to easily 5 | add real-time notifications to you exiting Django applications. 6 | 7 | Before installing and using the client, make sure you setup iShout.js using [this guide](https://bitbucket.org/inzane/ishout "iShout.js Installation"). 8 | 9 | 10 | ## Installation 11 | 12 | * install the client either by cloning this repo, or, if using pip, by using the following command: 13 | 14 | pip install django-realtime 15 | 16 | * add `drealtime` to `INSTALLED_APPS` in your settings.py file. 17 | 18 | 19 | ## Configuration 20 | 21 | * add `'drealtime.middleware.iShoutCookieMiddleware'` to your `MIDDLEWARE_CLASSES`settings. right above `SessionMiddleware`. this will take care of setting the authentication cookie. 22 | * in your templates, add `{% load drealtimetags %}` at the top of the template. inside your `
` tag, add `{% ishout_js %}`. this will include the proper `