Skip to content

Commit

Permalink
Restored support to oldest Django versions
Browse files Browse the repository at this point in the history
  • Loading branch information
silviolleite committed Feb 1, 2019
1 parent 053afe1 commit 2326895
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 33 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ django-pwa
[![codecov](https://codecov.io/gh/silviolleite/django-pwa/branch/master/graph/badge.svg)](https://codecov.io/gh/silviolleite/django-pwa)
[![PyPI - Downloads](https://img.shields.io/pypi/dm/django-pwa.svg)](https://pypi.org/project/django-pwa/)
[![PyPI - Downloads](https://img.shields.io/pypi/v/django-pwa.svg)](https://pypi.org/project/django-pwa)
[![PyPI - Downloads](https://img.shields.io/pypi/djversions/django-pwa.svg)](https://pypi.org/project/django-pwa)

This Django app turns your project into a [progressive web app](https://developers.google.com/web/progressive-web-apps/). Navigating to your site on an Android phone will prompt you to add the app to your home screen.

Expand Down Expand Up @@ -67,11 +68,11 @@ All settings are optional, and the app will work fine with its internal defaults

Add the progressive web app URLs to urls.py:
```python
from django.urls import path, include
from django.urls import url, include

urlpatterns = [
...
path('', include('pwa.urls')), # You MUST use an empty string as the URL prefix
url('', include('pwa.urls')), # You MUST use an empty string as the URL prefix
...
]
```
Expand Down Expand Up @@ -172,7 +173,7 @@ Adding Your Own Service Worker
To add service worker functionality, you'll want to create a `serviceworker.js` or similarly named template in a template directory, and then point at it using the PWA_SERVICE_WORKER_PATH variable (PWA_APP_FETCH_URL is passed through).

```python
PWA_SERVICE_WORKER_PATH = 'my_app/serviceworker.js'
PWA_SERVICE_WORKER_PATH = os.path.join(BASE_DIR, 'my_app', 'serviceworker.js')

```

Expand Down
5 changes: 3 additions & 2 deletions pwa/app_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import os

# Path to the service worker implementation. Default implementation is empty.
PWA_SERVICE_WORKER_PATH = getattr(settings, 'PWA_SERVICE_WORKER_PATH', 'serviceworker.js')

PWA_SERVICE_WORKER_PATH = getattr(settings, 'PWA_SERVICE_WORKER_PATH',
os.path.join(os.path.abspath(os.path.dirname(__file__)), 'templates',
'serviceworker.js'))
# App parameters to include in manifest.json and appropriate meta tags
PWA_APP_NAME = getattr(settings, 'PWA_APP_NAME', 'MyApp')
PWA_APP_DESCRIPTION = getattr(settings, 'PWA_APP_DESCRIPTION', 'My Progressive Web App')
Expand Down
10 changes: 5 additions & 5 deletions pwa/urls.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from django.urls import path
from django.conf.urls import url

from .views import Manifest, ServiceWorker, OfflineView
from .views import manifest, service_worker, offline

# Serve up serviceworker.js and manifest.json at the root
urlpatterns = [
path('serviceworker.js', ServiceWorker.as_view(), name='serviceworker'),
path('manifest.json', Manifest.as_view(), name='manifest'),
path('offline', OfflineView.as_view(), name='offline')
url('^serviceworker.js$', service_worker, name='serviceworker'),
url('^manifest.json$', manifest, name='manifest'),
url('^offline/$', offline, name='offline')
]
32 changes: 13 additions & 19 deletions pwa/views.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
from django.views.generic.base import TemplateView
from django.http import HttpResponse
from django.shortcuts import render

from . import app_settings


class ServiceWorker(TemplateView):
content_type = 'application/javascript'
template_name = app_settings.PWA_SERVICE_WORKER_PATH

def get_context_data(self, **kwargs):
kwargs['PWA_APP_FETCH_URL'] = app_settings.PWA_APP_FETCH_URL
return super().get_context_data(**kwargs)
def service_worker(request):
response = HttpResponse(open(app_settings.PWA_SERVICE_WORKER_PATH).read(), content_type='application/javascript')
return response


class Manifest(TemplateView):
content_type = 'application/json'
template_name = 'manifest.json'

def get_context_data(self, **kwargs):
for setting_name in dir(app_settings):
if setting_name.startswith('PWA_'):
kwargs[setting_name] = getattr(app_settings, setting_name)
return super().get_context_data(**kwargs)
def manifest(request):
return render(request, 'manifest.json', {
setting_name: getattr(app_settings, setting_name)
for setting_name in dir(app_settings)
if setting_name.startswith('PWA_')
})


class OfflineView(TemplateView):
template_name = "offline.html"
def offline(request):
return render(request, "offline.html")
8 changes: 6 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir)))

install_requirements = [
"django>=2",
"django>=1.8",
]

setup(
name='django-pwa',
version='1.0.2',
version='1.0.3',
packages=find_packages(),
install_requires=install_requirements,
include_package_data=True,
Expand All @@ -35,6 +35,10 @@
classifiers=[
'Environment :: Web Environment',
'Framework :: Django',
'Framework :: Django :: 1.8',
'Framework :: Django :: 1.9',
'Framework :: Django :: 1.10',
'Framework :: Django :: 1.11',
'Framework :: Django :: 2.0',
'Framework :: Django :: 2.1',
'Intended Audience :: Developers',
Expand Down
8 changes: 6 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[tox]
envlist =
py35-django{20}
py35-django{21}
py35-django{18,19,110,111,20,21}
py36-django{20}
py36-django{21}
py37-django{20}
Expand All @@ -17,6 +16,11 @@ basepython =
py36: python3.6
py37: python3.7
deps =
django18: django==1.8
django19: django==1.9
django110: django==1.10
django111: django==1.11
django20: Django==2.0
django21: Django==2.1

pypandoc==1.3.3

0 comments on commit 2326895

Please sign in to comment.