Skip to content

Commit 89146d0

Browse files
authored
Merge pull request #227 from AvaCodeSolutions/feat/226/sidebar-custom-component
feat: #226 option to add custom component in sidebar
2 parents 39d9e45 + fdc20fb commit 89146d0

9 files changed

Lines changed: 57 additions & 13 deletions

File tree

django_email_learning/jobs/deliver_contents_job.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,12 @@ def run(self) -> None:
5858
)
5959

6060
def get_delivery_queue(self) -> DeliveryQueueProtocol:
61+
DJANGO_EMAIL_LEARNING_SETTINGS: dict = getattr(
62+
settings, "DJANGO_EMAIL_LEARNING", {}
63+
)
6164
try:
62-
return import_string(settings.DJANGO_EMAIL_LEARNING["DELIVERY_QUEUE"])
63-
except (AttributeError, KeyError):
65+
return import_string(DJANGO_EMAIL_LEARNING_SETTINGS["DELIVERY_QUEUE"])
66+
except KeyError:
6467
from django_email_learning.services.defaults.database_delivery_queue import (
6568
DatabaseDeliveryQueue,
6669
)

django_email_learning/models.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,12 @@ def _fernet(cls, salt: str) -> Fernet:
127127
salt=salt.encode(),
128128
iterations=100000,
129129
)
130+
DJANGO_EMAIL_LEARNING_SETTINGS: dict = getattr(
131+
settings, "DJANGO_EMAIL_LEARNING", {}
132+
)
130133
try:
131-
secret = settings.DJANGO_EMAIL_LEARNING["ENCRYPTION_SECRET_KEY"]
132-
except (AttributeError, KeyError):
134+
secret = DJANGO_EMAIL_LEARNING_SETTINGS["ENCRYPTION_SECRET_KEY"]
135+
except KeyError:
133136
raise ImproperlyConfigured(
134137
"DJANGO_EMAIL_LEARNING['ENCRYPTION_SECRET_KEY'] must be set in settings.py"
135138
)

django_email_learning/platform/api/views.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343

4444
logger = logging.getLogger(__name__)
4545

46+
DJANGO_EMAIL_LEARNING_SETTINGS: dict = getattr(settings, "DJANGO_EMAIL_LEARNING", {})
47+
4648

4749
@method_decorator(ensure_csrf_cookie, name="get")
4850
@method_decorator(accessible_for(roles={"admin", "editor"}), name="post")
@@ -581,7 +583,7 @@ def post(self, request, *args, **kwargs) -> JsonResponse: # type: ignore[no-unt
581583
form.save(
582584
request=request,
583585
use_https=request.is_secure(),
584-
from_email=settings.DJANGO_EMAIL_LEARNING.get(
586+
from_email=DJANGO_EMAIL_LEARNING_SETTINGS.get(
585587
"FROM_EMAIL", settings.DEFAULT_FROM_EMAIL
586588
),
587589
email_template_name="emails/password_reset.txt",
@@ -831,10 +833,10 @@ def get(self, request, *args, **kwargs) -> JsonResponse: # type: ignore[no-unty
831833

832834
@staticmethod
833835
def calculate_job_health_status(last_execution_started_at: datetime) -> str:
834-
success_threshold = settings.DJANGO_EMAIL_LEARNING.get(
836+
success_threshold = DJANGO_EMAIL_LEARNING_SETTINGS.get(
835837
"JOB_HEALTH_SUCCESS_THRESHOLD_MINUTES", DEFAULT_SUCCESS_THRESHOLD_MINUTES
836838
)
837-
warning_threshold = settings.DJANGO_EMAIL_LEARNING.get(
839+
warning_threshold = DJANGO_EMAIL_LEARNING_SETTINGS.get(
838840
"JOB_HEALTH_WARNING_THRESHOLD_MINUTES", DEFAULT_WARNING_THRESHOLD_MINUTES
839841
)
840842
if not isinstance(success_threshold, int) or success_threshold <= 0:

django_email_learning/platform/views.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
is_an_organization_member,
1212
)
1313
from typing import Dict, Any
14+
from django.conf import settings
1415

1516

1617
@method_decorator(login_required, name="dispatch")
@@ -36,10 +37,25 @@ def get_shared_context(self) -> Dict[str, Any]:
3637
current_lang_code = get_language()
3738
lang_info = get_language_info(current_lang_code)
3839

40+
DJANGO_EMAIL_LEARNING_SETTINGS: dict = getattr(
41+
settings, "DJANGO_EMAIL_LEARNING", {}
42+
)
43+
3944
return {
4045
"appContext": {
4146
"apiBaseUrl": reverse("django_email_learning:api_platform:root")[:-1],
4247
"platformBaseUrl": reverse("django_email_learning:platform:root")[:-1],
48+
"sidebarCustomComponent": {
49+
"scriptUrl": DJANGO_EMAIL_LEARNING_SETTINGS.get("SIDEBAR", {})
50+
.get("CUSTOM_COMPONENT", {})
51+
.get("SCRIPT_URL"),
52+
"componentTag": DJANGO_EMAIL_LEARNING_SETTINGS.get("SIDEBAR", {})
53+
.get("CUSTOM_COMPONENT", {})
54+
.get("COMPONENT_TAG"),
55+
"styleUrl": DJANGO_EMAIL_LEARNING_SETTINGS.get("SIDEBAR", {})
56+
.get("CUSTOM_COMPONENT", {})
57+
.get("STYLE_URL"),
58+
},
4359
"userRole": role,
4460
"direction": "rtl" if lang_info["bidi"] else "ltr",
4561
"isPlatformAdmin": (

django_email_learning/services/command_models/enroll_command.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,11 @@ def execute(self) -> None:
9292
reverse("django_email_learning:personalised:verify_enrollment")
9393
+ f"?token={token}"
9494
)
95+
DJANGO_EMAIL_LEARNING_SETTINGS: dict = getattr(
96+
settings, "DJANGO_EMAIL_LEARNING", {}
97+
)
9598
verification_link = (
96-
settings.DJANGO_EMAIL_LEARNING["SITE_BASE_URL"] + verification_relative_path
99+
DJANGO_EMAIL_LEARNING_SETTINGS["SITE_BASE_URL"] + verification_relative_path
97100
)
98101

99102
template_context = {

django_email_learning/services/email_sender_service.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,22 @@
55

66
class EmailSenderService:
77
def __init__(self) -> None:
8+
DJANGO_EMAIL_LEARNING_SETTINGS: dict = getattr(
9+
settings, "DJANGO_EMAIL_LEARNING", {}
10+
)
811
try:
912
self.email_sender = import_string(
10-
settings.DJANGO_EMAIL_LEARNING["EMAIL_SENDER"]
13+
DJANGO_EMAIL_LEARNING_SETTINGS["EMAIL_SENDER"]
1114
)
12-
except (AttributeError, KeyError):
15+
except KeyError:
1316
from django_email_learning.services.defaults.email_sender import (
1417
DjangoEmailSender,
1518
)
1619

1720
self.email_sender = DjangoEmailSender()
1821

1922
try:
20-
self.from_email = settings.DJANGO_EMAIL_LEARNING["FROM_EMAIL"]
23+
self.from_email = DJANGO_EMAIL_LEARNING_SETTINGS["FROM_EMAIL"]
2124
except (AttributeError, KeyError):
2225
try:
2326
self.from_email = settings.DEFAULT_FROM_EMAIL

django_email_learning/templates/platform/base.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
<script>
77
localStorage.setItem('activeOrganizationId', '{{ activeOrganizationId | escapejs }}');
88
</script>
9+
{% if appContext.sidebarCustomComponent.styleUrl %}
10+
<link rel="stylesheet" href="{{ appContext.sidebarCustomComponent.styleUrl }}">
11+
{% endif %}
12+
{% if appContext.sidebarCustomComponent.scriptUrl %}
13+
<script src="{{ appContext.sidebarCustomComponent.scriptUrl }}" type="module"></script>
14+
{% endif %}
915
{{ appContext | json_script:"app-context" }}
1016
{% block extra_head %}{% endblock %}
1117
<meta charset="UTF-8" />

django_service/settings.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@
103103
"SITE_BASE_URL": "http://localhost:8000",
104104
"ENCRYPTION_SECRET_KEY": "your-very-secure-and-random-key",
105105
"FROM_EMAIL": os.environ.get("FROM_EMAIL", "webmaster@localhost"),
106+
"SIDEBAR": {
107+
"CUSTOM_COMPONENT": {
108+
"SCRIPT_URL": "https://cdn.jsdelivr.net/npm/ldrs/dist/auto/helix.js",
109+
"STYLE_URL": None,
110+
"COMPONENT_TAG": "<l-helix />",
111+
}
112+
},
106113
}
107114

108115

frontend/src/components/MenuBar.jsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function MenuBar({activeOrganizationId, changeOrganizationCallback, showOrganiza
5454
const [organizations, setOrganizations] = useState([])
5555
const [deliverContentsJobStatus, setDeliverContentsJobStatus] = useState(null)
5656
const [chip, setChip] = useState(null)
57-
const { localeMessages, isPlatformAdmin, isOrganizationAdmin, direction, apiBaseUrl, platformBaseUrl } = useAppContext();
57+
const { localeMessages, isPlatformAdmin, isOrganizationAdmin, direction, apiBaseUrl, platformBaseUrl, sidebarCustomComponent } = useAppContext();
5858

5959
const theme = useTheme();
6060
const isMdUpScreen = useMediaQuery(theme.breakpoints.up('md'));
@@ -166,7 +166,7 @@ function MenuBar({activeOrganizationId, changeOrganizationCallback, showOrganiza
166166
</Box>
167167
</AppBar>
168168
<Drawer anchor={direction === 'rtl' ? 'right' : 'left'} variant={drawerVariant} onClose={toggleMenuDrawer(false)} display={{md: "none" }} open={menuOpen} sx={{ '& .MuiDrawer-paper': { boxSizing: 'border-box', width: drawerWidth } }}
169-
slotProps={{ backdrop: { sx: { backgroundColor: 'rgba(251, 251, 255, 0.57)', backdropFilter: 'blur(5px)' }}, paper: { sx: { boxShadow: '2px 0px 8px rgba(0, 0, 0, 0.1)'}}}}>
169+
slotProps={{ backdrop: { sx: { backgroundColor: 'rgba(251, 251, 255, 0.57)', backdropFilter: 'blur(5px)' }}, paper: { sx: { borderRadius: 0, boxShadow: '2px 0px 8px rgba(0, 0, 0, 0.1)'}}}}>
170170
<Box my={2} textAlign="center">
171171
<img src={logoVerticalUrl} alt="Logo" style={{ width: "50%" }} />
172172
</Box>
@@ -185,6 +185,7 @@ function MenuBar({activeOrganizationId, changeOrganizationCallback, showOrganiza
185185
</MenuItem>
186186
)) }
187187
</MenuList>
188+
{sidebarCustomComponent && <Box sx={{ height: "100px", width: "100%" }} dangerouslySetInnerHTML={{ __html: sidebarCustomComponent.componentTag }} />}
188189
</Drawer>
189190
</Box>)
190191
}

0 commit comments

Comments
 (0)