Preliminary monitoring
This commit is contained in:
0
shynet/analytics/__init__.py
Normal file
0
shynet/analytics/__init__.py
Normal file
9
shynet/analytics/admin.py
Normal file
9
shynet/analytics/admin.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from django.contrib import admin
|
||||
|
||||
from .models import Hit, Session
|
||||
|
||||
admin.site.register(Session)
|
||||
|
||||
admin.site.register(Hit)
|
||||
|
||||
# Register your models here.
|
||||
5
shynet/analytics/apps.py
Normal file
5
shynet/analytics/apps.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class AnalyticsConfig(AppConfig):
|
||||
name = "analytics"
|
||||
23
shynet/analytics/ingress_urls.py
Normal file
23
shynet/analytics/ingress_urls.py
Normal file
@@ -0,0 +1,23 @@
|
||||
from django.contrib import admin
|
||||
from django.urls import include, path
|
||||
|
||||
from .views import ingress
|
||||
|
||||
urlpatterns = [
|
||||
path(
|
||||
"<service_uuid>/pixel.gif", ingress.PixelView.as_view(), name="endpoint_pixel"
|
||||
),
|
||||
path(
|
||||
"<service_uuid>/script.js", ingress.ScriptView.as_view(), name="endpoint_script"
|
||||
),
|
||||
path(
|
||||
"<service_uuid>/<identifier>/pixel.gif",
|
||||
ingress.PixelView.as_view(),
|
||||
name="endpoint_pixel_id",
|
||||
),
|
||||
path(
|
||||
"<service_uuid>/<identifier>/script.js",
|
||||
ingress.ScriptView.as_view(),
|
||||
name="endpoint_pixel_id",
|
||||
),
|
||||
]
|
||||
65
shynet/analytics/migrations/0001_initial.py
Normal file
65
shynet/analytics/migrations/0001_initial.py
Normal file
@@ -0,0 +1,65 @@
|
||||
# Generated by Django 3.0.5 on 2020-04-10 06:58
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
import analytics.models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = []
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name="Hit",
|
||||
fields=[
|
||||
(
|
||||
"id",
|
||||
models.AutoField(
|
||||
auto_created=True,
|
||||
primary_key=True,
|
||||
serialize=False,
|
||||
verbose_name="ID",
|
||||
),
|
||||
),
|
||||
("start", models.DateTimeField(auto_now_add=True)),
|
||||
("duration", models.FloatField(default=0.0)),
|
||||
("heartbeats", models.IntegerField(default=0)),
|
||||
("tracker", models.TextField()),
|
||||
("location", models.TextField(blank=True)),
|
||||
("referrer", models.TextField(blank=True)),
|
||||
("loadTime", models.FloatField(null=True)),
|
||||
("httpStatus", models.IntegerField(null=True)),
|
||||
("metadata_raw", models.TextField()),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name="Session",
|
||||
fields=[
|
||||
(
|
||||
"uuid",
|
||||
models.UUIDField(
|
||||
default=analytics.models._default_uuid,
|
||||
primary_key=True,
|
||||
serialize=False,
|
||||
),
|
||||
),
|
||||
("identifier", models.TextField(blank=True)),
|
||||
("first_seen", models.DateTimeField(auto_now_add=True)),
|
||||
("last_seen", models.DateTimeField(auto_now_add=True)),
|
||||
("user_agent", models.TextField()),
|
||||
("browser", models.TextField()),
|
||||
("device", models.TextField()),
|
||||
("os", models.TextField()),
|
||||
("ip", models.GenericIPAddressField()),
|
||||
("asn", models.TextField(blank=True)),
|
||||
("country", models.TextField(blank=True)),
|
||||
("longitude", models.FloatField(null=True)),
|
||||
("latitude", models.FloatField(null=True)),
|
||||
("time_zone", models.TextField(blank=True)),
|
||||
("metadata_raw", models.TextField()),
|
||||
],
|
||||
),
|
||||
]
|
||||
31
shynet/analytics/migrations/0002_auto_20200410_0258.py
Normal file
31
shynet/analytics/migrations/0002_auto_20200410_0258.py
Normal file
@@ -0,0 +1,31 @@
|
||||
# Generated by Django 3.0.5 on 2020-04-10 06:58
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
("core", "0001_initial"),
|
||||
("analytics", "0001_initial"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name="session",
|
||||
name="service",
|
||||
field=models.ForeignKey(
|
||||
on_delete=django.db.models.deletion.CASCADE, to="core.Service"
|
||||
),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name="hit",
|
||||
name="session",
|
||||
field=models.ForeignKey(
|
||||
on_delete=django.db.models.deletion.CASCADE, to="analytics.Session"
|
||||
),
|
||||
),
|
||||
]
|
||||
0
shynet/analytics/migrations/__init__.py
Normal file
0
shynet/analytics/migrations/__init__.py
Normal file
72
shynet/analytics/models.py
Normal file
72
shynet/analytics/models.py
Normal file
@@ -0,0 +1,72 @@
|
||||
import json
|
||||
import uuid
|
||||
|
||||
from django.db import models
|
||||
|
||||
from core.models import Service
|
||||
|
||||
|
||||
def _default_uuid():
|
||||
return str(uuid.uuid4())
|
||||
|
||||
|
||||
class Session(models.Model):
|
||||
uuid = models.UUIDField(default=_default_uuid, primary_key=True)
|
||||
service = models.ForeignKey(Service, on_delete=models.CASCADE)
|
||||
|
||||
# Cross-session identification; optional, and provided by the service
|
||||
identifier = models.TextField(blank=True)
|
||||
|
||||
# Time
|
||||
first_seen = models.DateTimeField(auto_now_add=True)
|
||||
last_seen = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
# Core request information
|
||||
user_agent = models.TextField()
|
||||
browser = models.TextField()
|
||||
device = models.TextField()
|
||||
os = models.TextField()
|
||||
ip = models.GenericIPAddressField()
|
||||
|
||||
# GeoIP data
|
||||
asn = models.TextField(blank=True)
|
||||
country = models.TextField(blank=True)
|
||||
longitude = models.FloatField(null=True)
|
||||
latitude = models.FloatField(null=True)
|
||||
time_zone = models.TextField(blank=True)
|
||||
|
||||
# Additional metadata, stored as JSON string
|
||||
metadata_raw = models.TextField()
|
||||
|
||||
@property
|
||||
def metadata(self):
|
||||
try:
|
||||
return json.loads(self.metadata_raw)
|
||||
except: # Metadata is not crucial; in the case of a read error, just ignore it
|
||||
return {}
|
||||
|
||||
|
||||
class Hit(models.Model):
|
||||
session = models.ForeignKey(Session, on_delete=models.CASCADE)
|
||||
|
||||
# Base request information
|
||||
start = models.DateTimeField(auto_now_add=True)
|
||||
duration = models.FloatField(default=0.0) # Seconds spent on page
|
||||
heartbeats = models.IntegerField(default=0)
|
||||
tracker = models.TextField() # Tracking pixel or JS
|
||||
|
||||
# Advanced page information
|
||||
location = models.TextField(blank=True)
|
||||
referrer = models.TextField(blank=True)
|
||||
loadTime = models.FloatField(null=True)
|
||||
httpStatus = models.IntegerField(null=True)
|
||||
|
||||
# Additional metadata, stored as JSON string
|
||||
metadata_raw = models.TextField()
|
||||
|
||||
@property
|
||||
def metadata(self):
|
||||
try:
|
||||
return json.loads(self.metadata_raw)
|
||||
except: # Metadata is not crucial; in the case of a read error, just ignore it
|
||||
return {}
|
||||
125
shynet/analytics/tasks.py
Normal file
125
shynet/analytics/tasks.py
Normal file
@@ -0,0 +1,125 @@
|
||||
import json
|
||||
import logging
|
||||
|
||||
import geoip2.database
|
||||
import user_agents
|
||||
from celery import shared_task
|
||||
from django.conf import settings
|
||||
from django.core.cache import cache
|
||||
from django.utils import timezone
|
||||
|
||||
from core.models import Service
|
||||
|
||||
from .models import Hit, Session
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
_geoip2_city_reader = None
|
||||
_geoip2_asn_reader = None
|
||||
|
||||
|
||||
def _geoip2_lookup(ip):
|
||||
global _geoip2_city_reader, _geoip2_asn_reader # TODO: is there a better way to do global Django vars? Is this thread safe?
|
||||
try:
|
||||
if settings.MAXMIND_CITY_DB == None or settings.MAXMIND_ASN_DB == None:
|
||||
return None
|
||||
if _geoip2_city_reader == None or _geoip2_asn_reader == None:
|
||||
_geoip2_city_reader = geoip2.database.Reader(settings.MAXMIND_CITY_DB)
|
||||
_geoip2_asn_reader = geoip2.database.Reader(settings.MAXMIND_ASN_DB)
|
||||
city_results = _geoip2_city_reader.city(ip)
|
||||
asn_results = _geoip2_asn_reader.asn(ip)
|
||||
return {
|
||||
"asn": asn_results.autonomous_system_organization,
|
||||
"country": city_results.country.iso_code,
|
||||
"longitude": city_results.location.longitude,
|
||||
"latitude": city_results.location.latitude,
|
||||
"time_zone": city_results.location.time_zone,
|
||||
}
|
||||
except geoip2.errors.AddressNotFoundError:
|
||||
return {}
|
||||
|
||||
|
||||
@shared_task
|
||||
def ingress_request(
|
||||
service_uuid, tracker, time, payload, ip, location, user_agent, identifier=""
|
||||
):
|
||||
try:
|
||||
ip_data = _geoip2_lookup(ip)
|
||||
|
||||
service = Service.objects.get(uuid=service_uuid)
|
||||
log.debug(f"Linked to service {service}")
|
||||
|
||||
# Create or update session
|
||||
session_metadata = payload.get("sessionMetadata", {})
|
||||
session = Session.objects.filter(
|
||||
service=service,
|
||||
last_seen__gt=timezone.now() - timezone.timedelta(minutes=30),
|
||||
ip=ip,
|
||||
user_agent=user_agent,
|
||||
identifier=identifier,
|
||||
).first()
|
||||
if session is None:
|
||||
log.debug("Cannot link to existing session; creating a new one...")
|
||||
ua = user_agents.parse(user_agent)
|
||||
|
||||
session = Session.objects.create(
|
||||
service=service,
|
||||
ip=ip,
|
||||
user_agent=user_agent,
|
||||
identifier=identifier,
|
||||
browser=f"{ua.browser.family or ''} {ua.browser.version_string or ''}".strip(),
|
||||
device=f"{ua.device.model or ''}",
|
||||
os=f"{ua.os.family or ''} {ua.os.version_string or ''}".strip(),
|
||||
metadata_raw=json.dumps(session_metadata),
|
||||
asn=ip_data.get("asn", ""),
|
||||
country=ip_data.get("country", ""),
|
||||
longitude=ip_data.get("longitude"),
|
||||
latitude=ip_data.get("latitude"),
|
||||
time_zone=ip_data.get("time_zone", ""),
|
||||
)
|
||||
else:
|
||||
log.debug("Updating old session with new data...")
|
||||
# Update old metadata with new metadata
|
||||
new_metadata = session.metadata
|
||||
new_metadata.update(session_metadata)
|
||||
session.metadata_raw = json.dumps(new_metadata)
|
||||
# Update last seen time
|
||||
session.last_seen = timezone.now()
|
||||
session.save()
|
||||
|
||||
# Create or update hit
|
||||
hit_metadata = payload.get("hitMetadata", {})
|
||||
idempotency = payload.get("idempotency")
|
||||
idempotency_path = f"hit_idempotency_{idempotency}"
|
||||
hit = None
|
||||
if idempotency is not None:
|
||||
if cache.get(idempotency_path) is not None:
|
||||
hit = Hit.objects.filter(
|
||||
pk=cache.get(idempotency_path), session=session
|
||||
).first()
|
||||
if hit is not None:
|
||||
# There is an existing hit with an identical idempotency key. That means
|
||||
# this is a heartbeat.
|
||||
log.debug("Hit is a heartbeat; updating old hit with new data...")
|
||||
hit.heartbeats += 1
|
||||
hit.duration = (timezone.now() - hit.start).total_seconds()
|
||||
new_metadata = hit.metadata
|
||||
new_metadata.update(hit_metadata)
|
||||
hit.metadata_raw = json.dumps(new_metadata)
|
||||
hit.save()
|
||||
if hit is None:
|
||||
log.debug("Hit is a page load; creating new hit...")
|
||||
# There is no existing hit; create a new one
|
||||
hit = Hit.objects.create(
|
||||
session=session,
|
||||
tracker=tracker,
|
||||
location=location,
|
||||
referrer=payload.get("referrer", ""),
|
||||
loadTime=payload.get("loadTime"),
|
||||
metadata_raw=json.dumps(hit_metadata),
|
||||
)
|
||||
# Set idempotency (if applicable)
|
||||
if idempotency is not None:
|
||||
cache.set(idempotency_path, hit.pk, timeout=30 * 60)
|
||||
except Exception as e:
|
||||
log.error(e)
|
||||
27
shynet/analytics/templates/analytics/scripts/page.js
Normal file
27
shynet/analytics/templates/analytics/scripts/page.js
Normal file
@@ -0,0 +1,27 @@
|
||||
window.onload = function () {
|
||||
var idempotency =
|
||||
Math.random().toString(36).substring(2, 15) +
|
||||
Math.random().toString(36).substring(2, 15);
|
||||
function sendUpdate() {
|
||||
var xhr = new XMLHttpRequest();
|
||||
xhr.open("POST", "{{endpoint}}", true);
|
||||
xhr.setRequestHeader("Content-Type", "application/json");
|
||||
xhr.send(
|
||||
JSON.stringify({
|
||||
idempotency: idempotency,
|
||||
referrer: document.referrer,
|
||||
loadTime:
|
||||
window.performance.timing.domContentLoadedEventEnd -
|
||||
window.performance.timing.navigationStart,
|
||||
hitMetadata:
|
||||
typeof shynetHitMetadata !== "undefined" ? shynetHitMetadata : {},
|
||||
sessionMetadata:
|
||||
typeof shynetSessionMetadata !== "undefined"
|
||||
? shynetSessionMetadata
|
||||
: {},
|
||||
})
|
||||
);
|
||||
}
|
||||
setInterval(sendUpdate, 5000);
|
||||
sendUpdate();
|
||||
};
|
||||
3
shynet/analytics/tests.py
Normal file
3
shynet/analytics/tests.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
0
shynet/analytics/views/__init__.py
Normal file
0
shynet/analytics/views/__init__.py
Normal file
85
shynet/analytics/views/ingress.py
Normal file
85
shynet/analytics/views/ingress.py
Normal file
@@ -0,0 +1,85 @@
|
||||
import base64
|
||||
import json
|
||||
|
||||
from django.http import HttpResponse
|
||||
from django.shortcuts import render
|
||||
from django.utils import timezone
|
||||
from django.utils.decorators import method_decorator
|
||||
from django.views.decorators.csrf import csrf_exempt
|
||||
from django.views.generic import TemplateView, View
|
||||
from ipware import get_client_ip
|
||||
|
||||
from ..tasks import ingress_request
|
||||
|
||||
|
||||
def ingress(request, service_uuid, identifier, tracker, payload):
|
||||
time = timezone.now()
|
||||
client_ip, is_routable = get_client_ip(request)
|
||||
location = request.META.get("HTTP_REFERER")
|
||||
user_agent = request.META.get("HTTP_USER_AGENT")
|
||||
|
||||
ingress_request.delay(
|
||||
service_uuid,
|
||||
tracker,
|
||||
time,
|
||||
payload,
|
||||
client_ip,
|
||||
location,
|
||||
user_agent,
|
||||
identifier,
|
||||
)
|
||||
|
||||
|
||||
class PixelView(View):
|
||||
# Fallback view to serve an unobtrusive 1x1 transparent tracking pixel for browsers with
|
||||
# JavaScript disabled.
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
# Extract primary data
|
||||
ingress(
|
||||
request,
|
||||
self.kwargs.get("service_uuid"),
|
||||
self.kwargs.get("identifier", ""),
|
||||
"PIXEL",
|
||||
{},
|
||||
)
|
||||
|
||||
data = base64.b64decode(
|
||||
"R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="
|
||||
)
|
||||
resp = HttpResponse(data, content_type="image/gif")
|
||||
resp["Cache-Control"] = "no-cache"
|
||||
resp["Access-Control-Allow-Origin"] = "*"
|
||||
return resp
|
||||
|
||||
|
||||
@method_decorator(csrf_exempt, name="dispatch")
|
||||
class ScriptView(View):
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
resp = super().dispatch(request, *args, **kwargs)
|
||||
resp["Access-Control-Allow-Origin"] = "*"
|
||||
resp["Access-Control-Allow-Methods"] = "GET,HEAD,OPTIONS,POST"
|
||||
resp[
|
||||
"Access-Control-Allow-Headers"
|
||||
] = "Origin, X-Requested-With, Content-Type, Accept, Authorization, Referer"
|
||||
return resp
|
||||
|
||||
def get(self, *args, **kwargs):
|
||||
return render(
|
||||
self.request,
|
||||
"analytics/scripts/page.js",
|
||||
context={"endpoint": self.request.build_absolute_uri()},
|
||||
content_type="application/javascript",
|
||||
)
|
||||
|
||||
def post(self, *args, **kwargs):
|
||||
payload = json.loads(self.request.body)
|
||||
ingress(
|
||||
self.request,
|
||||
self.kwargs.get("service_uuid"),
|
||||
self.kwargs.get("identifier", ""),
|
||||
"JS",
|
||||
payload,
|
||||
)
|
||||
return HttpResponse(
|
||||
json.dumps({"status": "OK"}), content_type="application/json"
|
||||
)
|
||||
@@ -1,6 +1,7 @@
|
||||
from django.contrib import admin
|
||||
from django.contrib.auth.admin import UserAdmin
|
||||
from .models import User, Service
|
||||
|
||||
from .models import Service, User
|
||||
|
||||
admin.site.register(User, UserAdmin)
|
||||
admin.site.register(Service)
|
||||
admin.site.register(Service)
|
||||
|
||||
@@ -2,4 +2,4 @@ from django.apps import AppConfig
|
||||
|
||||
|
||||
class CoreConfig(AppConfig):
|
||||
name = 'core'
|
||||
name = "core"
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
# Generated by Django 3.0.5 on 2020-04-09 19:01
|
||||
# Generated by Django 3.0.5 on 2020-04-10 06:58
|
||||
|
||||
from django.conf import settings
|
||||
import django.contrib.auth.models
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
import django.utils.timezone
|
||||
import uuid
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
|
||||
import core.models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
@@ -13,47 +14,146 @@ class Migration(migrations.Migration):
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
('auth', '0011_update_proxy_permissions'),
|
||||
("auth", "0011_update_proxy_permissions"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='User',
|
||||
name="User",
|
||||
fields=[
|
||||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('password', models.CharField(max_length=128, verbose_name='password')),
|
||||
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
|
||||
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
|
||||
('first_name', models.CharField(blank=True, max_length=30, verbose_name='first name')),
|
||||
('last_name', models.CharField(blank=True, max_length=150, verbose_name='last name')),
|
||||
('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')),
|
||||
('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')),
|
||||
('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')),
|
||||
('username', models.TextField(default=uuid.uuid4, unique=True)),
|
||||
('email', models.EmailField(max_length=254, unique=True)),
|
||||
('verified', models.BooleanField(default=False)),
|
||||
('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.Group', verbose_name='groups')),
|
||||
('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.Permission', verbose_name='user permissions')),
|
||||
(
|
||||
"id",
|
||||
models.AutoField(
|
||||
auto_created=True,
|
||||
primary_key=True,
|
||||
serialize=False,
|
||||
verbose_name="ID",
|
||||
),
|
||||
),
|
||||
("password", models.CharField(max_length=128, verbose_name="password")),
|
||||
(
|
||||
"last_login",
|
||||
models.DateTimeField(
|
||||
blank=True, null=True, verbose_name="last login"
|
||||
),
|
||||
),
|
||||
(
|
||||
"is_superuser",
|
||||
models.BooleanField(
|
||||
default=False,
|
||||
help_text="Designates that this user has all permissions without explicitly assigning them.",
|
||||
verbose_name="superuser status",
|
||||
),
|
||||
),
|
||||
(
|
||||
"first_name",
|
||||
models.CharField(
|
||||
blank=True, max_length=30, verbose_name="first name"
|
||||
),
|
||||
),
|
||||
(
|
||||
"last_name",
|
||||
models.CharField(
|
||||
blank=True, max_length=150, verbose_name="last name"
|
||||
),
|
||||
),
|
||||
(
|
||||
"is_staff",
|
||||
models.BooleanField(
|
||||
default=False,
|
||||
help_text="Designates whether the user can log into this admin site.",
|
||||
verbose_name="staff status",
|
||||
),
|
||||
),
|
||||
(
|
||||
"is_active",
|
||||
models.BooleanField(
|
||||
default=True,
|
||||
help_text="Designates whether this user should be treated as active. Unselect this instead of deleting accounts.",
|
||||
verbose_name="active",
|
||||
),
|
||||
),
|
||||
(
|
||||
"date_joined",
|
||||
models.DateTimeField(
|
||||
default=django.utils.timezone.now, verbose_name="date joined"
|
||||
),
|
||||
),
|
||||
(
|
||||
"username",
|
||||
models.TextField(default=core.models._default_uuid, unique=True),
|
||||
),
|
||||
("email", models.EmailField(max_length=254, unique=True)),
|
||||
(
|
||||
"groups",
|
||||
models.ManyToManyField(
|
||||
blank=True,
|
||||
help_text="The groups this user belongs to. A user will get all permissions granted to each of their groups.",
|
||||
related_name="user_set",
|
||||
related_query_name="user",
|
||||
to="auth.Group",
|
||||
verbose_name="groups",
|
||||
),
|
||||
),
|
||||
(
|
||||
"user_permissions",
|
||||
models.ManyToManyField(
|
||||
blank=True,
|
||||
help_text="Specific permissions for this user.",
|
||||
related_name="user_set",
|
||||
related_query_name="user",
|
||||
to="auth.Permission",
|
||||
verbose_name="user permissions",
|
||||
),
|
||||
),
|
||||
],
|
||||
options={
|
||||
'verbose_name': 'user',
|
||||
'verbose_name_plural': 'users',
|
||||
'abstract': False,
|
||||
"verbose_name": "user",
|
||||
"verbose_name_plural": "users",
|
||||
"abstract": False,
|
||||
},
|
||||
managers=[
|
||||
('objects', django.contrib.auth.models.UserManager()),
|
||||
],
|
||||
managers=[("objects", django.contrib.auth.models.UserManager()),],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='Service',
|
||||
name="Service",
|
||||
fields=[
|
||||
('uuid', models.UUIDField(primary_key=True, serialize=False)),
|
||||
('name', models.TextField(max_length=64)),
|
||||
('created', models.DateTimeField(auto_now_add=True)),
|
||||
('link', models.URLField(blank=True)),
|
||||
('status', models.CharField(choices=[('AC', 'Active'), ('AR', 'Archived')], default='AC', max_length=2)),
|
||||
('collaborators', models.ManyToManyField(related_name='collaborating_services', to=settings.AUTH_USER_MODEL)),
|
||||
('owner', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='owning_services', to=settings.AUTH_USER_MODEL)),
|
||||
(
|
||||
"uuid",
|
||||
models.UUIDField(
|
||||
default=core.models._default_uuid,
|
||||
primary_key=True,
|
||||
serialize=False,
|
||||
),
|
||||
),
|
||||
("name", models.TextField(max_length=64)),
|
||||
("created", models.DateTimeField(auto_now_add=True)),
|
||||
("link", models.URLField(blank=True)),
|
||||
("origins", models.TextField(default="*")),
|
||||
(
|
||||
"status",
|
||||
models.CharField(
|
||||
choices=[("AC", "Active"), ("AR", "Archived")],
|
||||
db_index=True,
|
||||
default="AC",
|
||||
max_length=2,
|
||||
),
|
||||
),
|
||||
(
|
||||
"collaborators",
|
||||
models.ManyToManyField(
|
||||
blank=True,
|
||||
related_name="collaborating_services",
|
||||
to=settings.AUTH_USER_MODEL,
|
||||
),
|
||||
),
|
||||
(
|
||||
"owner",
|
||||
models.ForeignKey(
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
related_name="owning_services",
|
||||
to=settings.AUTH_USER_MODEL,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
]
|
||||
|
||||
@@ -1,23 +1,37 @@
|
||||
from django.db import models
|
||||
import uuid
|
||||
|
||||
from django.contrib.auth.models import AbstractUser
|
||||
from django.db import models
|
||||
|
||||
|
||||
def _default_uuid():
|
||||
return str(uuid.uuid4())
|
||||
|
||||
|
||||
class User(AbstractUser):
|
||||
username = models.TextField(default=lambda: str(uuid.uuid4()), unique=True)
|
||||
username = models.TextField(default=_default_uuid, unique=True)
|
||||
email = models.EmailField(unique=True)
|
||||
|
||||
def __str__(self):
|
||||
return self.email
|
||||
|
||||
|
||||
class Service(models.Model):
|
||||
ACTIVE = "AC"
|
||||
ARCHIVED = "AR"
|
||||
SERVICE_STATUSES = [(ACTIVE, "Active"), (ARCHIVED, "Archived")]
|
||||
|
||||
uuid = models.UUIDField(primary_key=True)
|
||||
uuid = models.UUIDField(default=_default_uuid, primary_key=True)
|
||||
name = models.TextField(max_length=64)
|
||||
owner = models.ForeignKey(User, on_delete=models.CASCADE, related_name="owning_services")
|
||||
collaborators = models.ManyToManyField(User, related_name="collaborating_services")
|
||||
owner = models.ForeignKey(
|
||||
User, on_delete=models.CASCADE, related_name="owning_services"
|
||||
)
|
||||
collaborators = models.ManyToManyField(
|
||||
User, related_name="collaborating_services", blank=True
|
||||
)
|
||||
created = models.DateTimeField(auto_now_add=True)
|
||||
link = models.URLField(blank=True)
|
||||
status = models.CharField(max_length=2, choices=SERVICE_STATUSES, default=ACTIVE)
|
||||
origins = models.TextField(default="*")
|
||||
status = models.CharField(
|
||||
max_length=2, choices=SERVICE_STATUSES, default=ACTIVE, db_index=True
|
||||
)
|
||||
|
||||
50
shynet/core/templates/base.html
Normal file
50
shynet/core/templates/base.html
Normal file
@@ -0,0 +1,50 @@
|
||||
<!DOCTYPE html>
|
||||
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>{% block head_title %}Privacy-oriented analytics{% endblock %} | Shynet</title>
|
||||
<meta name="robots" content="noindex">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<link href="https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css" rel="stylesheet">
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/a17t@latest/dist/a17t.css">
|
||||
{% block extra_head %}
|
||||
{% endblock %}
|
||||
</head>
|
||||
|
||||
<body class="bg-gray-100 min-h-full">
|
||||
{% block body %}
|
||||
|
||||
<section class="max-w-4xl mx-auto px-6 md:py-12">
|
||||
{% if messages %}
|
||||
<div>
|
||||
{% for message in messages %}
|
||||
<article class="card {{message.tags}} mb-2 w-full">{{message}}</article>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
<div>
|
||||
<strong>Menu:</strong>
|
||||
<ul>
|
||||
{% if user.is_authenticated %}
|
||||
<li><a href="{% url 'account_email' %}">Change E-mail</a></li>
|
||||
<li><a href="{% url 'account_logout' %}">Sign Out</a></li>
|
||||
{% else %}
|
||||
<li><a href="{% url 'account_login' %}">Sign In</a></li>
|
||||
<li><a href="{% url 'account_signup' %}">Sign Up</a></li>
|
||||
{% endif %}
|
||||
</ul>
|
||||
</div>
|
||||
<main>
|
||||
{% block content %}
|
||||
{% endblock %}
|
||||
</main>
|
||||
</section>
|
||||
{% endblock %}
|
||||
{% block extra_body %}
|
||||
{% endblock %}
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -1,20 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title>{{title|default:"Privacy-oriented analytics"}} | Shynet</title>
|
||||
<meta name="robots" content="noindex">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<link href="https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css" rel="stylesheet">
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/a17t@latest/dist/a17t.css">
|
||||
</head>
|
||||
|
||||
<body class="bg-gray-100 min-h-full">
|
||||
<section class="max-w-4xl mx-auto px-6 md:py-12">
|
||||
{% block body %}
|
||||
{% endblock %}
|
||||
</section>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -1,9 +1,9 @@
|
||||
{% extends "core/base.html" %}
|
||||
{% extends "base.html" %}
|
||||
|
||||
{% block body %}
|
||||
<main class="content">
|
||||
{% block content %}
|
||||
<section class="content">
|
||||
<h2>Shynet Analytics</h2>
|
||||
<p>Eventually, more information about Shynet will be available here.</p>
|
||||
<a href="{% url 'account_login' %}" class="button ~urge !high">Log In</a>
|
||||
</main>
|
||||
</section>
|
||||
{% endblock %}
|
||||
@@ -1,8 +1,8 @@
|
||||
from django.contrib import admin
|
||||
from django.urls import path, include
|
||||
from django.urls import include, path
|
||||
|
||||
from . import views
|
||||
|
||||
urlpatterns = [
|
||||
path("", views.IndexView.as_view(), name="index"),
|
||||
]
|
||||
]
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from django.views.generic import TemplateView
|
||||
|
||||
|
||||
class IndexView(TemplateView):
|
||||
template_name = "core/pages/index.html"
|
||||
template_name = "core/pages/index.html"
|
||||
|
||||
@@ -5,7 +5,7 @@ import sys
|
||||
|
||||
|
||||
def main():
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'shynet.settings')
|
||||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "shynet.settings")
|
||||
try:
|
||||
from django.core.management import execute_from_command_line
|
||||
except ImportError as exc:
|
||||
@@ -17,5 +17,5 @@ def main():
|
||||
execute_from_command_line(sys.argv)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
from .celery import app as celery_app
|
||||
|
||||
__all__ = ("celery_app",)
|
||||
|
||||
11
shynet/shynet/celery.py
Normal file
11
shynet/shynet/celery.py
Normal file
@@ -0,0 +1,11 @@
|
||||
import os
|
||||
|
||||
from celery import Celery
|
||||
|
||||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "shynet.settings")
|
||||
|
||||
app = Celery("shynet")
|
||||
|
||||
app.config_from_object("django.conf:settings", namespace="CELERY")
|
||||
|
||||
app.autodiscover_tasks()
|
||||
@@ -38,10 +38,11 @@ INSTALLED_APPS = [
|
||||
"django.contrib.messages",
|
||||
"django.contrib.staticfiles",
|
||||
"django.contrib.sites",
|
||||
"core",
|
||||
"analytics",
|
||||
"allauth",
|
||||
"allauth.account",
|
||||
"allauth.socialaccount",
|
||||
"core",
|
||||
]
|
||||
|
||||
MIDDLEWARE = [
|
||||
@@ -134,3 +135,16 @@ ACCOUNT_USER_MODEL_USERNAME_FIELD = None
|
||||
ACCOUNT_USERNAME_REQUIRED = False
|
||||
|
||||
SITE_ID = 1
|
||||
|
||||
# Celery
|
||||
|
||||
if DEBUG:
|
||||
CELERY_TASK_ALWAYS_EAGER = True
|
||||
|
||||
CELERY_BROKER_URL = os.getenv("CELERY_BROKER_URL")
|
||||
CELERY_REDIS_SOCKET_TIMEOUT = 15
|
||||
|
||||
# GeoIP
|
||||
|
||||
MAXMIND_CITY_DB = os.getenv("MAXMIND_CITY_DB")
|
||||
MAXMIND_ASN_DB = os.getenv("MAXMIND_ASN_DB")
|
||||
|
||||
@@ -14,10 +14,11 @@ Including another URLconf
|
||||
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
|
||||
"""
|
||||
from django.contrib import admin
|
||||
from django.urls import path, include
|
||||
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("", include(("core.urls", "core"), namespace="core")),
|
||||
]
|
||||
|
||||
@@ -11,6 +11,6 @@ import os
|
||||
|
||||
from django.core.wsgi import get_wsgi_application
|
||||
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'shynet.settings')
|
||||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "shynet.settings")
|
||||
|
||||
application = get_wsgi_application()
|
||||
|
||||
Reference in New Issue
Block a user