Commit f28b459bc92159a456b7de3ef7ff376939ca12ee
1 parent
7558958e
Exists in
master
and in
2 other branches
Adding function to get chat messages to api (initial)
Showing
4 changed files
with
57 additions
and
2 deletions
Show diff stats
api/urls.py
@@ -15,6 +15,7 @@ router.register(r'usersapi', UserViewSet) | @@ -15,6 +15,7 @@ router.register(r'usersapi', UserViewSet) | ||
15 | router.register(r'users', views.LoginViewset) | 15 | router.register(r'users', views.LoginViewset) |
16 | router.register(r'subjects', views.SubjectViewset) | 16 | router.register(r'subjects', views.SubjectViewset) |
17 | router.register(r'participants', views.ParticipantsViewset) | 17 | router.register(r'participants', views.ParticipantsViewset) |
18 | +router.register(r'chat', views.ChatViewset) | ||
18 | 19 | ||
19 | urlpatterns = [ | 20 | urlpatterns = [ |
20 | #API REST | 21 | #API REST |
api/views.py
@@ -12,6 +12,9 @@ from django.db.models import Q | @@ -12,6 +12,9 @@ from django.db.models import Q | ||
12 | 12 | ||
13 | from security.models import Security | 13 | from security.models import Security |
14 | 14 | ||
15 | +from chat.serializers import ChatSerializer | ||
16 | +from chat.models import TalkMessages | ||
17 | + | ||
15 | from subjects.serializers import SubjectSerializer | 18 | from subjects.serializers import SubjectSerializer |
16 | from subjects.models import Subject | 19 | from subjects.models import Subject |
17 | 20 | ||
@@ -180,4 +183,42 @@ class ParticipantsViewset(viewsets.ReadOnlyModelViewSet): | @@ -180,4 +183,42 @@ class ParticipantsViewset(viewsets.ReadOnlyModelViewSet): | ||
180 | 183 | ||
181 | response = json.dumps(info) | 184 | response = json.dumps(info) |
182 | 185 | ||
186 | + return HttpResponse(response) | ||
187 | + | ||
188 | +class ChatViewset(viewsets.ModelViewSet): | ||
189 | + queryset = TalkMessages.objects.all() | ||
190 | + permissions_classes = (IsAuthenticated, ) | ||
191 | + | ||
192 | + @csrf_exempt | ||
193 | + @list_route(methods = ['POST'], permissions_classes = [IsAuthenticated]) | ||
194 | + def get_messages(self, request): | ||
195 | + username = request.data['email'] | ||
196 | + user_two = request.data['user_two'] | ||
197 | + | ||
198 | + messages = None | ||
199 | + | ||
200 | + response = "" | ||
201 | + | ||
202 | + if not user_two == "": | ||
203 | + messages = TalkMessages.objects.filter((Q(talk__user_one__email = username) & Q(talk__user_two__email = user_two)) | (Q(talk__user_one__email = user_two) & Q(talk__user_two__email = username))).order_by('-create_date') | ||
204 | + | ||
205 | + serializer = ChatSerializer(messages, many = True) | ||
206 | + | ||
207 | + json_r = json.dumps(serializer.data) | ||
208 | + json_r = json.loads(json_r) | ||
209 | + | ||
210 | + info = {} | ||
211 | + | ||
212 | + info["data"] = {} | ||
213 | + info["data"]["messages"] = json_r | ||
214 | + | ||
215 | + info["message"] = "" | ||
216 | + info["type"] = "" | ||
217 | + info["title"] = "" | ||
218 | + info["success"] = True | ||
219 | + info["number"] = 1 | ||
220 | + info['extra'] = 0 | ||
221 | + | ||
222 | + response = json.dumps(info) | ||
223 | + | ||
183 | return HttpResponse(response) | 224 | return HttpResponse(response) |
184 | \ No newline at end of file | 225 | \ No newline at end of file |
@@ -0,0 +1,15 @@ | @@ -0,0 +1,15 @@ | ||
1 | +from rest_framework import serializers | ||
2 | + | ||
3 | +from .models import TalkMessages | ||
4 | + | ||
5 | +from subjects.serializers import SubjectSerializer | ||
6 | +from users.serializers import UserSerializer | ||
7 | + | ||
8 | +class ChatSerializer(serializers.ModelSerializer): | ||
9 | + user = UserSerializer() | ||
10 | + subject = SubjectSerializer() | ||
11 | + image = serializers.CharField(required = False, allow_blank = True, max_length = 255) | ||
12 | + | ||
13 | + class Meta: | ||
14 | + model = TalkMessages | ||
15 | + exclude = ["talk"] | ||
0 | \ No newline at end of file | 16 | \ No newline at end of file |
subjects/serializers.py
@@ -2,8 +2,6 @@ from rest_framework import serializers | @@ -2,8 +2,6 @@ from rest_framework import serializers | ||
2 | 2 | ||
3 | from .models import Subject, Tag | 3 | from .models import Subject, Tag |
4 | 4 | ||
5 | -from users.serializers import UserSerializer | ||
6 | - | ||
7 | class TagSerializer(serializers.ModelSerializer): | 5 | class TagSerializer(serializers.ModelSerializer): |
8 | def validate(self, data): | 6 | def validate(self, data): |
9 | query = Tag.objects.filter(name = data['name']) | 7 | query = Tag.objects.filter(name = data['name']) |