Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #16: Better way to store webpush data in DOM #18

Merged
merged 1 commit into from
Oct 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions webpush/static/webpush/webpush.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,14 @@ function postSubscribeObj(statusType, subscription) {
// and the push subscription endpoint + key the server needs
// to send push messages

var element = document.getElementById('webpush-data'),
push_url = element.dataset.url,
browser = navigator.userAgent.match(/(firefox|msie|chrome|safari|trident)/ig)[0].toLowerCase(),
var browser = navigator.userAgent.match(/(firefox|msie|chrome|safari|trident)/ig)[0].toLowerCase(),
data = { status_type: statusType,
subscription: subscription.toJSON(),
browser: browser,
group: element.dataset.group
group: subBtn.dataset.group
};

fetch(push_url, {
fetch(subBtn.dataset.url, {
method: 'post',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(data),
Expand Down
6 changes: 1 addition & 5 deletions webpush/templates/webpush.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
{% if request.user.is_authenticated or group %}

{% load staticfiles %}
<script id="webpush-js" type="text/javascript" src="{% static 'webpush/webpush.js' %}"></script>

<script id="webpush-js" type="text/javascript" src="{% static 'webpush/webpush.js' %}"></script>
<script id="service-worker-js" type="text/javascript" src="{% static 'webpush/webpush_serviceworker.js' %}"></script>

<link rel="manifest" href="{% url 'webpush_manifest_json' %}">

<div id="webpush-data" {% if group %} data-group="{{ group }}" {% endif %} data-url="{{ url }}" hidden></div>
{% endif %}
6 changes: 4 additions & 2 deletions webpush/templates/webpush_button.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{% if request.user.is_authenticated or group %}
<button id="webpush-subscribe-button"> Subscribe to Push Messaging </button>
<button id="webpush-subscribe-button" {% if group %}data-group="{{ group }}"{% endif %} data-url="{{ url }}">
Subscribe to Push Messaging
</button>
<div id="webpush-message" hidden></div>
{% endif %}
{% endif %}
24 changes: 7 additions & 17 deletions webpush/templatetags/webpush_notifications.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from django import template
from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist
from django.core.urlresolvers import reverse

register = template.Library()
Expand All @@ -9,23 +7,15 @@
@register.filter
@register.inclusion_tag('webpush.html', takes_context=True)
def webpush(context):
group = context.get("webpush", {}).get("group")

url = reverse('save_webpush_info')
request = context["request"]

return { 'group': group,
'url': url,
"request": request
}
group = context.get('webpush', {}).get('group')
request = context['request']
return {'group': group, 'request': request}


@register.filter
@register.inclusion_tag('webpush_button.html', takes_context=True)
def webpush_button(context):
group = context.get("webpush", {}).get("group")
request = context["request"]

return {"group": group,
"request": request
}
group = context.get('webpush', {}).get('group')
url = reverse('save_webpush_info')
request = context['request']
return {'group': group, 'url': url, 'request': request}