Use sites framework to get current site

This commit is contained in:
R. Miles McCain 2020-04-14 17:21:54 -04:00
parent 579c162365
commit 6d542e81e5
No known key found for this signature in database
GPG Key ID: 91CB47BDDF2671A5
11 changed files with 27 additions and 20 deletions

View File

@ -18,6 +18,6 @@ urlpatterns = [
path(
"<service_uuid>/<identifier>/script.js",
ingress.ScriptView.as_view(),
name="endpoint_pixel_id",
name="endpoint_script_id",
),
]

View File

@ -4,7 +4,7 @@ window.onload = function () {
Math.random().toString(36).substring(2, 15);
function sendUpdate() {
var xhr = new XMLHttpRequest();
xhr.open("POST", "{{endpoint}}", true);
xhr.open("POST", self.location.protocol + "//{{request.site.domain|default:request.META.HTTP_HOST}}{{endpoint}}", true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(
JSON.stringify({

View File

@ -2,7 +2,7 @@ import base64
import json
from django.http import HttpResponse
from django.shortcuts import render
from django.shortcuts import render, reverse
from django.utils import timezone
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt
@ -64,10 +64,24 @@ class ScriptView(View):
return resp
def get(self, *args, **kwargs):
endpoint = (
reverse(
"ingress:endpoint_script",
kwargs={"service_uuid": self.kwargs.get("service_uuid"),},
)
if self.kwargs.get("identifier") == None
else reverse(
"ingress:endpoint_script_id",
kwargs={
"service_uuid": self.kwargs.get("service_uuid"),
"identifier": self.kwargs.get("identifier"),
},
)
)
return render(
self.request,
"analytics/scripts/page.js",
context={"endpoint": self.request.build_absolute_uri()},
context={"endpoint": endpoint},
content_type="application/javascript",
)

View File

@ -29,11 +29,3 @@ class DateRangeMixin:
data["start_date"] = self.get_start_date()
data["end_date"] = self.get_end_date()
return data
class BaseUrlMixin:
def get_context_data(self, **kwargs):
data = super().get_context_data(**kwargs)
url_data = urlparse(self.request.build_absolute_uri())
data["base_url"] = f"{url_data.scheme}://{url_data.netloc}"
return data

View File

@ -9,7 +9,7 @@
{% block card %}
<form class="login" method="POST" action="{% url 'account_login' %}">
<aside class="aside ~info mb-4">
<p>Welcome to Shynet, a self-hosted analytics tool that's open source and privacy conscious. View the <a href="https://github.com/milesmcc/shynet">source code</a>.</p>
<p>Welcome to {{request.site.name|default:"Shynet"}}, a self-hosted analytics tool that's open source and privacy conscious. Powered by Shynet. View the <a href="https://github.com/milesmcc/shynet">source code</a>.</p>
</aside>
{% csrf_token %}
{{ form|a17t }}

View File

@ -4,7 +4,7 @@
<html>
<head>
<title>{% block head_title %}Privacy-oriented analytics{% endblock %} | Shynet</title>
<title>{% block head_title %}Privacy-oriented analytics{% endblock %} | {{request.site.name}}</title>
<meta name="robots" content="noindex">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
{% include 'a17t/head.html' %}

View File

@ -21,6 +21,6 @@
{% for object in services %}
{% include 'dashboard/includes/service_overview.html' %}
{% empty %}
<p>You don't have any services on this Shynet instance yet.</p>
<p>You don't have any services on {{request.site.name|default:"Shynet"}} yet.</p>
{% endfor %}
{% endblock %}

View File

@ -2,8 +2,8 @@
{% block content %}
<section class="content">
<h2>Shynet Analytics</h2>
<p>Eventually, more information about Shynet will be available here.</p>
<h2>{{request.site.name}} Analytics</h2>
<p>{{request.site.name}} uses Shynet. Eventually, more information about Shynet will be available here.</p>
<a href="{% url 'account_login' %}" class="button ~urge !high">Log In</a>
</section>
{% endblock %}

View File

@ -15,7 +15,7 @@ from django.db.models import Q
from analytics.models import Session
from .forms import ServiceForm
from .mixins import BaseUrlMixin, DateRangeMixin
from .mixins import DateRangeMixin
from core.models import Service
class DashboardView(LoginRequiredMixin, DateRangeMixin, TemplateView):
@ -64,7 +64,7 @@ class ServiceView(
class ServiceUpdateView(
LoginRequiredMixin, PermissionRequiredMixin, BaseUrlMixin, UpdateView
LoginRequiredMixin, PermissionRequiredMixin, UpdateView
):
model = Service
form_class = ServiceForm

View File

@ -59,6 +59,7 @@ MIDDLEWARE = [
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.sites.middleware.CurrentSiteMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]

View File

@ -19,7 +19,7 @@ from django.urls import include, path
urlpatterns = [
path("admin/", admin.site.urls),
path("accounts/", include("allauth.urls")),
path("ingress/", include("analytics.ingress_urls"), name="ingress"),
path("ingress/", include(("analytics.ingress_urls", "ingress")), name="ingress"),
path("dashboard/", include(("dashboard.urls", "dashboard"), namespace="dashboard")),
path("", include(("core.urls", "core"), namespace="core")),
]