Fakultas Ilmu Komputer UI

Commit 7f88c0c4 authored by Joshua Casey's avatar Joshua Casey
Browse files

Merge branch 'Features/CompanyVacancyApplicationList' of...

Merge branch 'Features/CompanyVacancyApplicationList' of https://gitlab.com/PPL2017csui/PPLA1 into Features/StudentTranscript
parents b324f522 ca128936
......@@ -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")
......@@ -122,3 +122,19 @@ class IsAdminOrSupervisorOrCompanyOrSelf(permissions.IsAuthenticated):
)
return hasattr(user, "student") and user.student == student
return False
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):
"""
......
......@@ -22,9 +22,15 @@ from rest_framework import routers
from rest_framework_swagger.views import get_swagger_view
from core import apps
<<<<<<< HEAD
from core.views.accounts import StudentViewSet, CompanyViewSet, SupervisorViewSet, UserViewSet, LoginViewSet, \
CompanyRegisterViewSet, StudentProfileViewSet
from core.views.vacancies import VacancyViewSet, BookmarkedVacancyByStudentViewSet, ApplicationViewSet, CompanyApplicationViewSet, CompanyVacanciesViewSet
=======
from core.views.accounts import StudentViewSet, CompanyViewSet, SupervisorViewSet, UserViewSet, LoginViewSet, CompanyRegisterViewSet
from core.views.vacancies import VacancyViewSet, BookmarkedVacancyByStudentViewSet, ApplicationViewSet, \
CompanyApplicationViewSet, CompanyVacanciesViewSet, CompanyApplicationStatusViewSet
>>>>>>> ca12893633723b0b5543f4e23d9be11a968295a9
schema_view = get_swagger_view()
router = routers.DefaultRouter()
......@@ -36,6 +42,7 @@ router.register(r'login', LoginViewSet)
router.register(r'register', CompanyRegisterViewSet)
router.register(r'vacancies', VacancyViewSet)
router.register(r'profiles/students', StudentProfileViewSet)
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