Skip to content

Commit 598dcad

Browse files
committed
initial version
0 parents  commit 598dcad

18 files changed

+273
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
db.sqlite3
2+
.venv/
3+
*.pyc

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Framework Embedded Analytics in Django

charts/__init__.py

Whitespace-only changes.

charts/admin.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.contrib import admin
2+
3+
# Register your models here.

charts/apps.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.apps import AppConfig
2+
3+
4+
class ChartsConfig(AppConfig):
5+
default_auto_field = 'django.db.models.BigAutoField'
6+
name = 'charts'

charts/migrations/__init__.py

Whitespace-only changes.

charts/models.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.db import models
2+
3+
# Create your models here.

charts/templates/charts/index.html

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="utf-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1">
7+
<title>Demo Charts</title>
8+
<!-- <link rel="stylesheet" href="styles.css"> -->
9+
</head>
10+
11+
<body>
12+
<h1>Demo chart</h1>
13+
<div id="medals-chart"></div>
14+
<code>{{ medal_chart_url }}</code>
15+
16+
<script type="module">
17+
const {MedalsChart} = await import("{{ medal_chart_url }}");
18+
const target = document.querySelector("#medals-chart");
19+
target.append(await MedalsChart());
20+
</script>
21+
</body>
22+
23+
</html>

charts/tests.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.test import TestCase
2+
3+
# Create your tests here.

charts/urls.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from django.urls import path
2+
3+
from . import views
4+
5+
urlpatterns = [
6+
path("", views.index, name="index"),
7+
]

charts/views.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from urllib.parse import urlparse
2+
import datetime
3+
4+
import jwt
5+
from django.shortcuts import render
6+
from django.conf import settings
7+
8+
def index(request):
9+
signed_url = sign_embedded_chart_url("https://observablehq.observablehq.cloud/olympian-embeds/medals-chart.js")
10+
context = {
11+
'medal_chart_url': signed_url,
12+
}
13+
return render(request, "charts/index.html", context)
14+
15+
def sign_embedded_chart_url(url):
16+
parsed_url = urlparse(url)
17+
payload_data = {
18+
'sub': 'mythmon',
19+
'urn:observablehq:path': parsed_url.path,
20+
'iat': int(datetime.datetime.now().timestamp()),
21+
'nbf': int(datetime.datetime.now().timestamp()),
22+
'exp': int((datetime.datetime.now() + datetime.timedelta(minutes=15)).timestamp()),
23+
}
24+
token = jwt.encode(payload_data, settings.EMBED_PRIVATE_KEY, algorithm='EdDSA')
25+
return f"{url}?token={token}"

demo/__init__.py

Whitespace-only changes.

demo/asgi.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
ASGI config for demo project.
3+
4+
It exposes the ASGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/5.1/howto/deployment/asgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.asgi import get_asgi_application
13+
14+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'demo.settings')
15+
16+
application = get_asgi_application()

demo/settings.py

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import os
2+
from pathlib import Path
3+
4+
# Build paths inside the project like this: BASE_DIR / 'subdir'.
5+
BASE_DIR = Path(__file__).resolve().parent.parent
6+
7+
8+
# Quick-start development settings - unsuitable for production
9+
# See https://docs.djangoproject.com/en/5.1/howto/deployment/checklist/
10+
11+
# SECURITY WARNING: keep the secret key used in production secret!
12+
SECRET_KEY = 'django-insecure-=_0kx2ng=-oli85@n@ld4_)9-1u-)05sydru53wn%_*oah=@_l'
13+
14+
# SECURITY WARNING: don't run with debug turned on in production!
15+
DEBUG = True
16+
17+
ALLOWED_HOSTS = []
18+
19+
20+
# Application definition
21+
22+
INSTALLED_APPS = [
23+
'django.contrib.admin',
24+
'django.contrib.auth',
25+
'django.contrib.contenttypes',
26+
'django.contrib.sessions',
27+
'django.contrib.messages',
28+
'django.contrib.staticfiles',
29+
'charts',
30+
]
31+
32+
MIDDLEWARE = [
33+
'django.middleware.security.SecurityMiddleware',
34+
'django.contrib.sessions.middleware.SessionMiddleware',
35+
'django.middleware.common.CommonMiddleware',
36+
'django.middleware.csrf.CsrfViewMiddleware',
37+
'django.contrib.auth.middleware.AuthenticationMiddleware',
38+
'django.contrib.messages.middleware.MessageMiddleware',
39+
'django.middleware.clickjacking.XFrameOptionsMiddleware',
40+
]
41+
42+
ROOT_URLCONF = 'demo.urls'
43+
44+
TEMPLATES = [
45+
{
46+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
47+
'DIRS': [],
48+
'APP_DIRS': True,
49+
'OPTIONS': {
50+
'context_processors': [
51+
'django.template.context_processors.debug',
52+
'django.template.context_processors.request',
53+
'django.contrib.auth.context_processors.auth',
54+
'django.contrib.messages.context_processors.messages',
55+
],
56+
},
57+
},
58+
]
59+
60+
WSGI_APPLICATION = 'demo.wsgi.application'
61+
62+
63+
# Database
64+
# https://docs.djangoproject.com/en/5.1/ref/settings/#databases
65+
66+
DATABASES = {
67+
'default': {
68+
'ENGINE': 'django.db.backends.sqlite3',
69+
'NAME': BASE_DIR / 'db.sqlite3',
70+
}
71+
}
72+
73+
74+
# Password validation
75+
# https://docs.djangoproject.com/en/5.1/ref/settings/#auth-password-validators
76+
77+
AUTH_PASSWORD_VALIDATORS = [
78+
{
79+
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
80+
},
81+
{
82+
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
83+
},
84+
{
85+
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
86+
},
87+
{
88+
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
89+
},
90+
]
91+
92+
93+
# Internationalization
94+
# https://docs.djangoproject.com/en/5.1/topics/i18n/
95+
96+
LANGUAGE_CODE = 'en-us'
97+
98+
TIME_ZONE = 'UTC'
99+
100+
USE_I18N = True
101+
102+
USE_TZ = True
103+
104+
105+
# Static files (CSS, JavaScript, Images)
106+
# https://docs.djangoproject.com/en/5.1/howto/static-files/
107+
108+
STATIC_URL = 'static/'
109+
110+
# Default primary key field type
111+
# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field
112+
113+
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
114+
115+
EMBED_PRIVATE_KEY = os.environ['EMBED_PRIVATE_KEY']

demo/urls.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
URL configuration for demo project.
3+
4+
The `urlpatterns` list routes URLs to views. For more information please see:
5+
https://docs.djangoproject.com/en/5.1/topics/http/urls/
6+
Examples:
7+
Function views
8+
1. Add an import: from my_app import views
9+
2. Add a URL to urlpatterns: path('', views.home, name='home')
10+
Class-based views
11+
1. Add an import: from other_app.views import Home
12+
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
13+
Including another URLconf
14+
1. Import the include() function: from django.urls import include, path
15+
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
16+
"""
17+
from django.contrib import admin
18+
from django.urls import include, path
19+
20+
urlpatterns = [
21+
path('', include("charts.urls")),
22+
path('admin/', admin.site.urls),
23+
]

demo/wsgi.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
WSGI config for demo project.
3+
4+
It exposes the WSGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/5.1/howto/deployment/wsgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.wsgi import get_wsgi_application
13+
14+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'demo.settings')
15+
16+
application = get_wsgi_application()

manage.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env python
2+
"""Django's command-line utility for administrative tasks."""
3+
import os
4+
import sys
5+
6+
7+
def main():
8+
"""Run administrative tasks."""
9+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'demo.settings')
10+
try:
11+
from django.core.management import execute_from_command_line
12+
except ImportError as exc:
13+
raise ImportError(
14+
"Couldn't import Django. Are you sure it's installed and "
15+
"available on your PYTHONPATH environment variable? Did you "
16+
"forget to activate a virtual environment?"
17+
) from exc
18+
execute_from_command_line(sys.argv)
19+
20+
21+
if __name__ == '__main__':
22+
main()

requirements.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
asgiref==3.8.1
2+
cffi==1.17.1
3+
cryptography==43.0.3
4+
Django==5.1.2
5+
pycparser==2.22
6+
PyJWT==2.9.0
7+
sqlparse==0.5.1

0 commit comments

Comments
 (0)