3
3
from django .http import Http404
4
4
from django .contrib .auth import get_user_model
5
5
6
- from .models import (
7
- ChatSession , ChatSessionMember , ChatSessionMessage , deserialize_user
8
- )
9
-
10
6
from rest_framework import permissions
11
7
from rest_framework .views import APIView
12
8
from rest_framework .response import Response
13
- from notifications .signals import notify
9
+
10
+ from notifications .utils import notify
11
+ from notifications import default_settings as notifs_settings
12
+
13
+ from .models import ChatSession , ChatSessionMessage , deserialize_user
14
14
15
15
16
16
class ChatSessionView (APIView ):
@@ -47,17 +47,17 @@ def patch(self, request, *args, **kwargs):
47
47
48
48
owner = deserialize_user (owner )
49
49
members = [
50
- deserialize_user (chat_session .user )
50
+ deserialize_user (chat_session .user )
51
51
for chat_session in chat_session .members .all ()
52
52
]
53
53
members .insert (0 , owner ) # Make the owner the first member
54
54
55
- return Response ({
55
+ return Response ({
56
56
'status' : 'SUCCESS' , 'members' : members ,
57
- 'message' : '%s joined that chat' % user .username ,
57
+ 'message' : '%s joined the chat' % user .username ,
58
58
'user' : deserialize_user (user )
59
59
})
60
-
60
+
61
61
62
62
class ChatSessionMessageView (APIView ):
63
63
"""Create/Get Chat session messages."""
@@ -69,8 +69,8 @@ def get(self, request, *args, **kwargs):
69
69
uri = kwargs ['uri' ]
70
70
71
71
chat_session = ChatSession .objects .get (uri = uri )
72
- messages = [chat_session_message .to_json ()
73
- for chat_session_message in chat_session .messages .all ()]
72
+ messages = [chat_session_message .to_json ()
73
+ for chat_session_message in chat_session .messages .all ()]
74
74
75
75
return Response ({
76
76
'id' : chat_session .id , 'uri' : chat_session .uri ,
@@ -96,20 +96,19 @@ def post(self, request, *args, **kwargs):
96
96
'obj' : chat_session_message .id ,
97
97
'short_description' : 'You a new message' , 'silent' : True ,
98
98
'extra_data' : {
99
- 'uri' : chat_session .uri ,
99
+ notifs_settings .NOTIFICATIONS_WEBSOCKET_URL_PARAM :
100
+ chat_session .uri ,
100
101
'message' : chat_session_message .to_json ()
101
102
}
102
103
}
103
- notify .send (
104
- sender = self .__class__ ,** notif_args , channels = ['websocket' ]
105
- )
104
+ notify (** notif_args , channels = ['websocket' ])
106
105
107
- return Response ({
106
+ return Response ({
108
107
'status' : 'SUCCESS' , 'uri' : chat_session .uri , 'message' : message ,
109
108
'user' : deserialize_user (user )
110
109
})
111
110
112
111
113
112
def raise_404 (request ):
114
113
"""Raise a 404 Error."""
115
- raise Http404
114
+ raise Http404
0 commit comments