Commit 674108cb6370af1c6f5f3af5757f1c29d56bc83e

Authored by Zambom
1 parent 3e309da4

Changing the way that subjects and participants are loaded in api

api/urls.py
... ... @@ -14,6 +14,7 @@ router.register(r'logs', LogViewSet)
14 14 router.register(r'usersapi', UserViewSet)
15 15 router.register(r'users', views.LoginViewset)
16 16 router.register(r'subjects', views.SubjectViewset)
  17 +router.register(r'participants', views.ParticipantsViewset)
17 18  
18 19 urlpatterns = [
19 20 #API REST
... ...
api/views.py
... ... @@ -2,11 +2,14 @@ import requests, json
2 2 from django.shortcuts import get_object_or_404, reverse
3 3 from django.contrib.auth import authenticate
4 4 from django.views.decorators.csrf import csrf_exempt
  5 +
5 6 from rest_framework import viewsets
6 7 from rest_framework.response import Response
7 8 from rest_framework.decorators import detail_route, list_route
8 9 from rest_framework.permissions import IsAuthenticated, IsAuthenticatedOrReadOnly
9 10  
  11 +from django.db.models import Q
  12 +
10 13 from security.models import Security
11 14  
12 15 from subjects.serializers import SubjectSerializer
... ... @@ -139,4 +142,42 @@ class SubjectViewset(viewsets.ReadOnlyModelViewSet):
139 142  
140 143 response = json.dumps(sub_info)
141 144  
  145 + return HttpResponse(response)
  146 +
  147 +class ParticipantsViewset(viewsets.ReadOnlyModelViewSet):
  148 + queryset = User.objects.all()
  149 + permissions_classes = (IsAuthenticated, )
  150 +
  151 + @csrf_exempt
  152 + @list_route(methods = ['POST'], permissions_classes = [IsAuthenticated])
  153 + def get_participants(self, request):
  154 + username = request.data['email']
  155 + subject_slug = request.data['subject_slug']
  156 +
  157 + participants = None
  158 +
  159 + response = ""
  160 +
  161 + if not subject_slug == "":
  162 + 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()
  163 +
  164 + serializer = UserSerializer(participants, many = True)
  165 +
  166 + json_r = json.dumps(serializer.data)
  167 + json_r = json.loads(json_r)
  168 +
  169 + info = {}
  170 +
  171 + info["data"] = {}
  172 + info["data"]["participants"] = json_r
  173 +
  174 + info["message"] = ""
  175 + info["type"] = ""
  176 + info["title"] = ""
  177 + info["success"] = True
  178 + info["number"] = 1
  179 + info['extra'] = 0
  180 +
  181 + response = json.dumps(info)
  182 +
142 183 return HttpResponse(response)
143 184 \ No newline at end of file
... ...
subjects/serializers.py
... ... @@ -26,8 +26,7 @@ class TagSerializer(serializers.ModelSerializer):
26 26 validators = []
27 27  
28 28 class SubjectSerializer(serializers.ModelSerializer):
29   - participants = UserSerializer(many = True, source = 'get_participants')
30   -
  29 +
31 30 class Meta:
32 31 model = Subject
33   - fields = ["name", "slug", "visible", "participants"]
34 32 \ No newline at end of file
  33 + fields = ["name", "slug", "visible"]
35 34 \ No newline at end of file
... ...
users/serializers.py
... ... @@ -60,5 +60,5 @@ class UserBackupSerializer(serializers.ModelSerializer):
60 60 class UserSerializer(serializers.ModelSerializer):
61 61 class Meta:
62 62 model = User
63   - fields = ('username','email','image','last_update','date_created','last_name','social_name',
  63 + fields = ('username','email','image_url','last_update','date_created','last_name','social_name',
64 64 'is_staff','is_active','description')
... ...