Commit 3e309da463cc489bf651ab0dae02c314e09eea55
1 parent
60cf51fe
Exists in
master
and in
2 other branches
Adding subjects and participants list api access
Showing
4 changed files
with
104 additions
and
42 deletions
Show diff stats
api/urls.py
| ... | ... | @@ -13,6 +13,7 @@ router = routers.DefaultRouter() |
| 13 | 13 | router.register(r'logs', LogViewSet) |
| 14 | 14 | router.register(r'usersapi', UserViewSet) |
| 15 | 15 | router.register(r'users', views.LoginViewset) |
| 16 | +router.register(r'subjects', views.SubjectViewset) | |
| 16 | 17 | |
| 17 | 18 | urlpatterns = [ |
| 18 | 19 | #API REST | ... | ... |
api/views.py
| ... | ... | @@ -9,6 +9,9 @@ from rest_framework.permissions import IsAuthenticated, IsAuthenticatedOrReadOnl |
| 9 | 9 | |
| 10 | 10 | from security.models import Security |
| 11 | 11 | |
| 12 | +from subjects.serializers import SubjectSerializer | |
| 13 | +from subjects.models import Subject | |
| 14 | + | |
| 12 | 15 | from users.serializers import UserSerializer |
| 13 | 16 | from users.models import User |
| 14 | 17 | |
| ... | ... | @@ -16,37 +19,6 @@ from oauth2_provider.views.generic import ProtectedResourceView |
| 16 | 19 | from oauth2_provider.models import Application |
| 17 | 20 | from django.http import HttpResponse |
| 18 | 21 | |
| 19 | -class LoginViewset(viewsets.ReadOnlyModelViewSet): | |
| 20 | - queryset = User.objects.all() | |
| 21 | - permissions_classes = (IsAuthenticated,) | |
| 22 | - | |
| 23 | - @csrf_exempt | |
| 24 | - @list_route(methods = ['POST'], permissions_classes = [IsAuthenticated]) | |
| 25 | - def login(self, request): | |
| 26 | - username = request.data['email'] | |
| 27 | - | |
| 28 | - user = self.queryset.get(email = username) | |
| 29 | - | |
| 30 | - if not user is None: | |
| 31 | - serializer = UserSerializer(user) | |
| 32 | - | |
| 33 | - json_r = json.dumps(serializer.data) | |
| 34 | - json_r = json.loads(json_r) | |
| 35 | - | |
| 36 | - user_info = {} | |
| 37 | - user_info["data"] = json_r | |
| 38 | - | |
| 39 | - user_info["message"] = "" | |
| 40 | - user_info["type"] = "" | |
| 41 | - user_info["title"] = "" | |
| 42 | - user_info["success"] = True | |
| 43 | - user_info["number"] = 1 | |
| 44 | - user_info['extra'] = 0 | |
| 45 | - | |
| 46 | - response = json.dumps(user_info) | |
| 47 | - | |
| 48 | - return HttpResponse(response) | |
| 49 | - | |
| 50 | 22 | @csrf_exempt |
| 51 | 23 | def getToken(request): |
| 52 | 24 | oauth = Application.objects.filter(name = "amadeus-droid") |
| ... | ... | @@ -91,4 +63,80 @@ def getToken(request): |
| 91 | 63 | except KeyError: |
| 92 | 64 | response = "Error" |
| 93 | 65 | |
| 94 | - return HttpResponse(response) | |
| 95 | 66 | \ No newline at end of file |
| 67 | + return HttpResponse(response) | |
| 68 | + | |
| 69 | +class LoginViewset(viewsets.ReadOnlyModelViewSet): | |
| 70 | + queryset = User.objects.all() | |
| 71 | + permissions_classes = (IsAuthenticated,) | |
| 72 | + | |
| 73 | + @csrf_exempt | |
| 74 | + @list_route(methods = ['POST'], permissions_classes = [IsAuthenticated]) | |
| 75 | + def login(self, request): | |
| 76 | + username = request.data['email'] | |
| 77 | + | |
| 78 | + user = self.queryset.get(email = username) | |
| 79 | + response = "" | |
| 80 | + | |
| 81 | + if not user is None: | |
| 82 | + serializer = UserSerializer(user) | |
| 83 | + | |
| 84 | + json_r = json.dumps(serializer.data) | |
| 85 | + json_r = json.loads(json_r) | |
| 86 | + | |
| 87 | + user_info = {} | |
| 88 | + user_info["data"] = json_r | |
| 89 | + | |
| 90 | + user_info["message"] = "" | |
| 91 | + user_info["type"] = "" | |
| 92 | + user_info["title"] = "" | |
| 93 | + user_info["success"] = True | |
| 94 | + user_info["number"] = 1 | |
| 95 | + user_info['extra'] = 0 | |
| 96 | + | |
| 97 | + response = json.dumps(user_info) | |
| 98 | + | |
| 99 | + return HttpResponse(response) | |
| 100 | + | |
| 101 | +class SubjectViewset(viewsets.ReadOnlyModelViewSet): | |
| 102 | + queryset = Subject.objects.all() | |
| 103 | + permissions_classes = (IsAuthenticated, ) | |
| 104 | + | |
| 105 | + @csrf_exempt | |
| 106 | + @list_route(methods = ['POST'], permissions_classes = [IsAuthenticated]) | |
| 107 | + def get_subjects(self, request): | |
| 108 | + username = request.data['email'] | |
| 109 | + | |
| 110 | + user = User.objects.get(email = username) | |
| 111 | + | |
| 112 | + subjects = None | |
| 113 | + | |
| 114 | + response = "" | |
| 115 | + | |
| 116 | + if not user is None: | |
| 117 | + if user.is_staff: | |
| 118 | + subjects = Subject.objects.all().order_by("name") | |
| 119 | + else: | |
| 120 | + pk = user.pk | |
| 121 | + | |
| 122 | + subjects = Subject.objects.filter(Q(students__pk=pk) | Q(professor__pk=pk) | Q(category__coordinators__pk=pk)).distinct() | |
| 123 | + | |
| 124 | + serializer = SubjectSerializer(subjects, many = True) | |
| 125 | + | |
| 126 | + json_r = json.dumps(serializer.data) | |
| 127 | + json_r = json.loads(json_r) | |
| 128 | + | |
| 129 | + sub_info = {} | |
| 130 | + | |
| 131 | + sub_info["data"] = {} | |
| 132 | + sub_info["data"]["subjects"] = json_r | |
| 133 | + | |
| 134 | + sub_info["message"] = "" | |
| 135 | + sub_info["type"] = "" | |
| 136 | + sub_info["title"] = "" | |
| 137 | + sub_info["success"] = True | |
| 138 | + sub_info["number"] = 1 | |
| 139 | + sub_info['extra'] = 0 | |
| 140 | + | |
| 141 | + response = json.dumps(sub_info) | |
| 142 | + | |
| 143 | + return HttpResponse(response) | |
| 96 | 144 | \ No newline at end of file | ... | ... |
subjects/models.py
| 1 | 1 | from django.db import models |
| 2 | +from django.db.models import Q | |
| 2 | 3 | |
| 3 | 4 | from autoslug.fields import AutoSlugField |
| 4 | 5 | |
| ... | ... | @@ -10,8 +11,10 @@ from django.core.exceptions import ValidationError |
| 10 | 11 | |
| 11 | 12 | from categories.models import Category |
| 12 | 13 | import datetime |
| 14 | + | |
| 13 | 15 | class Tag(models.Model): |
| 14 | 16 | name = models.CharField( _("Name"), unique = True,max_length= 200, blank = True) |
| 17 | + | |
| 15 | 18 | def __str__(self): |
| 16 | 19 | return self.name |
| 17 | 20 | |
| ... | ... | @@ -41,6 +44,7 @@ class Subject(models.Model): |
| 41 | 44 | category = models.ForeignKey(Category, related_name="subject_category", null=True) |
| 42 | 45 | |
| 43 | 46 | max_upload_size = models.IntegerField(_("Maximum upload size"), default=1024, null=True) |
| 47 | + | |
| 44 | 48 | class Meta: |
| 45 | 49 | verbose_name = "Subject" |
| 46 | 50 | verbose_name_plural = "Subjects" |
| ... | ... | @@ -49,11 +53,11 @@ class Subject(models.Model): |
| 49 | 53 | def __str__(self): |
| 50 | 54 | return self.name |
| 51 | 55 | |
| 52 | - # def clean(self): | |
| 53 | - | |
| 54 | - # if self.subscribe_begin > self.end_date: | |
| 55 | - # raise ValidationError(_('Subscribe period should be between course time')) | |
| 56 | - | |
| 57 | - | |
| 58 | - | |
| 59 | - | |
| 56 | + def get_participants(self): | |
| 57 | + data = User.objects.filter( | |
| 58 | + Q(is_staff = True) | Q(subject_student__slug = self.slug) | | |
| 59 | + Q(professors__slug = self.slug) | | |
| 60 | + Q(coordinators__subject_category__slug = self.slug) | |
| 61 | + ).distinct() | |
| 62 | + | |
| 63 | + return data | |
| 60 | 64 | \ No newline at end of file | ... | ... |
subjects/serializers.py
| 1 | 1 | from rest_framework import serializers |
| 2 | 2 | |
| 3 | -from .models import Tag | |
| 3 | +from .models import Subject, Tag | |
| 4 | + | |
| 5 | +from users.serializers import UserSerializer | |
| 4 | 6 | |
| 5 | 7 | class TagSerializer(serializers.ModelSerializer): |
| 6 | 8 | def validate(self, data): |
| ... | ... | @@ -21,4 +23,11 @@ class TagSerializer(serializers.ModelSerializer): |
| 21 | 23 | "validators": [], |
| 22 | 24 | }, |
| 23 | 25 | } |
| 24 | - validators = [] | |
| 25 | 26 | \ No newline at end of file |
| 27 | + validators = [] | |
| 28 | + | |
| 29 | +class SubjectSerializer(serializers.ModelSerializer): | |
| 30 | + participants = UserSerializer(many = True, source = 'get_participants') | |
| 31 | + | |
| 32 | + class Meta: | |
| 33 | + model = Subject | |
| 34 | + fields = ["name", "slug", "visible", "participants"] | |
| 26 | 35 | \ No newline at end of file | ... | ... |