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( path(
"<service_uuid>/<identifier>/script.js", "<service_uuid>/<identifier>/script.js",
ingress.ScriptView.as_view(), 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); Math.random().toString(36).substring(2, 15);
function sendUpdate() { function sendUpdate() {
var xhr = new XMLHttpRequest(); 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.setRequestHeader("Content-Type", "application/json");
xhr.send( xhr.send(
JSON.stringify({ JSON.stringify({

View File

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

View File

@ -29,11 +29,3 @@ class DateRangeMixin:
data["start_date"] = self.get_start_date() data["start_date"] = self.get_start_date()
data["end_date"] = self.get_end_date() data["end_date"] = self.get_end_date()
return data 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 %} {% block card %}
<form class="login" method="POST" action="{% url 'account_login' %}"> <form class="login" method="POST" action="{% url 'account_login' %}">
<aside class="aside ~info mb-4"> <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> </aside>
{% csrf_token %} {% csrf_token %}
{{ form|a17t }} {{ form|a17t }}

View File

@ -4,7 +4,7 @@
<html> <html>
<head> <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="robots" content="noindex">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
{% include 'a17t/head.html' %} {% include 'a17t/head.html' %}

View File

@ -21,6 +21,6 @@
{% for object in services %} {% for object in services %}
{% include 'dashboard/includes/service_overview.html' %} {% include 'dashboard/includes/service_overview.html' %}
{% empty %} {% 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 %} {% endfor %}
{% endblock %} {% endblock %}

View File

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

View File

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

View File

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

View File

@ -19,7 +19,7 @@ from django.urls import include, path
urlpatterns = [ urlpatterns = [
path("admin/", admin.site.urls), path("admin/", admin.site.urls),
path("accounts/", include("allauth.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("dashboard/", include(("dashboard.urls", "dashboard"), namespace="dashboard")),
path("", include(("core.urls", "core"), namespace="core")), path("", include(("core.urls", "core"), namespace="core")),
] ]