Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Cleaning up code, improving documentation

  • Loading branch information...
commit 2eaa914abe305eb94dc51186e88c9c73ace39cd4 1 parent d6c38b7
@overshard authored
Showing with 45 additions and 44 deletions.
  1. +15 −10 README.md
  2. +29 −33 fresh/middleware.py
  3. +1 −1  setup.py
View
25 README.md
@@ -1,25 +1,30 @@
# django-fresh
-django-fresh will auto-refresh your browser whenever there is an update to any
-of the files in your project. Very useful for development, not intended for
+django-fresh will auto-refresh your browser whenever you update to any of the
+files in your project. Very useful for development, not intended for
production!
## Setup
- pip install django-fresh
+ 1. Install fresh by running `pip install django-fresh`.
+ 2. In `settings.py` add `fresh` to `INSTALLED_APPS`.
+ 3. In `settings.py` add `fresh.middleware.FreshMiddleware` to `MIDDLEWARE_CLASSES`.
+ 4. In `urls.py` add `url(r'', include('fresh.urls'))` to `urlpatterns`.
-Go to your projects `settings.py`, add `fresh` to your `INSTALLED_APPS` and add
-to `MIDDLEWARE_CLASSES` `fresh.middleware.Fresh`. Then open up `urls.py` and
-add `url(r'', include('fresh.urls'))` to your `urlpatterns`.
+## How It Works
-## Notice
+django-fresh injects a small piece of JavaScript into each of your HTML pages
+which will then make the page poll your Django app checking if files were
+changed. If django-fresh sees that you modified a file it will tell the next
+polling to refresh the page.
+
+
+## Note
django-fresh checks to see if `debug` is `True`, if it is `False` it doesn't do
-anything so it is safe to add it to a production `settings.py`. However, I do
-recommend following good Django practices and having separate development and
-production settings.
+anything to prevent you from accidently including it in production.
## License (Simplified BSD)
View
62 fresh/middleware.py
@@ -13,49 +13,45 @@
fresh = False
-class Fresh(object):
- class RefreshEventHandler(FileSystemEventHandler):
+class RefreshEventHandler(FileSystemEventHandler):
- def on_any_event(self, event):
- global fresh
- fresh = True
+ def on_any_event(self, event):
+ global fresh
+ fresh = True
+
+
+class FreshMiddleware(object):
+
+ def process_response(self, request, response):
+ if not settings.DEBUG:
+ return response
+
+ mimetype = response._headers['content-type'][1]
+ global fresh
+
+ if mimetype == 'application/json':
+ items = json.loads(response.content)
+ if fresh and items.get('fresh') != None:
+ fresh = False
+ items['fresh'] = True
+ response.content = json.dumps(items)
+ elif mimetype == 'text/html; charset=utf-8':
+ soup = BeautifulSoup(response.content)
+ script_fresh = soup.new_tag('script', src='/static/fresh/js/refresher.js')
+ soup.head.append(script_fresh)
+ response.content = soup.prettify()
+
+ return response
def watcher(self):
observer = Observer()
path = settings.SITE_ROOT
- event_handler = self.RefreshEventHandler()
+ event_handler = RefreshEventHandler()
observer.schedule(event_handler, path, recursive=True)
observer.start()
- def process_response(self, request, response):
- if settings.DEBUG:
- mimetype = response._headers['content-type'][1]
- if mimetype == 'application/json':
- items = json.loads(response.content)
- try:
- global fresh
- if fresh:
- items['fresh'] = fresh
- fresh = False
- response.content = json.dumps(items)
- print response.content
- except:
- pass
- elif mimetype == 'text/html; charset=utf-8':
- try:
- soup = BeautifulSoup(response.content)
-
- # Append refresher
- script_fresh = soup.new_tag('script', src='/static/fresh/js/refresher.js')
- soup.head.append(script_fresh)
-
- response.content = soup.prettify()
- except:
- pass
- return response
-
def __init__(self):
if settings.DEBUG:
self.watcher()
View
2  setup.py
@@ -2,7 +2,7 @@
setup(
name='django-fresh',
- version='1.0.0',
+ version='1.0.1',
license='Simplified BSD',
install_requires = [
Please sign in to comment.
Something went wrong with that request. Please try again.