Commit 4979135894e955547a3565ecc99af745530c5643
1 parent
2ace8926
Exists in
master
and in
2 other branches
Adding subject and participants badge in app
Showing
3 changed files
with
38 additions
and
5 deletions
Show diff stats
api/views.py
... | ... | @@ -65,7 +65,7 @@ def getToken(request): |
65 | 65 | } |
66 | 66 | |
67 | 67 | auth = (oauth.client_id, oauth.client_secret) |
68 | - | |
68 | + | |
69 | 69 | response = requests.post(request.build_absolute_uri(reverse('oauth2_provider:token')), data = data, auth = auth) |
70 | 70 | |
71 | 71 | json_r = json.loads(response.content.decode('utf-8')) |
... | ... | @@ -169,7 +169,7 @@ class SubjectViewset(viewsets.ReadOnlyModelViewSet): |
169 | 169 | |
170 | 170 | subjects = Subject.objects.filter(Q(students__pk=pk) | Q(professor__pk=pk) | Q(category__coordinators__pk=pk)).distinct() |
171 | 171 | |
172 | - serializer = SubjectSerializer(subjects, many = True) | |
172 | + serializer = SubjectSerializer(subjects, many = True, context = {"request_user": user}) | |
173 | 173 | |
174 | 174 | json_r = json.dumps(serializer.data) |
175 | 175 | json_r = json.loads(json_r) |
... | ... | @@ -207,7 +207,7 @@ class ParticipantsViewset(viewsets.ReadOnlyModelViewSet): |
207 | 207 | if not subject_slug == "": |
208 | 208 | participants = User.objects.filter(Q(is_staff = True) | Q(subject_student__slug = subject_slug) | Q(professors__slug = subject_slug) | Q(coordinators__subject_category__slug = subject_slug)).exclude(email = username).distinct() |
209 | 209 | |
210 | - serializer = UserSerializer(participants, many = True) | |
210 | + serializer = UserSerializer(participants, many = True, context = {"request_user": username}) | |
211 | 211 | |
212 | 212 | json_r = json.dumps(serializer.data) |
213 | 213 | json_r = json.loads(json_r) | ... | ... |
subjects/serializers.py
1 | +import datetime | |
2 | +from django.db.models import Q | |
3 | + | |
1 | 4 | from rest_framework import serializers |
2 | 5 | |
6 | +from chat.models import ChatVisualizations | |
7 | + | |
3 | 8 | from .models import Subject, Tag |
4 | 9 | |
5 | 10 | class TagSerializer(serializers.ModelSerializer): |
... | ... | @@ -24,7 +29,16 @@ class TagSerializer(serializers.ModelSerializer): |
24 | 29 | validators = [] |
25 | 30 | |
26 | 31 | class SubjectSerializer(serializers.ModelSerializer): |
32 | + notifications = serializers.SerializerMethodField() | |
33 | + | |
34 | + def get_notifications(self, subject): | |
35 | + user = self.context.get("request_user", None) | |
36 | + | |
37 | + if not user is None: | |
38 | + return ChatVisualizations.objects.filter(Q(user = user) & Q(viewed = False) & Q(message__subject = subject) & (Q(user__is_staff = True) | Q(message__subject__students = user) | Q(message__subject__professor = user) | Q(message__subject__category__coordinators = user))).distinct().count() | |
39 | + | |
40 | + return 0 | |
27 | 41 | |
28 | 42 | class Meta: |
29 | 43 | model = Subject |
30 | - fields = ["name", "slug", "visible", "description_brief", "description"] | |
31 | 44 | \ No newline at end of file |
45 | + fields = ["name", "slug", "visible", "description_brief", "description", "notifications"] | |
32 | 46 | \ No newline at end of file | ... | ... |
users/serializers.py
1 | 1 | import os |
2 | 2 | import zipfile |
3 | 3 | import time |
4 | +from django.db.models import Q | |
4 | 5 | from django.conf import settings |
5 | 6 | from django.core.files import File |
7 | + | |
6 | 8 | from rest_framework import serializers |
7 | 9 | |
8 | 10 | from log.serializers import LogSerializer |
9 | 11 | from log.models import Log |
10 | 12 | |
13 | +from chat.models import Conversation, ChatVisualizations | |
14 | + | |
11 | 15 | from .models import User |
12 | 16 | |
13 | 17 | class UserBackupSerializer(serializers.ModelSerializer): |
... | ... | @@ -58,7 +62,22 @@ class UserBackupSerializer(serializers.ModelSerializer): |
58 | 62 | validators = [] |
59 | 63 | |
60 | 64 | class UserSerializer(serializers.ModelSerializer): |
65 | + unseen_msgs = serializers.SerializerMethodField() | |
66 | + | |
67 | + def get_unseen_msgs(self, user_to): | |
68 | + user = self.context.get('request_user', None) | |
69 | + | |
70 | + if not user is None: | |
71 | + chat = Conversation.objects.filter((Q(user_one__email = user) & Q(user_two = user_to)) | (Q(user_one = user_to) & Q(user_two__email = user))) | |
72 | + | |
73 | + if chat.count() > 0: | |
74 | + chat = chat[0] | |
75 | + | |
76 | + return ChatVisualizations.objects.filter(message__talk = chat, user__email = user, viewed = False).count() | |
77 | + | |
78 | + return 0 | |
79 | + | |
61 | 80 | class Meta: |
62 | 81 | model = User |
63 | 82 | fields = ('username','email','image_url','last_update','date_created','last_name','social_name', |
64 | - 'is_staff','is_active','description') | |
83 | + 'is_staff','is_active','description','unseen_msgs') | ... | ... |