combo/combo/apps/pwa/views.py

88 lines
3.0 KiB
Python

# combo - content management system
# Copyright (C) 2015-2018 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import json
from django.conf import settings
from django.http import HttpResponse, HttpResponseForbidden, Http404, JsonResponse
from django.template.loader import get_template, TemplateDoesNotExist
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import TemplateView
from .models import PushSubscription, PwaSettings
from combo import VERSION
def manifest_json(request, *args, **kwargs):
try:
template = get_template('combo/manifest.json')
except TemplateDoesNotExist:
raise Http404()
context = {
'site_base': request.build_absolute_uri('/')[:-1],
}
return HttpResponse(template.render(context, request), content_type='application/json')
def js_response(request, template_name, **kwargs):
template = get_template(template_name)
context = {
'pwa_vapid_publik_key': settings.PWA_VAPID_PUBLIK_KEY,
'pwa_notification_badge_url': settings.PWA_NOTIFICATION_BADGE_URL,
'pwa_notification_icon_url': settings.PWA_NOTIFICATION_ICON_URL,
}
context.update(kwargs)
return HttpResponse(template.render(context, request),
content_type='application/javascript; charset=utf-8')
def service_worker_js(request, *args, **kwargs):
pwa_settings = PwaSettings.singleton()
if pwa_settings.id:
version = '%s-%s' % (VERSION, pwa_settings.last_update_timestamp)
else:
version = VERSION
return js_response(request, 'combo/service-worker.js',
version=version)
def service_worker_registration_js(request, *args, **kwargs):
return js_response(request, 'combo/service-worker-registration.js')
@csrf_exempt
def subscribe_push(request, *args, **kwargs):
if not (request.user and request.user.is_authenticated()):
return HttpResponseForbidden()
if request.method != 'POST':
return HttpResponseForbidden()
subscription_data = json.loads(request.body)
if subscription_data is None:
PushSubscription.objects.filter(user=request.user).delete()
else:
subscription, created = PushSubscription.objects.get_or_create(
user=request.user,
subscription_info=subscription_data)
subscription.save()
return JsonResponse({'err': 0})
class OfflinePage(TemplateView):
template_name = 'combo/pwa/offline.html'
offline_page = OfflinePage.as_view()