From 9ca284b68255aace2d7dfbc9f1623d92ac2435ae Mon Sep 17 00:00:00 2001 From: Xinacod Date: Sun, 22 Mar 2026 21:43:26 +0100 Subject: [PATCH] fix: skip notification when community has no admin MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `admins.first()` returns None on communities with no assigned admin, which then gets passed as the `user` field to `Notification.objects.create`. That silently fails (the exception is caught and swallowed) so the notification just disappears with no trace except a log error. Fixed by extracting the result of `admins.first()` into a variable and only creating the notification when it's not None. Affects four call sites across articles and communities. Closes no issue — found while reading the notification flow. --- articles/api.py | 26 +++++++++++++------------ communities/api_invitation.py | 36 +++++++++++++++++++---------------- communities/api_join.py | 16 +++++++++------- communities/articles_api.py | 24 ++++++++++++----------- 4 files changed, 56 insertions(+), 46 deletions(-) diff --git a/articles/api.py b/articles/api.py index 21fe9c4..9ccc659 100644 --- a/articles/api.py +++ b/articles/api.py @@ -159,18 +159,20 @@ def create_article( # Send notification to the community admin try: - Notification.objects.create( - user=community.admins.first(), - community=community, - category="communities", - notification_type="article_submitted", - message=( - f"New article submitted in {community.name}" - f" by {request.auth.username}" - ), - link=f"/community/{community.name}/submissions", - content=article.title, - ) + admin = community.admins.first() + if admin: + Notification.objects.create( + user=admin, + community=community, + category="communities", + notification_type="article_submitted", + message=( + f"New article submitted in {community.name}" + f" by {request.auth.username}" + ), + link=f"/community/{community.name}/submissions", + content=article.title, + ) except Exception: # Continue even if notification creation fails pass diff --git a/communities/api_invitation.py b/communities/api_invitation.py index 2959c17..fea7a7d 100644 --- a/communities/api_invitation.py +++ b/communities/api_invitation.py @@ -186,13 +186,15 @@ def respond_to_invitation( # Optional: Send notification to community admin about the decision try: - Notification.objects.create( - user=invitation.community.admins.first(), - message=f"{request.auth.username} has {payload.action}ed the " - f"invitation to join {invitation.community.name}.", - category="communities", - notification_type="join_request_responded", - ) + admin = invitation.community.admins.first() + if admin: + Notification.objects.create( + user=admin, + message=f"{request.auth.username} has {payload.action}ed the " + f"invitation to join {invitation.community.name}.", + category="communities", + notification_type="join_request_responded", + ) except Exception as e: logger.error(f"Error creating notification: {e}") # Continue even if notification fails - the invitation was already processed @@ -417,15 +419,17 @@ def respond_to_email_invitation( try: # Optional: Send notification to community admin about the decision - Notification.objects.create( - user=invitation.community.admins.first(), - message=( - f"{user.username} has {payload.action}ed the " - f"invitation to join {invitation.community.name}." - ), - category="communities", - notification_type="join_request_responded", - ) + admin = invitation.community.admins.first() + if admin: + Notification.objects.create( + user=admin, + message=( + f"{user.username} has {payload.action}ed the " + f"invitation to join {invitation.community.name}." + ), + category="communities", + notification_type="join_request_responded", + ) except Exception as e: logger.error(f"Error creating notification: {e}") # Continue even if notification fails - the invitation was already processed diff --git a/communities/api_join.py b/communities/api_join.py index 4f5b3a6..4074af7 100644 --- a/communities/api_join.py +++ b/communities/api_join.py @@ -119,13 +119,15 @@ def join_community(request, community_id: int): # Send a notification to the first admin try: - Notification.objects.create( - user=community.admins.first(), - community=community, - notification_type="join_request_received", - message=f"New join request from {user.username}", - link=f"/community/{community.name}/requests", - ) + admin = community.admins.first() + if admin: + Notification.objects.create( + user=admin, + community=community, + notification_type="join_request_received", + message=f"New join request from {user.username}", + link=f"/community/{community.name}/requests", + ) except Exception as e: logger.error(f"Error creating notification: {e}") # Continue even if notification fails diff --git a/communities/articles_api.py b/communities/articles_api.py index 17f60b8..f2fdee4 100644 --- a/communities/articles_api.py +++ b/communities/articles_api.py @@ -111,17 +111,19 @@ def submit_article(request, community_name: str, article_slug: str): # Send a notification to the community admins try: - Notification.objects.create( - user=community.admins.first(), - community=community, - category="communities", - notification_type="article_submitted", - message=( - f"New article submitted in {community.name} by {request.auth.username}" - ), - link=f"/community/{community.name}/submissions", - content=article.title, - ) + admin = community.admins.first() + if admin: + Notification.objects.create( + user=admin, + community=community, + category="communities", + notification_type="article_submitted", + message=( + f"New article submitted in {community.name} by {request.auth.username}" + ), + link=f"/community/{community.name}/submissions", + content=article.title, + ) except Exception as e: logger.error(f"Error creating notification: {e}") # Continue even if notification fails