Skip to content

Commit

Permalink
Merge pull request #1014 from Metro-Records/feature/manage-alerts
Browse files Browse the repository at this point in the history
Add ability to manage alerts
  • Loading branch information
xmedr authored Oct 11, 2023
2 parents 6b51ef7 + 559c7dd commit dcf2bff
Show file tree
Hide file tree
Showing 12 changed files with 251 additions and 4 deletions.
1 change: 1 addition & 0 deletions councilmatic/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
"adv_cache_tag",
"debug_toolbar",
"captcha",
"markdownify.apps.MarkdownifyConfig",
)

try:
Expand Down
2 changes: 2 additions & 0 deletions councilmatic/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
MinutesView,
pong,
test_logging,
AlertDeleteView,
)
from lametro.feeds import LAMetroPersonDetailFeed

Expand Down Expand Up @@ -107,6 +108,7 @@
name="delete_submission",
),
url(r"^delete-event/(?P<event_slug>[^/]+)/$", delete_event, name="delete_event"),
path("alerts/<int:pk>/delete/", AlertDeleteView.as_view(), name="delete_alert"),
url(
r"^pong/$",
pong,
Expand Down
24 changes: 23 additions & 1 deletion lametro/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from django import forms
from django.core.files.uploadedfile import InMemoryUploadedFile
from django.core.exceptions import ValidationError

from captcha.fields import ReCaptchaField
from captcha.fields import ReCaptchaV3
Expand All @@ -11,7 +12,7 @@

from councilmatic_core.views import CouncilmaticSearchForm

from lametro.models import LAMetroPerson
from lametro.models import LAMetroPerson, Alert


class LAMetroCouncilmaticSearchForm(CouncilmaticSearchForm):
Expand Down Expand Up @@ -169,3 +170,24 @@ def __init__(self, *args, **kwargs):
class Meta:
model = LAMetroPerson
fields = ["councilmatic_biography"]


class AlertForm(forms.ModelForm):
def clean_description(self):
data = self.cleaned_data.get("description", None)
if not data:
raise ValidationError("Please provide an alert description")
return data

class Meta:
model = Alert
fields = "__all__"
widgets = {
"description": forms.Textarea(
attrs={
"rows": 4,
"placeholder": "Enter alert text",
"style": "border: none;",
}
),
}
42 changes: 42 additions & 0 deletions lametro/migrations/0012_alert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Generated by Django 3.2.19 on 2023-09-11 14:33

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("lametro", "0011_relatebillsubject"),
]

operations = [
migrations.CreateModel(
name="Alert",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("description", models.TextField(blank=True, null=True)),
(
"type",
models.CharField(
choices=[
("primary", "Primary"),
("secondary", "Secondary"),
("success", "Success"),
("danger", "Danger"),
("warning", "Warning"),
("info", "Info"),
],
max_length=255,
),
),
],
),
]
14 changes: 14 additions & 0 deletions lametro/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1144,3 +1144,17 @@ def __str__(self):

else:
return self.name


class Alert(models.Model):
TYPE_CHOICES = [
("primary", "Primary"),
("secondary", "Secondary"),
("success", "Success"),
("danger", "Danger"),
("warning", "Warning"),
("info", "Info"),
]

description = models.TextField(null=True, blank=True)
type = models.CharField(max_length=255, choices=TYPE_CHOICES)
8 changes: 8 additions & 0 deletions lametro/static/css/city_custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -423,3 +423,11 @@ hr .events-line {
background-position: center center;
height: 200px;
}

label[for=id_type], label[for=id_description], .text-white {
color: white;
}

textarea#id_description {
resize: vertical;
}
11 changes: 11 additions & 0 deletions lametro/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,17 @@
</div>
</nav>

{% load extras lametro_extras static %}
{% load markdownify %}
{% get_alerts as alerts %}
{% if alerts %}
{% for alert in alerts %}
<div class="alert alert-{{alert.type}}" style="margin-bottom: 0px;">
{{alert.description|markdownify}}
</div>
{% endfor %}
{% endif %}

{% endcache %}

{% block full_content %}
Expand Down
74 changes: 74 additions & 0 deletions lametro/templates/index/_alert_manager.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<div class="row">
<div class="col-xs-12">
<h2 class="text-white" style="display: inline-block;">Manage Alerts</h2>
<button class="btn btn-primary" id="alert-manager-toggle" style="margin-left: 10px; width: 70px;">Show</button>
</div>

</div>

<div class="row" id="alert-manager">
<div class="col-md-7">
<h3 class="text-white">Add New</h3>
<form role="form" method="POST" enctype="multipart/form-data" id="alert-form">
{% csrf_token %}
<p>
{{alert_form.type.label_tag}} {{alert_form.type}}
</p>

<p>
{{alert_form.description.label_tag}}
<div class="d-flex" style="background-color: white; flex-direction: column;">
<div style="border-bottom: 1px solid rgb(118, 118, 118);">
<button id="link" type="button" aria-label="Insert markdown for a link">
<i class="fa fa-link" aria-hidden="true"></i>
</button>
<button id="bold" type="button" aria-label="Insert markdown for bold text">
<i class="fa fa-bold" aria-hidden="true"></i>
</button>
<button id="italic" type="button" aria-label="Insert markdown for italic text">
<i class="fa fa-italic" aria-hidden="true"></i>
</button>
</div>

{{alert_form.description}}
</div>

{% for error in alert_form.description.errors %}
<span style="color: #eb6864; display: block;">*{{error|striptags}}</span>
{% endfor %}
</p>

<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>

<div class="col-md-5 text-white">
<h3 class="text-white">View Existing</h3>
{% if alerts %}
<table class="table">
<thead>
<th>Type</th>
<th>Description</th>
<th aria-hidden="true"></th>
</thead>
<tbody>
{% for alert in alerts %}
<tr>
<td>{{alert.type|title}}</td>
<td>{{alert.description}}</td>
<td>
<form action="{% url 'delete_alert' alert.pk %}" method="POST">
{% csrf_token %}
<button type="submit" class="btn btn-primary">Delete</button>
</form>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% else %}
<p>No Alerts Added</p>
{% endif %}
</div>
</div>
<hr class="col-md-12" aria-hidden="true">
45 changes: 45 additions & 0 deletions lametro/templates/index/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
<div id="section-photo" class="container-fluid">
<div class="row">
<div class='col-sm-10 col-sm-offset-1'>
{% if user.is_authenticated %}
{% include "index/_alert_manager.html" %}
{% endif %}
<span class="hidden-xs hidden-sm"><br/><br/></span>
<h1 class="home-header">Los Angeles County Metropolitan Transportation Authority</h1>
<p class="h3">
Expand Down Expand Up @@ -172,5 +175,47 @@ <h2 style="display: inline-block;">

$('#beta-info').tooltip({html: true, trigger: 'focus click', placement: 'right'});

function toggleAlertManager(){
$("#alert-manager").toggle();
}

toggleAlertManager();

$("#alert-manager-toggle").click(function() {
toggleAlertManager();
if ($(this).text() == "Show"){
$(this).text("Hide")
} else {
$(this).text("Show")
}
return false;
});

$("#id_type").on('change', function() {
$(this).removeClass()
$("#id_description").removeClass()
if (this.value != "") {
$(this).addClass("alert-" + this.value)
$("#id_description").addClass("alert-" + this.value)
}
});


$("#link, #bold, #italic").on('click', function(){
input = document.getElementById("id_description")

switch(this.id) {
case "link":
input.value += ("[this is a link!](https://url.com) ")
break;
case "bold":
input.value += ("**this is bolded text!** ")
break;
case "italic":
input.value += ("*this is italicized text!* ")
break;
}
})

</script>
{% endblock %}
7 changes: 6 additions & 1 deletion lametro/templatetags/lametro_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from councilmatic_core.models import Person, Bill
from councilmatic_core.utils import ExactHighlighter

from lametro.models import app_timezone
from lametro.models import app_timezone, Alert
from lametro.utils import format_full_text, parse_subject


Expand Down Expand Up @@ -316,3 +316,8 @@ def sort_topics(topics):
"""Sorts a board report's topics alphabetically."""

return sorted(topics, key=lambda t: t.name)


@register.simple_tag
def get_alerts():
return Alert.objects.all()
26 changes: 24 additions & 2 deletions lametro/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from django.urls import reverse
from django.utils import timezone
from django.utils.text import slugify
from django.views.generic import TemplateView
from django.views.generic import TemplateView, DeleteView, FormView
from django.http import (
HttpResponseRedirect,
HttpResponsePermanentRedirect,
Expand All @@ -33,6 +33,7 @@
from django.core import management
from django.core.serializers import serialize
from django.core.cache import cache
from django.urls import reverse_lazy

from councilmatic_core.views import (
IndexView,
Expand All @@ -57,13 +58,15 @@
LAMetroEvent,
LAMetroOrganization,
LAMetroSubject,
Alert,
)
from lametro.forms import (
AgendaUrlForm,
AgendaPdfForm,
LAMetroCouncilmaticSearchForm,
PersonHeadshotForm,
PersonBioForm,
AlertForm,
)

from councilmatic.settings_jurisdiction import MEMBER_BIOS, BILL_STATUS_DESCRIPTIONS
Expand All @@ -77,11 +80,14 @@
app_timezone = pytz.timezone(settings.TIME_ZONE)


class LAMetroIndexView(IndexView):
class LAMetroIndexView(IndexView, FormView):
template_name = "index/index.html"

event_model = LAMetroEvent

form_class = AlertForm
success_url = reverse_lazy("index")

@property
def extra_context(self):
extra = {}
Expand All @@ -100,9 +106,15 @@ def extra_context(self):
"start_date"
)
extra["form"] = LAMetroCouncilmaticSearchForm()
extra["alert_form"] = self.get_form()

return extra

def form_valid(self, form):
form.save()

return super().form_valid(form)


class LABillDetail(BillDetailView):
model = LAMetroBill
Expand Down Expand Up @@ -1036,6 +1048,16 @@ def get_context_data(self, **kwargs):
return context


class AlertDeleteView(DeleteView):
model = Alert
success_url = reverse_lazy("index")

def form_valid(self, form):
self.object.delete()

return super().form_valid(form)


def metro_login(request):
logout(request)
if request.method == "POST":
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ django-storages[boto3]<1.13
urllib3>1.25.0
python-dotenv==0.20.0
esprima==4.0.1
django-markdownify==0.9.3

# Development
django-debug-toolbar
Expand Down

0 comments on commit dcf2bff

Please sign in to comment.