From 0c390f3a0f71331bc466216943b7484ee8ff2bdb Mon Sep 17 00:00:00 2001 From: Remy van der Wereld Date: Wed, 11 Sep 2024 15:28:33 +0200 Subject: [PATCH] 101127 added notification alerts for misfunctioning webapp --- README.md | 6 ++--- app/settings/settings.py | 2 ++ .../sass/basics/_06_site-layout.scss | 13 ++++++++- app/web/alerts/admin.py | 15 +++++++++++ app/web/alerts/context_processors.py | 7 +++++ app/web/alerts/migrations/0001_initial.py | 27 +++++++++++++++++++ app/web/alerts/migrations/__init__.py | 0 app/web/alerts/models.py | 20 ++++++++++++++ app/web/core/templates/base.html | 11 ++++++++ 9 files changed, 97 insertions(+), 4 deletions(-) create mode 100644 app/web/alerts/admin.py create mode 100644 app/web/alerts/context_processors.py create mode 100644 app/web/alerts/migrations/0001_initial.py create mode 100644 app/web/alerts/migrations/__init__.py create mode 100644 app/web/alerts/models.py diff --git a/README.md b/README.md index 948888ff..9383a041 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/app/settings/settings.py b/app/settings/settings.py index f835c73f..e275de51 100755 --- a/app/settings/settings.py +++ b/app/settings/settings.py @@ -46,6 +46,7 @@ 'web.profiles', 'web.forms', 'web.feedback', + 'web.alerts', ) SOURCE_COMMIT = os.environ.get('COMMIT_HASH') @@ -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', [ diff --git a/app/static_src/sass/basics/_06_site-layout.scss b/app/static_src/sass/basics/_06_site-layout.scss index ef59450a..ecac5921 100644 --- a/app/static_src/sass/basics/_06_site-layout.scss +++ b/app/static_src/sass/basics/_06_site-layout.scss @@ -179,4 +179,15 @@ footer { position: -webkit-sticky; top: 0; transition: margin-top .3s ease-in-out; -} \ No newline at end of file +} + +.alert-wrapper { + width: 100%; +} + +.alert-message { + padding: 15px; + background-color: #FF9100; + color: white; + text-align: center; +} diff --git a/app/web/alerts/admin.py b/app/web/alerts/admin.py new file mode 100644 index 00000000..ec24d7d6 --- /dev/null +++ b/app/web/alerts/admin.py @@ -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 diff --git a/app/web/alerts/context_processors.py b/app/web/alerts/context_processors.py new file mode 100644 index 00000000..af50ae32 --- /dev/null +++ b/app/web/alerts/context_processors.py @@ -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} diff --git a/app/web/alerts/migrations/0001_initial.py b/app/web/alerts/migrations/0001_initial.py new file mode 100644 index 00000000..e9399b91 --- /dev/null +++ b/app/web/alerts/migrations/0001_initial.py @@ -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', + }, + ), + ] diff --git a/app/web/alerts/migrations/__init__.py b/app/web/alerts/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/app/web/alerts/models.py b/app/web/alerts/models.py new file mode 100644 index 00000000..8fbc9bb9 --- /dev/null +++ b/app/web/alerts/models.py @@ -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" diff --git a/app/web/core/templates/base.html b/app/web/core/templates/base.html index 532d5f44..6c143b1e 100644 --- a/app/web/core/templates/base.html +++ b/app/web/core/templates/base.html @@ -42,6 +42,16 @@ +{% if alerts %} +
+ {% for alert in alerts %} +
+ {{ alert.message|safe }} +
+ {% endfor %} +
+{% endif %} +
{% include 'svg-sprite.html' %} {% block skiplinks %} @@ -90,6 +100,7 @@

{% endif %}

+ {% include 'users/user_type_menus.html' %} {% block messages %}