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