Skip to content

Commit

Permalink
Merge pull request #4 from qburst/main
Browse files Browse the repository at this point in the history
feat: Optimized code by introducing bulk_create function
  • Loading branch information
vapdev authored Feb 14, 2023
2 parents c860746 + 980a5a7 commit eb354a1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
8 changes: 6 additions & 2 deletions apps/conversation/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
from rest_framework import generics
from rest_framework.permissions import IsAuthenticated
from rest_framework.decorators import permission_classes
from apps.notification.utilities import create_notification
from apps.notification.utilities import create_notification_bulk
from ..notification.models import Notification
from .models import Conversation, ConversationMessage
from .serializers import ChatSerializer, ConversationSerializer

Expand Down Expand Up @@ -42,9 +43,12 @@ def api_add_message(request):
message = ConversationMessage.objects.create(conversation_id=conversation_id, content=content, created_by=request.user)

if conversation_id:
notifications = []
for user in message.conversation.users.all():
if user != request.user:
create_notification(request, user, 'message')
notification = Notification(created_by=request, to_user=user, notification_type='message')
notifications.append(notification)
create_notification_bulk(notifications)

return JsonResponse({'success': True})

Expand Down
8 changes: 6 additions & 2 deletions apps/feed/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
from django.http import JsonResponse
from django.contrib.auth.models import User
from django.views.decorators.csrf import csrf_exempt
from apps.notification.utilities import create_notification
from apps.notification.utilities import create_notification, create_notification_bulk
from rest_framework.decorators import api_view
from rest_framework.pagination import PageNumberPagination
from rest_framework.permissions import IsAuthenticated
from rest_framework.decorators import permission_classes
from ..notification.models import Notification
from .serializers import TweekSerializer
from apps.feed.models import Tweek, Like, Dislike

Expand Down Expand Up @@ -64,11 +65,14 @@ def api_add_tweek(request):

# send notification to users mentioned in the tweek
results = re.findall("(^|[^@\w])@(\w{1,20})", body)
notifications = []
for result in results:
result = result[1]
if User.objects.filter(username=result).exists() and result != request.user.username:
user = User.objects.get(username=result)
create_notification(request, user, 'mention')
notification = Notification(created_by=request, to_user=user, notification_type='mention')
notifications.append(notification)
create_notification_bulk(notifications)

return JsonResponse({'success': True})

Expand Down
4 changes: 4 additions & 0 deletions apps/notification/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ def create_notification(created_by, to_user, notification_type):
notification = Notification.objects.create(to_user=to_user, notification_type=notification_type,
created_by=created_by)
return notification

def create_notification_bulk(notifications):
notification = Notification.objects.bulk_create(notifications)
return notification

0 comments on commit eb354a1

Please sign in to comment.