Initial revision.
This commit is contained in:
0
pwa/__init__.py
Normal file
0
pwa/__init__.py
Normal file
21
pwa/app_settings.py
Normal file
21
pwa/app_settings.py
Normal file
@@ -0,0 +1,21 @@
|
||||
""" Settings required by django-progressive-web-app. """
|
||||
from django.conf import settings
|
||||
import os
|
||||
|
||||
# Path to the service worker implementation. Default implementation is empty.
|
||||
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')
|
||||
PWA_APP_ROOT_URL = getattr(settings, 'PWA_APP_ROOT_URL', '/')
|
||||
PWA_APP_THEME_COLOR = getattr(settings, 'PWA_APP_THEME_COLOR', '#000')
|
||||
PWA_APP_ICONS = getattr(settings, 'PWA_APP_ICONS', [
|
||||
{
|
||||
'src': '/',
|
||||
'sizes': '160x160'
|
||||
}
|
||||
])
|
||||
|
||||
|
||||
5
pwa/apps.py
Normal file
5
pwa/apps.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class PwaConfig(AppConfig):
|
||||
name = 'pwa'
|
||||
11
pwa/templates/manifest.json
Normal file
11
pwa/templates/manifest.json
Normal file
@@ -0,0 +1,11 @@
|
||||
{% load pwa %}
|
||||
{
|
||||
"name": {{ PWA_APP_NAME|js }},
|
||||
"short_name": {{ PWA_APP_NAME|js }},
|
||||
"description": {{ PWA_APP_DESCRIPTION|js }},
|
||||
"start_url": "/",
|
||||
"display": "standalone",
|
||||
"background_color": "#fff",
|
||||
"theme_color": {{ PWA_APP_THEME_COLOR|js }},
|
||||
"icons": {{ PWA_APP_ICONS|js }}
|
||||
}
|
||||
27
pwa/templates/pwa.html
Normal file
27
pwa/templates/pwa.html
Normal file
@@ -0,0 +1,27 @@
|
||||
<!-- Path to manifest.json -->
|
||||
<link rel="manifest" href="/manifest.json">
|
||||
|
||||
<!-- Icons for Apple Devices -->
|
||||
{% for icon in PWA_APP_ICONS %}
|
||||
<link rel="apple-touch-icon" href="{{ icon.src }}" sizes="{{ icon.sizes }}">
|
||||
{% endfor %}
|
||||
|
||||
<meta name="theme-color" content="{{ PWA_APP_THEME_COLOR }}">
|
||||
<meta name="apple-mobile-web-app-capable" content="yes">
|
||||
<meta name="apple-mobile-web-app-title" content="{{ PWA_APP_NAME }}">
|
||||
<meta name="apple-mobile-web-app-status-bar-style" content="default">
|
||||
|
||||
<script type="text/javascript">
|
||||
// Initialize the service worker
|
||||
if ('serviceWorker' in navigator) {
|
||||
window.addEventListener('load', function () {
|
||||
navigator.serviceWorker.register('/serviceworker.js').then(function (registration) {
|
||||
// Registration was successful
|
||||
console.log('django-progressive-web-app: ServiceWorker registration successful with scope: ', registration.scope);
|
||||
}).catch(function (err) {
|
||||
// registration failed :(
|
||||
console.log('django-progressive-web-app: ServiceWorker registration failed: ', err);
|
||||
});
|
||||
});
|
||||
}
|
||||
</script>
|
||||
2
pwa/templates/serviceworker.js
Normal file
2
pwa/templates/serviceworker.js
Normal file
@@ -0,0 +1,2 @@
|
||||
// Empty Service Worker implementation. To use your own Service Worker, set the PWA_SERVICE_WORKER_PATH variable in
|
||||
// settings.py
|
||||
0
pwa/templatetags/__init__.py
Normal file
0
pwa/templatetags/__init__.py
Normal file
25
pwa/templatetags/pwa.py
Normal file
25
pwa/templatetags/pwa.py
Normal file
@@ -0,0 +1,25 @@
|
||||
import json
|
||||
|
||||
from django import template
|
||||
from django.core.serializers.json import DjangoJSONEncoder
|
||||
from django.utils.safestring import mark_safe
|
||||
|
||||
from .. import app_settings
|
||||
|
||||
register = template.Library()
|
||||
|
||||
|
||||
@register.filter(is_safe=True)
|
||||
def js(obj):
|
||||
""" Transform a python object so it can be safely used in javascript/JSON. """
|
||||
return mark_safe(json.dumps(obj, cls=DjangoJSONEncoder))
|
||||
|
||||
|
||||
@register.inclusion_tag('pwa.html', takes_context=True)
|
||||
def progressive_web_app_meta(context):
|
||||
# Pass all PWA_* settings into the template
|
||||
return {
|
||||
setting_name: getattr(app_settings, setting_name)
|
||||
for setting_name in dir(app_settings)
|
||||
if setting_name.startswith('PWA_')
|
||||
}
|
||||
8
pwa/urls.py
Normal file
8
pwa/urls.py
Normal file
@@ -0,0 +1,8 @@
|
||||
from django.conf.urls import url
|
||||
from .views import manifest, service_worker
|
||||
|
||||
# Serve up serviceworker.js and manifest.json at the root
|
||||
urlpatterns = [
|
||||
url('^serviceworker.js$', service_worker),
|
||||
url('^manifest.json$', manifest)
|
||||
]
|
||||
17
pwa/views.py
Normal file
17
pwa/views.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from django.http import HttpResponse
|
||||
from django.shortcuts import render
|
||||
|
||||
from . import app_settings
|
||||
|
||||
|
||||
def service_worker(request):
|
||||
response = HttpResponse(open(app_settings.PWA_SERVICE_WORKER_PATH).read(), content_type='application/javascript')
|
||||
return response
|
||||
|
||||
|
||||
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_')
|
||||
})
|
||||
Reference in New Issue
Block a user