diff --git a/src/sdg/organisaties/views/notificaties.py b/src/sdg/organisaties/views/notificaties.py
index f012d3751..5138ad5ce 100644
--- a/src/sdg/organisaties/views/notificaties.py
+++ b/src/sdg/organisaties/views/notificaties.py
@@ -47,13 +47,7 @@ def get(self, request, *args, **kwargs):
# Call the parent class's get method to fetch the queryset
response = super().get(request, *args, **kwargs)
- # Get or create the NotificationViewed instance for the current user
- notification_viewed, create = NotificationViewed.objects.get_or_create(
- gebruiker=request.user
- )
-
- # Update the last_viewed_date to the current time
- notification_viewed.last_viewed_date = timezone.now()
- notification_viewed.save()
+ # Update or create the NotificationViewed instance for the current user
+ NotificationViewed.objects.update_or_create(gebruiker=request.user)
return response
diff --git a/src/sdg/producten/migrations/0060_alter_notificationviewed_last_viewed_date.py b/src/sdg/producten/migrations/0060_alter_notificationviewed_last_viewed_date.py
new file mode 100644
index 000000000..a91b7a576
--- /dev/null
+++ b/src/sdg/producten/migrations/0060_alter_notificationviewed_last_viewed_date.py
@@ -0,0 +1,18 @@
+# Generated by Django 3.2.23 on 2024-11-15 16:33
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ("producten", "0059_notificationviewed"),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name="notificationviewed",
+ name="last_viewed_date",
+ field=models.DateTimeField(auto_now=True),
+ ),
+ ]
diff --git a/src/sdg/producten/models/notification.py b/src/sdg/producten/models/notification.py
index 701cc4ecf..c1286e92e 100644
--- a/src/sdg/producten/models/notification.py
+++ b/src/sdg/producten/models/notification.py
@@ -1,15 +1,12 @@
from django.contrib.auth import get_user_model
from django.db import models
-from django.utils import timezone
User = get_user_model()
class NotificationViewed(models.Model):
gebruiker = models.OneToOneField(User, on_delete=models.CASCADE)
- last_viewed_date = models.DateTimeField(null=True, blank=True)
+ last_viewed_date = models.DateTimeField(auto_now=True)
def __str__(self):
- return (
- f"{self.gebruiker} - Notification last viewed on: {self.last_viewed_date}"
- )
+ return f"User: {self.gebruiker_id} - Notification last viewed on: {self.last_viewed_date}"
diff --git a/src/sdg/templates/navigation/nav_item.html b/src/sdg/templates/navigation/nav_item.html
index bf1ed9360..4bc95ede7 100644
--- a/src/sdg/templates/navigation/nav_item.html
+++ b/src/sdg/templates/navigation/nav_item.html
@@ -6,6 +6,6 @@
{% if blank_target %}target="_blank" rel="noopener"{% endif %}
>
{{title}}
- {% if icon %}{{icon}}{% endif %}
+ {% if icon and show_icon %}{{icon}}{% endif %}
\ No newline at end of file
diff --git a/src/sdg/templates/navigation/navigation.html b/src/sdg/templates/navigation/navigation.html
index fbf3f950b..f0a99aaf3 100644
--- a/src/sdg/templates/navigation/navigation.html
+++ b/src/sdg/templates/navigation/navigation.html
@@ -29,11 +29,7 @@
{# Show new notification icon if has_new_notifications is True #}
{% url 'notificaties' as notification_url %}
- {% if has_new_notifications %}
- {% nav_item title=_('Notificaties') href=notification_url icon='' %}
- {% else %}
- {% nav_item title=_('Notificaties') href=notification_url %}
- {% endif %}
+ {% nav_item title=_('Notificaties') href=notification_url icon='' show_icon=has_new_notifications %}
{% if siteconfig.documentatie_link %}
{% nav_item title=siteconfig.documentatie_titel|capfirst|default:_("Documentatie") href=siteconfig.documentatie_link blank_target=True id="documentation" %}
diff --git a/src/sdg/utils/context_processors.py b/src/sdg/utils/context_processors.py
index af780f3bd..386a07d8a 100644
--- a/src/sdg/utils/context_processors.py
+++ b/src/sdg/utils/context_processors.py
@@ -32,23 +32,22 @@ def settings(request):
def has_new_notifications(request):
- if request.user and request.user.is_anonymous is not True:
- # Get the user's NotificationViewed instance
- try:
- notification_viewed = NotificationViewed.objects.get(gebruiker=request.user)
- except NotificationViewed.DoesNotExist:
- notification_viewed = None
+ user = request.user
+ has_new_notifications = False
- # Get the last_viewed_date from data
+ if user and user.is_anonymous is not True:
try:
+ notification_viewed = NotificationViewed.objects.get(gebruiker=user)
last_viewed_date = notification_viewed.last_viewed_date
- except AttributeError:
- # default last_viewed_date is 12 months ago
+ except NotificationViewed.DoesNotExist:
+ notification_viewed = None
+ # By default, the last_viewed_date is set to 12 months ago.
last_viewed_date = now() - relativedelta(months=12)
- # Get the latest product version after the last_viewed_date else None.
+ # Get the latest product versie (notifications are based on ProductVersie) later than last_viewed_date.
latest_notification = (
- ProductVersie.objects.filter(
+ ProductVersie.objects.select_related("product")
+ .filter(
product__referentie_product=None,
gewijzigd_op__gt=last_viewed_date,
)
@@ -56,17 +55,8 @@ def has_new_notifications(request):
.first()
)
- # Check if there is a new notification
- has_new = latest_notification is not None
-
- return {
- "has_new_notifications": has_new,
- "latest_notification_date": (
- latest_notification.gewijzigd_op if latest_notification else None
- ),
- }
+ has_new_notifications = bool(latest_notification)
return {
- "has_new_notifications": False,
- "latest_notification_date": None,
+ "has_new_notifications": has_new_notifications,
}
diff --git a/src/sdg/utils/templatetags/utils.py b/src/sdg/utils/templatetags/utils.py
index 635f3f140..bf54c53f6 100644
--- a/src/sdg/utils/templatetags/utils.py
+++ b/src/sdg/utils/templatetags/utils.py
@@ -178,6 +178,7 @@ def nav_item(context, href, title, **kwargs):
- icon, can be an element.
- id, set an id on the element
- blank_target, set the target to `_blank`
+ - show_icon, boolean to render the icon
"""
request = context.get("request")
@@ -194,6 +195,7 @@ def check_active_link():
# Get kwargs vars.
icon = kwargs.get("icon", None)
+ show_icon = kwargs.get("show_icon", None)
id = kwargs.get("id", None)
blank_target = kwargs.get("blank_target", False)
@@ -203,6 +205,7 @@ def check_active_link():
"href": href,
"title": title,
"icon": icon,
+ "show_icon": show_icon,
"blank_target": blank_target,
"id": id,
"active_link": active_link,