[models] Added method to return links to related objects.

This commit is contained in:
TheOneAboveAllTitan 2020-04-17 03:03:47 +05:30 committed by Alvaro Mariano
parent 8e87576f63
commit c401e69501
2 changed files with 37 additions and 2 deletions

View file

@ -9,10 +9,11 @@ Notification = load_model('notifications', 'Notification')
class NotificationAdmin(AbstractNotificationAdmin):
raw_id_fields = ('recipient',)
readonly_fields = ('action_object_url', 'actor_object_url', 'target_object_url')
list_display = ('recipient', 'actor',
'level', 'target', 'unread', 'public')
list_filter = ('level', 'unread', 'public', 'timestamp',)
def get_queryset(self, request):
qs = super(NotificationAdmin, self).get_queryset(request)
return qs.prefetch_related('actor')

View file

@ -11,9 +11,10 @@ from django.core.exceptions import ImproperlyConfigured
from django.db import models
from django.db.models.query import QuerySet
from django.utils import timezone
from django.utils.html import format_html
from jsonfield.fields import JSONField
from model_utils import Choices
from notifications import settings as notifications_settings
from notifications.signals import notify
from notifications.utils import id2slug
@ -24,6 +25,12 @@ if StrictVersion(get_version()) >= StrictVersion('1.8.0'):
else:
from django.contrib.contenttypes.generic import GenericForeignKey # noqa
try:
# Django >= 1.7
from django.urls import reverse, NoReverseMatch
except ImportError:
# Django <= 1.6
from django.core.urlresolvers import reverse, NoReverseMatch # pylint: disable=no-name-in-module,import-error
EXTRA_DATA = notifications_settings.get_config()['USE_JSONFIELD']
@ -252,6 +259,33 @@ class AbstractNotification(models.Model):
self.unread = True
self.save()
def actor_object_url(self):
try:
url = reverse("admin:{0}_{1}_change".format(self.actor_content_type.app_label,
self.actor_content_type.model),
args=(self.actor_object_id,))
return format_html("<a href='{url}'>{id}</a>", url=url, id=self.actor_object_id)
except NoReverseMatch:
return self.actor_object_id
def action_object_url(self):
try:
url = reverse("admin:{0}_{1}_change".format(self.action_object_content_type.app_label,
self.action_content_type.model),
args=(self.action_object_id,))
return format_html("<a href='{url}'>{id}</a>", url=url, id=self.action_object_object_id)
except NoReverseMatch:
return self.action_object_object_id
def target_object_url(self):
try:
url = reverse("admin:{0}_{1}_change".format(self.target_content_type.app_label,
self.target_content_type.model),
args=(self.target_object_id,))
return format_html("<a href='{url}'>{id}</a>", url=url, id=self.target_object_id)
except NoReverseMatch:
return self.target_object_id
def notify_handler(verb, **kwargs):
"""