Skip to content

Commit

Permalink
add get_absolute_url to the notifications API
Browse files Browse the repository at this point in the history
  • Loading branch information
PetrDlouhy committed Apr 17, 2024
1 parent ddf6b59 commit 089246b
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions notifications/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@
from notifications.utils import id2slug
from notifications.settings import get_config

def get_object_url(instance, notification, request):
"""
Get url representing the instance object.
This will return instance.get_url_for_notifications()
with parameters `notification` and `request`,
if it is defined and get_absolute_url() otherwise
"""
if hasattr(instance, 'get_url_for_notifications'):
return instance.get_url_for_notifications(notification, request)
elif hasattr(instance, 'get_absolute_url'):
return instance.get_absolute_url()
return None

def get_num_to_fetch(request):
default_num_to_fetch = get_config()['NUM_TO_FETCH']
try:
Expand All @@ -22,10 +35,22 @@ def get_notification_list(request, method_name='all'):
struct['slug'] = id2slug(notification.id)
if notification.actor:
struct['actor'] = str(notification.actor)
actor_url = get_object_url(
notification.actor, notification, request)
if actor_url:
struct['actor_url'] = actor_url
if notification.target:
struct['target'] = str(notification.target)
target_url = get_object_url(
notification.target, notification, request)
if target_url:
struct['target_url'] = target_url
if notification.action_object:
struct['action_object'] = str(notification.action_object)
action_object_url = get_object_url(
notification.action_object, notification, request)
if action_object_url:
struct['action_object_url'] = action_object_url
if notification.data:
struct['data'] = notification.data
notification_list.append(struct)
Expand Down

0 comments on commit 089246b

Please sign in to comment.