Fakultas Ilmu Komputer UI

Commit ca128936 authored by Joshua Casey's avatar Joshua Casey
Browse files

[#140656091] #30 Added API for application count and status

parent 51d5ea07
......@@ -4,7 +4,7 @@ from rest_framework.exceptions import APIException
from core.models import Company
from core.models import Student
from core.models import Supervisor
from core.models import Application
def is_admin_or_student(user):
return user.is_superuser or hasattr(user, "student")
......@@ -94,3 +94,19 @@ class IsAdminOrCompany(permissions.BasePermission):
)
return hasattr(user, "company") and user.company == company
class IsAdminOrVacancyOwner(permissions.BasePermission):
def has_permission(self, request, view):
return is_admin_or_company(request.user)
def has_object_permission(self, request, view, obj):
user = request.user
if user.is_superuser:
return True
if isinstance(obj, Application):
return user.company == obj.vacancy.company
else:
raise APIException(
"Checking owner permission on non-application object"
)
# -*- coding: utf-8 -*-
# Generated by Django 1.10.5 on 2017-04-25 15:50
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('core', '0006_auto_20170328_1950'),
]
operations = [
migrations.RemoveField(
model_name='application',
name='allow_transcript',
),
migrations.AddField(
model_name='application',
name='status',
field=models.IntegerField(default=0),
),
]
......@@ -15,10 +15,16 @@ class Vacancy(models.Model):
class Application(models.Model):
NEW = 0
READ = 1
BOOKMARKED = 2
REJECTED = 3
ACCEPTED = 4
cover_letter = models.TextField(null=True, blank=True)
allow_transcript = models.BooleanField(null=False, default=True)
student = models.ForeignKey(Student, on_delete=models.CASCADE)
vacancy = models.ForeignKey(Vacancy, on_delete=models.CASCADE)
status = models.IntegerField(default=NEW)
class Meta:
unique_together = (("student", "vacancy"),)
......
......@@ -24,5 +24,8 @@ class ApplicationSerializer(serializers.ModelSerializer):
fields = '__all__'
class ApplicationStatusSerializer(serializers.ModelSerializer):
class Meta:
model = Application
fields = ['status']
from rest_framework import viewsets
from rest_framework import viewsets, status
from rest_framework.exceptions import ValidationError
from rest_framework.generics import get_object_or_404
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.decorators import detail_route
from core.lib.permissions import IsAdminOrStudent, IsAdminOrCompany
from core.lib.permissions import IsAdminOrStudent, IsAdminOrCompany, IsAdminOrVacancyOwner
from core.models import Student, Company
from core.models.vacancies import Vacancy, Application
from core.serializers.vacancies import VacancySerializer, ApplicationSerializer
from core.serializers.vacancies import VacancySerializer, ApplicationSerializer, ApplicationStatusSerializer
class VacancyViewSet(viewsets.ModelViewSet):
......@@ -28,6 +29,13 @@ class VacancyViewSet(viewsets.ModelViewSet):
return Response(VacancySerializer(vacancies, many=True, context={'request': request}).data)
@detail_route(permission_classes=[IsAdminOrCompany])
def count(self, request, pk=None):
vacancy = self.get_object()
count = Application.objects.filter(vacancy=vacancy).count()
count_new = Application.objects.filter(vacancy=vacancy, status=Application.NEW).count()
return Response({"count": count, "count_new": count_new}, status=status.HTTP_200_OK)
class ApplicationViewSet(viewsets.GenericViewSet):
serializer_class = ApplicationSerializer
......@@ -76,6 +84,7 @@ class ApplicationViewSet(viewsets.GenericViewSet):
class CompanyApplicationViewSet(viewsets.GenericViewSet):
queryset = Application.objects.all()
permission_classes = [IsAdminOrCompany]
def list(self, request, company_id):
"""
......@@ -88,8 +97,23 @@ class CompanyApplicationViewSet(viewsets.GenericViewSet):
return Response(ApplicationSerializer(applications, many=True, context={'request': request}).data)
class CompanyApplicationStatusViewSet(viewsets.GenericViewSet):
queryset = Application.objects.all()
serializer_class = ApplicationStatusSerializer
permission_classes = [IsAdminOrVacancyOwner]
def partial_update(self, request, pk=None):
application = self.get_object()
serializer = self.get_serializer_class()(application, data=request.data, partial=True)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_202_ACCEPTED)
else:
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class CompanyVacanciesViewSet(viewsets.GenericViewSet):
queryset = Vacancy. objects.all()
queryset = Vacancy.objects.all()
def list(self, request, company_id):
"""
......
......@@ -23,7 +23,8 @@ from rest_framework_swagger.views import get_swagger_view
from core import apps
from core.views.accounts import StudentViewSet, CompanyViewSet, SupervisorViewSet, UserViewSet, LoginViewSet, CompanyRegisterViewSet
from core.views.vacancies import VacancyViewSet, BookmarkedVacancyByStudentViewSet, ApplicationViewSet, CompanyApplicationViewSet, CompanyVacanciesViewSet
from core.views.vacancies import VacancyViewSet, BookmarkedVacancyByStudentViewSet, ApplicationViewSet, \
CompanyApplicationViewSet, CompanyVacanciesViewSet, CompanyApplicationStatusViewSet
schema_view = get_swagger_view()
router = routers.DefaultRouter()
......@@ -34,6 +35,7 @@ router.register(r'supervisors', SupervisorViewSet)
router.register(r'login', LoginViewSet)
router.register(r'register', CompanyRegisterViewSet)
router.register(r'vacancies', VacancyViewSet)
router.register(r'applications', CompanyApplicationStatusViewSet)
router.register(r'students/(?P<student_id>\d+)/bookmarked-vacancies', BookmarkedVacancyByStudentViewSet,
base_name='bookmarked-vacancy-list')
router.register(r'students/(?P<student_id>\d+)/applied-vacancies', ApplicationViewSet,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment