-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Migrate alert management to Wagtail #1174
Conversation
FieldPanel("description", widget=MarkdownTextarea), | ||
] | ||
|
||
def content(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ModelAdmin can render custom model methods in the list display. This method renders the alert description and adds a styled label for the alert type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah neat, and TIL format_html
and expand_db_html
are useful tools to return db stored html
@@ -1237,17 +1237,3 @@ def __str__(self): | |||
|
|||
else: | |||
return self.name | |||
|
|||
|
|||
class Alert(models.Model): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Migrated verbatim to models/cms.py
.
@@ -0,0 +1,53 @@ | |||
[class*="alert"] { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Low key obsessed with CSS variables.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did some reading up on this and oh yea I can totally see why 👀
// Callback function to execute when mutations are observed | ||
const styleAlertTypeSelect = (mutationList, observer) => { | ||
mutationList.map(mu => { | ||
mu.addedNodes && mu.addedNodes.forEach(node => { | ||
if (node.id === "id_type") { | ||
styleParent({target: node}) | ||
node.addEventListener("change", styleParent) | ||
observer.disconnect() | ||
return | ||
} | ||
}) | ||
}) | ||
}; | ||
|
||
// Create an observer instance linked to the callback function | ||
const observer = new MutationObserver(styleAlertTypeSelect); | ||
|
||
const config = {childList: true, subtree: true}; | ||
|
||
// Start observing the target node for configured mutations | ||
observer.observe(document.body, config); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This whole rigamarole is necessary because Wagtail adds form fields to the DOM in JavaScript. In order to update the alert type select, we have to wait for it to show up, give it an initial style, then add an event listener for changes. The MutationObserver
API is pretty dope: https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
h/t to @xmedr for this UI!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah so is the mutation observer being used here mostly because we can't attach an event listener the usual way since the selector doesn't exist yet?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exactly!
@@ -115,6 +97,8 @@ | |||
{% endblock %} | |||
</main> | |||
|
|||
{% wagtailuserbar 'bottom-left' %} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replaces conditional admin link in top nav.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since they'll have to sign in to wagtail to manage the alerts after this, would it be useful to change up the Metro Login link in the footer? Maybe by making it link to the wagtail login instead. That is, as long as logging in through wagtail would still allow them to manage things like manual broadcast links or events that don't appear in legistar, which I imagine it would.
If we think yes, then I can take care of it when I branch off of this!
(Also something to consider in the future is whether wagtail should also somehow manage manual broadcasts.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, good idea. Yes!
) | ||
|
||
|
||
@hooks.register("construct_wagtail_userbar") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return format_html('<script src="{}"></script>', static("js/wagtail_custom.js")) | ||
|
||
|
||
@hooks.register("insert_global_admin_css", order=500) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lametro/wagtail_hooks.py
Outdated
return items | ||
|
||
|
||
@hooks.register("insert_global_admin_js", order=100) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -15,5 +15,5 @@ repos: | |||
rev: '9f60881' | |||
hooks: | |||
- id: flake8 | |||
files: lametro councilmatic tests | |||
files: ^(lametro|councilmatic|tests) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TRICKY LITTLE BUGGER
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FieldPanel("description", widget=MarkdownTextarea), | ||
] | ||
|
||
def content(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah neat, and TIL format_html
and expand_db_html
are useful tools to return db stored html
@@ -0,0 +1,53 @@ | |||
[class*="alert"] { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did some reading up on this and oh yea I can totally see why 👀
@@ -115,6 +97,8 @@ | |||
{% endblock %} | |||
</main> | |||
|
|||
{% wagtailuserbar 'bottom-left' %} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since they'll have to sign in to wagtail to manage the alerts after this, would it be useful to change up the Metro Login link in the footer? Maybe by making it link to the wagtail login instead. That is, as long as logging in through wagtail would still allow them to manage things like manual broadcast links or events that don't appear in legistar, which I imagine it would.
If we think yes, then I can take care of it when I branch off of this!
(Also something to consider in the future is whether wagtail should also somehow manage manual broadcasts.)
lametro/wagtail_hooks.py
Outdated
def add_modeladmin_links(request, items): | ||
for modeladmin_cls in (AlertAdmin,): | ||
items.append(ModelAdminLink(modeladmin_cls)) | ||
return items |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm curious why this is a loop, could you talk a bit about that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! Factored out.
// Callback function to execute when mutations are observed | ||
const styleAlertTypeSelect = (mutationList, observer) => { | ||
mutationList.map(mu => { | ||
mu.addedNodes && mu.addedNodes.forEach(node => { | ||
if (node.id === "id_type") { | ||
styleParent({target: node}) | ||
node.addEventListener("change", styleParent) | ||
observer.disconnect() | ||
return | ||
} | ||
}) | ||
}) | ||
}; | ||
|
||
// Create an observer instance linked to the callback function | ||
const observer = new MutationObserver(styleAlertTypeSelect); | ||
|
||
const config = {childList: true, subtree: true}; | ||
|
||
// Start observing the target node for configured mutations | ||
observer.observe(document.body, config); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah so is the mutation observer being used here mostly because we can't attach an event listener the usual way since the selector doesn't exist yet?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome, looks good!
/* Add default border to empty alert select */ | ||
.alert- { | ||
border: 1px solid var(--w-color-border-field-default)!important; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, tyty!
Overview
See title. Connects #1175.
Demo
Testing Instructions
Credentials for the datamade user are in Bitwarden under
LA Metro Wagtail User - Staging