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 e16f745
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
12 changes: 12 additions & 0 deletions notifications/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,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
15 changes: 14 additions & 1 deletion notifications/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,20 @@ def live_unread_notification_count(request):
return JsonResponse(data)


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


@never_cache
def live_unread_notification_list(request):
''' Return a json with a unread notification list '''
Expand All @@ -165,7 +179,6 @@ def live_unread_notification_list(request):
return JsonResponse(data)

unread_list = get_notification_list(request, 'unread')

data = {
'unread_count': request.user.notifications.unread().count(),
'unread_list': unread_list
Expand Down

0 comments on commit e16f745

Please sign in to comment.