Skip to content

Commit b936ebd

Browse files
committed
Working solution
1 parent aee45b7 commit b936ebd

File tree

13 files changed

+51
-30
lines changed

13 files changed

+51
-30
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,4 @@ backend/schema.graphql
137137
backend/__pypackages__/
138138
backend/custom_admin/.astro/
139139
backend/custom_admin/core.*
140+
core.*

backend/custom_admin/context_processors.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,3 @@
33

44
def admin_settings(request):
55
return {"CURRENT_ENV": settings.ENVIRONMENT}
6-
7-
8-
def astro_settings(request):
9-
return {
10-
"ASTRO_URL": request.build_absolute_uri("/astro"),
11-
"APOLLO_GRAPHQL_URL": "/admin/graphql",
12-
}

backend/custom_admin/src/components/shared/rich-editor/index.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { Card } from "@radix-ui/themes";
21
import { Color } from "@tiptap/extension-color";
32
import ListItem from "@tiptap/extension-list-item";
43
import TextAlign from "@tiptap/extension-text-align";
54
import TextStyle from "@tiptap/extension-text-style";
65
import { EditorContent, useEditor } from "@tiptap/react";
76
import StarterKit from "@tiptap/starter-kit";
7+
import clsx from "clsx";
88
import { MenuBar } from "./menu-bar";
99

1010
const extensions = [
@@ -28,9 +28,11 @@ const extensions = [
2828
export const RichEditor = ({
2929
content,
3030
onUpdate,
31+
className,
3132
}: {
3233
content: string;
3334
onUpdate: (content: string) => void;
35+
className?: string;
3436
}) => {
3537
const editor = useEditor({
3638
extensions,
@@ -40,7 +42,7 @@ export const RichEditor = ({
4042
},
4143
editorProps: {
4244
attributes: {
43-
class: "outline-none",
45+
class: clsx("outline-none", className),
4446
},
4547
},
4648
});

backend/custom_admin/src/components/widgets/rich-editor.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export const RichEditorWidget = ({ name, value }) => {
1010
<RichEditor
1111
content={value}
1212
onUpdate={(content) => setUpdatedValue(content)}
13+
className="[&>*]:!text-base"
1314
/>
1415

1516
<input type="hidden" name={name} value={updatedValue} />

backend/custom_admin/src/pages/widgets/rich-editor.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ import { RichEditorWidget } from "../../components/widgets/rich-editor";
55
---
66

77

8-
<RichEditorWidget name="{{widget.name|escapejs}}" value="{{widget.value|escapejs}}" client:only />
8+
<RichEditorWidget name="{{widget.name|escapejs}}" value="{{widget.value|empty_string_if_none|escapejs}}" client:only />

backend/custom_admin/template_backends.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from functools import cached_property
12
import requests
23
from django.template.backends.django import (
34
DjangoTemplates,
@@ -54,6 +55,7 @@ def get_template(self, template_name):
5455

5556
text = _proxy_to_astro(template_name)
5657

58+
# form renderer and django template renderer have some differences
5759
if hasattr(self, "from_string"):
5860
return self.from_string(text)
5961

@@ -67,15 +69,30 @@ def __init__(self, *args, **kwargs):
6769
astro_loader_path = "custom_admin.template_backends.AstroContentLoader"
6870

6971
if settings.DEBUG:
72+
# in debugging we don't want to cache the content
73+
# so we put the astro loader first
7074
self.engine.loaders = [astro_loader_path] + self.engine.loaders
7175
else:
7276
# When running in production, put the astro loader
73-
# in the Cached loader
77+
# in the Cached loader so we don't read the file every time
78+
# structure is: (cached_loader, [astro_loader, ... other loaders])
7479
self.engine.loaders[0] = (
7580
self.engine.loaders[0][0],
7681
[astro_loader_path] + self.engine.loaders[0][1],
7782
)
7883

7984

8085
class FormRenderer(GetTemplate, FormsDjangoTemplates):
81-
...
86+
@cached_property
87+
def engine(self):
88+
# Ugly hack because the form renderer is hardcoded here:
89+
# https://github.com/django/django/blob/fcd9d08379a2aee3b2c49eab0d0b8db6fd66d091/django/forms/renderers.py#L43
90+
# So we need to override the engine property to include the builtins
91+
engine = super().engine
92+
engine.engine.builtins = (
93+
engine.engine.builtins + settings.TEMPLATES[0]["OPTIONS"]["builtins"]
94+
)
95+
engine.engine.template_builtins = engine.engine.get_template_builtins(
96+
engine.engine.builtins
97+
)
98+
return engine

backend/custom_admin/templates/admin/pycon_base.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
width: 9px;
2121
height: 6px;
2222
}
23+
24+
p {
25+
line-height: var(--default-line-height);
26+
}
2327
</style>
2428
{% endblock extrastyle %} {% block userlinks %}
2529
<div
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from django import template
2+
3+
register = template.Library()
4+
5+
6+
@register.filter
7+
def empty_string_if_none(value):
8+
return "" if value is None else value
Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
1-
from custom_admin.context_processors import admin_settings, astro_settings
1+
from custom_admin.context_processors import admin_settings
22

33

44
def test_admin_settings(settings, rf):
55
settings.ENVIRONMENT = "test"
66

77
output = admin_settings(rf.get("/"))
88
assert output == {"CURRENT_ENV": "test"}
9-
10-
11-
def test_astro_settings(rf):
12-
request = rf.get("/")
13-
output = astro_settings(request)
14-
assert output == {
15-
"ASTRO_URL": "http://testserver/astro",
16-
"APOLLO_GRAPHQL_URL": "/admin/graphql",
17-
}

backend/notifications/admin/admins.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ class EmailTemplateAdmin(ConferencePermissionMixin, admin.ModelAdmin):
5353

5454
def formfield_for_dbfield(self, db_field, **kwargs):
5555
if db_field.name == "body":
56-
kwargs["widget"] = RichEditorWidget(attrs={"rows": 50, "cols": 200})
56+
kwargs["widget"] = RichEditorWidget()
5757

5858
if db_field.name in ("subject", "preview_text"):
59-
kwargs["widget"] = RichEditorWidget(attrs={"rows": 2, "cols": 200})
59+
kwargs["widget"] = Textarea(attrs={"rows": 2, "cols": 200})
6060

6161
return super().formfield_for_dbfield(db_field, **kwargs)
6262

0 commit comments

Comments
 (0)