Commit c64740f1 authored by Julien Enselme's avatar Julien Enselme

Add a view which responds in a random number of time

parent 82e8c3da
import logging
import random
import time
from django.http import Http404
from django.shortcuts import render
from django.views.generic import TemplateView
......@@ -5,6 +9,9 @@ from django.views.generic import TemplateView
from djangopwa import version
logger = logging.getLogger('djpwa.pwa.views')
def offline(request):
return render(request, 'pwa/offline.html')
......@@ -25,6 +32,14 @@ def say_something(request, key):
return render(request, 'pwa/say_something.html', context={'thing': things_to_say[key]})
def random_response(request):
response_time_ms = random.choice((0, 10, 50, 100, 1_000, 10_000))
response_time = response_time_ms / 1_000
print(f'Selected response time {response_time}')
time.sleep(response_time)
return render(request, 'pwa/random_response.html', context={'response_time': response_time})
class ServiceWorkerView(TemplateView):
template_name = 'sw.js'
content_type = 'application/javascript'
......
{% extends 'base.html' %}
{% block title %}Test PWA site – Saying something dynamic{% endblock %}
{% block body %}
<h1>Random response</h1>
<p>Responded in {{ response_time }} ms</p>
{% endblock %}
......@@ -26,6 +26,7 @@ urlpatterns = [
path('', pwa_views.my_page),
path('offline/', pwa_views.offline),
path('say-something/<str:key>', pwa_views.say_something),
path('random-response', pwa_views.random_response),
# The service worker cannot be in /static because its scope will be limited to /static.
# Since we want it to have a scope of the full application, we rely on this TemplateView
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment