Fakultas Ilmu Komputer UI

Commit fc89115f authored by Zamil Majdy's avatar Zamil Majdy
Browse files

Merge branch 'UserStory2' of https://gitlab.com/PPL2017csui/PPLA1 into UserStory2

parents 0ca095b4 86332029
......@@ -46,3 +46,10 @@ class LoginSerializer(serializers.HyperlinkedModelSerializer):
model = User
fields = ('url', 'username', 'email', 'is_staff', 'company', 'supervisor', 'student')
class RegisterSerializer(serializers.HyperlinkedModelSerializer):
company = CompanySerializer()
class Meta:
model = User
fields = ('url', 'username', 'email', 'is_staff', 'company')
import requests_mock
from rest_framework import status
from rest_framework.test import APITestCase
from rest_framework.test import APIClient, APITestCase
from django.contrib.auth.models import User
from core.models.accounts import Company
from core.models.accounts import Company, Supervisor
class LoginTests(APITestCase):
@requests_mock.Mocker()
def test_succesful_student_login_relogin(self, m):
"""
Ensure we can login
"""
m.post('https://api.cs.ui.ac.id/authentication/ldap/v2/', json={
"username": "dummy.mahasiswa",
......@@ -64,13 +61,18 @@ class LoginTests(APITestCase):
def test_success_company_login(self):
new_user = User.objects.create_user('dummy.login.company', 'dummy.login.company@company.com', 'lalala123')
new_company = Company.objects.create(user=new_user, description="lalalala", verified=True, logo=None, alamat=None)
new_company = Company.objects.create(user=new_user, description="lalalala", verified=True, logo=None, address=None)
url = '/api/login/'
response = self.client.post(url, {'username': 'dummy.login.company', 'password': 'lalala123', 'login-type': 'company'}, format='json')
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_bad_request_1(self):
url = '/api/login/'
response = self.client.post(url, {'username': 'lalala'}, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_bad_request(self):
def test_bad_request_2(self):
url = '/api/login/'
response = self.client.post(url, {'uesrname': 'lalala'}, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
\ No newline at end of file
response = self.client.post(url, {'username': 'lalala', 'password': 'lalalala', 'login-type' : 'lalala'}, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
......@@ -48,7 +48,7 @@ class ApplicationTests(APITestCase):
student_id = response.data.get('student').get('id')
new_user = User.objects.create_user('dummy.company', 'dummy.company@company.com', 'lalala123')
new_company = Company.objects.create(user=new_user, description="lalala",verified=True,logo=None,alamat=None)
new_company = Company.objects.create(user=new_user, description="lalala",verified=True,logo=None,address=None)
new_vacancy = Vacancy.objects.create(company=new_company, verified=True, open_time=datetime.fromtimestamp(0), description="lalala", close_time=datetime.today())
url = '/api/students/' + str(student_id) + '/applications/'
......@@ -101,7 +101,7 @@ class BookmarkApplicationTests(APITestCase):
student_id = response.data.get('student').get('id')
new_user = User.objects.create_user('dummy.company', 'dummy.company@company.com', 'lalala123')
new_company = Company.objects.create(user=new_user, description="lalala",verified=True,logo=None,alamat=None)
new_company = Company.objects.create(user=new_user, description="lalala",verified=True,logo=None,address=None)
new_vacancy = Vacancy.objects.create(company=new_company, verified=True, open_time=datetime.fromtimestamp(0), description="lalala", close_time=datetime.today())
url = '/api/students/' + str(student_id) + '/bookmarked-vacancies/'
......
......@@ -6,11 +6,12 @@ from rest_framework.decorators import list_route
from rest_framework.permissions import AllowAny
from rest_framework.permissions import IsAdminUser, IsAuthenticated
from rest_framework.response import Response
from rest_framework.parsers import FormParser,MultiPartParser
from core.lib.permissions import IsAdminOrStudent, IsAdminOrSelfOrReadOnly, IsAdminOrCompany, IsAdminOrSupervisor
from core.models.accounts import Student, Company, Supervisor
from core.serializers.accounts import UserSerializer, StudentSerializer, CompanySerializer, SupervisorSerializer, \
LoginSerializer
LoginSerializer, RegisterSerializer
class UserViewSet(viewsets.ModelViewSet):
......@@ -20,6 +21,9 @@ class UserViewSet(viewsets.ModelViewSet):
@list_route(methods=['get'], permission_classes=[IsAuthenticated])
def me(self, request):
"""
Get current user's details
"""
user = self.request.user
serializer = UserSerializer(user, context={"request": request})
return Response(serializer.data)
......@@ -46,23 +50,13 @@ class StudentViewSet(viewsets.ModelViewSet):
class CompanyViewSet(viewsets.ModelViewSet):
queryset = Company.objects.all()
serializer_class = CompanySerializer
permission_classes = [IsAdminUser]
def get_permissions(self):
if self.action == "update":
return [IsAdminOrSelfOrReadOnly(), IsAdminOrCompany()]
return super(CompanyViewSet, self).get_permissions()
permission_classes = [IsAdminOrSelfOrReadOnly, IsAdminOrCompany]
class SupervisorViewSet(viewsets.ModelViewSet):
queryset = Supervisor.objects.all()
serializer_class = SupervisorSerializer
permission_classes = [IsAdminUser]
def get_permissions(self):
if self.action == "update":
return [IsAdminOrSelfOrReadOnly(), IsAdminOrSupervisor()]
return super(SupervisorViewSet, self).get_permissions()
permission_classes = [IsAdminOrSelfOrReadOnly, IsAdminOrSupervisor]
class LoginViewSet(viewsets.GenericViewSet):
......@@ -72,14 +66,14 @@ class LoginViewSet(viewsets.GenericViewSet):
def create(self, request):
"""
Authenticate user by logging in
Authentication for user by means of logging in
---
parameters:
- name: body
description: JSON object containing three strings: username, password and login-type. login-type should be either 'sso-ui' or 'company'.
required: true
paramType: body
pytype: RequestSerializer
- name: body
description: JSON object containing three strings: username, password and login-type. login-type should be either 'sso-ui' or 'company'.
required: true
type: string
paramType: body
"""
username = request.data.get('username')
password = request.data.get('password')
......@@ -136,3 +130,42 @@ class LoginViewSet(viewsets.GenericViewSet):
return Response(status=status.HTTP_401_UNAUTHORIZED)
else:
return Response(status=status.HTTP_400_BAD_REQUEST)
class CompanyRegisterViewSet(viewsets.GenericViewSet):
permission_classes = (AllowAny,)
serializer_class = RegisterSerializer
queryset = Company.objects.all()
parser_classes = (MultiPartParser, FormParser,)
def create(self, request):
username = request.data.get('username')
password = request.data.get('password')
email = request.data.get('email')
company_name = request.data.get('name')
description = request.data.get('description')
logo = request.data.get('logo')
address = request.data.get('address')
if username is not None and password is not None and email is not None and company_name is not None and description is not None:
user, created = User.objects.get_or_create(
username=username,
email=email,
first_name=company_name,
last_name=""
)
if created:
user.set_password(password)
company = Company.objects.create(
user=user,
description=description,
logo=logo,
address=address
)
user.save()
company.save()
serializer = RegisterSerializer(user, context={'request': request})
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(status=status.HTTP_409_CONFLICT)
return Response(status=status.HTTP_400_BAD_REQUEST)
\ No newline at end of file
......@@ -26,11 +26,25 @@ class ApplicationViewSet(viewsets.GenericViewSet):
permission_classes = [IsAdminOrStudent]
def list(self, request, student_id):
"""
Get list of a student {student_id}'s application
---
"""
student = get_object_or_404(Student.objects.all(), pk=student_id)
vacancies = student.applied_vacancies
return Response(VacancySerializer(vacancies, many=True, context={'request': request}).data)
def create(self, request, student_id):
"""
Create a new application for student {student_id}
---
parameters:
- name: body
description: JSON object containing only one string: vacancy_id
required: true
type: string
paramType: body
"""
cover_letter = request.data.get('cover_letter')
vacancy = get_object_or_404(Vacancy.objects.all(), pk=request.data.get('vacancy_id'))
student = get_object_or_404(Student.objects.all(), pk=student_id)
......@@ -41,6 +55,10 @@ class ApplicationViewSet(viewsets.GenericViewSet):
return Response(ApplicationSerializer(application, context={'request': request}).data)
def destroy(self, request, student_id, pk):
"""
Remove a application {id} for student {student_id}
---
"""
vacancy = get_object_or_404(Vacancy.objects.all(), pk=pk)
student = get_object_or_404(Student.objects.all(), pk=student_id)
application = get_object_or_404(Application.objects.all(), student=student, vacancy=vacancy)
......@@ -53,17 +71,35 @@ class BookmarkedVacancyByStudentViewSet(viewsets.GenericViewSet):
permission_classes = [IsAdminOrStudent]
def list(self, request, student_id):
"""
Get list of a student {student_id}'s bookmarked vacancies
---
"""
student = get_object_or_404(Student.objects.all(), pk=student_id)
vacancies = self.serializer_class(student.bookmarked_vacancies, many=True, context={'request': request})
return Response(vacancies.data)
def create(self, request, student_id):
"""
Bookmarks a vacancy for student {student_id}
---
parameters:
- name: body
description: JSON object containing only one string: vacancy_id
required: true
type: string
paramType: body
"""
vacancy = get_object_or_404(Vacancy.objects.all(), pk=request.data['vacancy_id'])
student = get_object_or_404(Student.objects.all(), pk=student_id)
student.bookmarked_vacancies.add(vacancy)
return Response(self.serializer_class(student.bookmarked_vacancies, many=True, context={'request': request}).data)
def destroy(self, request, student_id, pk):
"""
Remove bookmark {id} for student {student_id}
---
"""
vacancy = get_object_or_404(Vacancy.objects.all(), pk=pk)
student = get_object_or_404(Student.objects.all(), pk=student_id)
student.bookmarked_vacancies.remove(vacancy)
......
......@@ -22,7 +22,7 @@ from rest_framework import routers
from rest_framework_swagger.views import get_swagger_view
from core import apps
from core.views.accounts import StudentViewSet, CompanyViewSet, SupervisorViewSet, UserViewSet, LoginViewSet
from core.views.accounts import StudentViewSet, CompanyViewSet, SupervisorViewSet, UserViewSet, LoginViewSet, CompanyRegisterViewSet
from core.views.vacancies import VacancyViewSet, BookmarkedVacancyByStudentViewSet, ApplicationViewSet
schema_view = get_swagger_view()
......@@ -32,6 +32,7 @@ router.register(r'students', StudentViewSet)
router.register(r'companies', CompanyViewSet)
router.register(r'supervisors', SupervisorViewSet)
router.register(r'login', LoginViewSet)
router.register(r'register', CompanyRegisterViewSet)
router.register(r'vacancies', VacancyViewSet)
router.register(r'students/(?P<student_id>\d+)/bookmarked-vacancies', BookmarkedVacancyByStudentViewSet,
base_name='bookmarked-vacancy-list')
......
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