Skip to content

Commit

Permalink
♻️ [#990] Refactor some code based on feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
stefrado committed Nov 18, 2024
1 parent 0503625 commit 5d91833
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 41 deletions.
10 changes: 2 additions & 8 deletions src/sdg/organisaties/views/notificaties.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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),
),
]
7 changes: 2 additions & 5 deletions src/sdg/producten/models/notification.py
Original file line number Diff line number Diff line change
@@ -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}"
2 changes: 1 addition & 1 deletion src/sdg/templates/navigation/nav_item.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
{% if blank_target %}target="_blank" rel="noopener"{% endif %}
>
{{title}}
{% if icon %}{{icon}}{% endif %}
{% if icon and show_icon %}{{icon}}{% endif %}
</a>
</li>
6 changes: 1 addition & 5 deletions src/sdg/templates/navigation/navigation.html
Original file line number Diff line number Diff line change
Expand Up @@ -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='<i class="fa fa-circle-exclamation accent"></i>' %}
{% else %}
{% nav_item title=_('Notificaties') href=notification_url %}
{% endif %}
{% nav_item title=_('Notificaties') href=notification_url icon='<i class="fa fa-circle-exclamation accent"></i>' 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" %}
Expand Down
34 changes: 12 additions & 22 deletions src/sdg/utils/context_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,41 +32,31 @@ 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,
)
.order_by("-gewijzigd_op")
.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,
}
3 changes: 3 additions & 0 deletions src/sdg/utils/templatetags/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ def nav_item(context, href, title, **kwargs):
- icon, can be an <i> element.
- id, set an id on the <a> element
- blank_target, set the target to `_blank`
- show_icon, boolean to render the icon
"""

request = context.get("request")
Expand All @@ -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)

Expand All @@ -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,
Expand Down

0 comments on commit 5d91833

Please sign in to comment.