Skip to content

Commit

Permalink
Merge pull request #30 from Amsterdam/feature/101127-add-alerts
Browse files Browse the repository at this point in the history
101127 added notification alerts for misfunctioning webapp
  • Loading branch information
remyvdwereld authored Sep 16, 2024
2 parents 94820e1 + 0c390f3 commit 8feb896
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 4 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@ docker-compose -f docker-compose.local.yml build
Start watching static files changes scss:

```bash
docker-compose exec web ./node_modules/.bin/node-sass -o ./assets/bundles/ static_src/sass --watch
docker-compose -f docker-compose.local.yml exec omslagroute ./node_modules/.bin/node-sass -o ./assets/bundles/ static_src/sass --watch
```

Start watching static files changes js, vue:

```bash
docker-compose exec web ./node_modules/.bin/webpack --config webpack.config.js --watch
docker-compose -f docker-compose.local.yml exec omslagroute ./node_modules/.bin/webpack --config webpack.config.js --watch
```

Migrate database without restarting containers:

```bash
docker-compose exec web python manage.py migrate
docker-compose -f docker-compose.local.yml exec omslagroute python manage.py migrate
```

# Styling resources
Expand Down
2 changes: 2 additions & 0 deletions app/settings/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
'web.profiles',
'web.forms',
'web.feedback',
'web.alerts',

)
SOURCE_COMMIT = os.environ.get('COMMIT_HASH')
Expand Down Expand Up @@ -158,6 +159,7 @@
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'web.core.context_processors.app_settings',
'web.alerts.context_processors.alerts_processor',
],
# 'loaders': [
# ('django.template.loaders.cached.Loader', [
Expand Down
13 changes: 12 additions & 1 deletion app/static_src/sass/basics/_06_site-layout.scss
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,15 @@ footer {
position: -webkit-sticky;
top: 0;
transition: margin-top .3s ease-in-out;
}
}

.alert-wrapper {
width: 100%;
}

.alert-message {
padding: 15px;
background-color: #FF9100;
color: white;
text-align: center;
}
15 changes: 15 additions & 0 deletions app/web/alerts/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from django.contrib import admin
from .models import Alert


@admin.register(Alert)
class AlertAdmin(admin.ModelAdmin):
list_display = ('id', 'is_active_boolean', 'start_time', 'end_time', 'message',)
search_fields = ('message',)

# Display boolean checkmark instead of True/False
def is_active_boolean(self, obj):
return obj.is_active()

is_active_boolean.boolean = True # This tells Django to use checkmarks/crosses
is_active_boolean.short_description = 'Active' # Optional, to rename the column
7 changes: 7 additions & 0 deletions app/web/alerts/context_processors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from .models import Alert
from django.utils import timezone


def alerts_processor(request):
alerts = Alert.objects.filter(start_time__lte=timezone.now(), end_time__gte=timezone.now())
return {'alerts': alerts}
27 changes: 27 additions & 0 deletions app/web/alerts/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 5.0.8 on 2024-09-11 12:11

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Alert',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('message', models.TextField()),
('start_time', models.DateTimeField()),
('end_time', models.DateTimeField()),
],
options={
'verbose_name': 'Alert',
'verbose_name_plural': 'Alerts',
},
),
]
Empty file.
20 changes: 20 additions & 0 deletions app/web/alerts/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from django.db import models
from django.utils import timezone


class Alert(models.Model):
message = models.TextField()
start_time = models.DateTimeField()
end_time = models.DateTimeField()

def is_active(self):
""" Check if the alert is active based on start and end time """
now = timezone.now()
return self.start_time <= now <= self.end_time

def __str__(self):
return f"Alert: {self.message[:50]}"

class Meta:
verbose_name = "Alert"
verbose_name_plural = "Alerts"
11 changes: 11 additions & 0 deletions app/web/core/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@
</head>
<body>

{% if alerts %}
<div class="alert-wrapper">
{% for alert in alerts %}
<div class="alert-message">
<strong>{{ alert.message|safe }}</strong>
</div>
{% endfor %}
</div>
{% endif %}

<div class="site-container">
{% include 'svg-sprite.html' %}
{% block skiplinks %}
Expand Down Expand Up @@ -90,6 +100,7 @@ <h1 class="site-title">
{% endif %}
</div>
</header>

{% include 'users/user_type_menus.html' %}
{% block messages %}
<ul class="alert-container screen-only">
Expand Down

0 comments on commit 8feb896

Please sign in to comment.