Misc cleanup

This commit is contained in:
Jeremy Stretch 2024-07-15 13:56:54 -04:00
parent 9fc0c83641
commit 7856da4d9c
6 changed files with 36 additions and 16 deletions

View File

@ -3,7 +3,6 @@ import re
from django import forms
from django.contrib.postgres.forms import SimpleArrayField
from django.core.exceptions import ObjectDoesNotExist
from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as _
from core.models import ObjectType

View File

@ -257,6 +257,15 @@ class NotificationGroupForm(forms.ModelForm):
model = NotificationGroup
fields = ('name', 'description', 'groups', 'users')
def clean(self):
super().clean()
# At least one User or Group must be assigned
if not self.cleaned_data['groups'] and not self.cleaned_data['users']:
raise forms.ValidationError(_("A notification group specify at least one user or group."))
return self.cleaned_data
class SubscriptionForm(forms.ModelForm):
object_type = ContentTypeChoiceField(

View File

@ -88,12 +88,6 @@ class Notification(models.Model):
def get_absolute_url(self):
return reverse('account:notifications')
def get_read_url(self):
return reverse('extras:notification_read', kwargs={'pk': self.pk})
def get_dismiss_url(self):
return reverse('extras:notification_dismiss', kwargs={'pk': self.pk})
def clean(self):
super().clean()
@ -105,6 +99,9 @@ class Notification(models.Model):
@cached_property
def event(self):
"""
Returns the registered Event which triggered this Notification.
"""
return registry['events'].get(self.event_type)
@ -165,6 +162,7 @@ class NotificationGroup(ChangeLoggedModel):
Notification(user=member, **kwargs)
for member in self.members
])
notify.alters_data = True
class Subscription(models.Model):

View File

@ -31,13 +31,17 @@ __all__ = (
'WebhookTable',
)
IMAGEATTACHMENT_IMAGE = '''
IMAGEATTACHMENT_IMAGE = """
{% if record.image %}
<a class="image-preview" href="{{ record.image.url }}" target="_blank">{{ record }}</a>
{% else %}
&mdash;
{% endif %}
'''
"""
NOTIFICATION_ICON = """
<span class="text-{{ value.color }} fs-3"><i class="{{ value.icon }}"></i></span>
"""
class CustomFieldTable(NetBoxTable):
@ -276,19 +280,23 @@ class SubscriptionTable(NetBoxTable):
linkify=True,
orderable=False
)
user = tables.Column(
verbose_name=_('User'),
linkify=True
)
actions = columns.ActionsColumn(
actions=('delete',)
)
class Meta(NetBoxTable.Meta):
model = Subscription
fields = ('pk', 'object', 'object_type', 'created')
fields = ('pk', 'object', 'object_type', 'created', 'user')
default_columns = ('object', 'object_type', 'created')
class NotificationTable(NetBoxTable):
icon = columns.TemplateColumn(
template_code='<span class="text-{{ value.color }} fs-3"><i class="{{ value.icon }}"></i></span>',
template_code=NOTIFICATION_ICON,
accessor=tables.A('event'),
attrs={
'td': {'class': 'w-1'},
@ -315,14 +323,18 @@ class NotificationTable(NetBoxTable):
timespec='minutes',
verbose_name=_('Read'),
)
user = tables.Column(
verbose_name=_('User'),
linkify=True
)
actions = NotificationActionsColumn(
actions=('dismiss',)
)
class Meta(NetBoxTable.Meta):
model = Notification
fields = ('pk', 'icon', 'object', 'object_type', 'event_type', 'created', 'read')
default_columns = ('icon', 'object', 'object_type', 'event_type', 'created', 'read')
fields = ('pk', 'icon', 'object', 'object_type', 'event_type', 'created', 'read', 'user')
default_columns = ('icon', 'object', 'object_type', 'event_type', 'created')
row_attrs = {
'data-read': lambda record: bool(record.read),
}

View File

@ -407,7 +407,9 @@ class NotificationGroupBulkDeleteView(generic.BulkDeleteView):
#
class NotificationsView(LoginRequiredMixin, View):
"""
HTMX-only user-specific notifications list.
"""
def get(self, request):
return render(request, 'htmx/notifications.html', {
'notifications': request.user.notifications.unread(),

View File

@ -7,11 +7,11 @@
<i class="{{ notification.event.icon }}"></i>
</div>
<div class="col text-truncate">
<a href="{{ notification.get_read_url }}" class="text-body d-block">{{ notification.object }}</a>
<a href="{% url 'extras:notification_read' pk=notification.pk %}" class="text-body d-block">{{ notification.object }}</a>
<div class="d-block text-secondary fs-5">{{ notification.event }} {{ notification.created|timesince }} {% trans "ago" %}</div>
</div>
<div class="col-auto">
<a href="#" hx-get="{{ notification.get_dismiss_url }}" hx-target="closest .notifications" class="list-group-item-actions text-secondary" title="{% trans "Dismiss" %}">
<a href="#" hx-get="{% url 'extras:notification_dismiss' pk=notification.pk %}" hx-target="closest .notifications" class="list-group-item-actions text-secondary" title="{% trans "Dismiss" %}">
<i class="mdi mdi-close"></i>
</a>
</div>