Fakultas Ilmu Komputer UI

Commit 2eb2a094 authored by Joshua Casey's avatar Joshua Casey
Browse files

[#140655219] [GREEN] #27 Implemented edit student profile API

parent 3dbac2a5
......@@ -16,7 +16,41 @@ class StudentSerializer(serializers.ModelSerializer):
class Meta:
model = Student
fields = ['id', 'name', 'user', 'npm', 'resume', 'phone_number', 'birth_place', 'birth_date', 'major', 'batch', 'show_transcript']
fields = ['id', 'name', 'user', 'npm', 'resume', 'phone_number', 'birth_place', 'birth_date', 'major', 'batch', \
'show_transcript', 'photo']
class StudentUpdateSerializer(serializers.ModelSerializer):
email = serializers.EmailField()
def to_representation(self, instance):
resume = None
photo = None
if instance.resume and hasattr(instance.resume, 'url'):
resume = instance.resume.url
if instance.photo and hasattr(instance.photo, 'url'):
photo = instance.photo.url
return {
'resume' : resume,
'email' : instance.user.email,
'phone_number' : instance.phone_number,
'photo' : photo,
'show_transcript' : instance.show_transcript
}
def update(self, instance, validated_data):
instance.resume = validated_data.get('resume', instance.resume)
instance.show_transcript = validated_data.get('show_transcript', instance.show_transcript)
instance.phone_number = validated_data.get('phone_number', instance.phone_number)
instance.photo = validated_data.get('photo', instance.photo)
instance.user.email = validated_data.get('email', instance.user.email)
instance.save()
instance.user.save()
return instance
class Meta:
model = Student
fields = ['resume', 'email', 'phone_number', 'photo', 'show_transcript']
class CompanySerializer(serializers.ModelSerializer):
......
......@@ -2,18 +2,18 @@ import requests
from django.contrib.auth import authenticate, login
from django.contrib.auth.models import User
from rest_framework import viewsets, status
from rest_framework.generics import get_object_or_404
from rest_framework.decorators import list_route
from rest_framework.parsers import FormParser,MultiPartParser
from rest_framework.permissions import AllowAny
from rest_framework.permissions import IsAdminUser, IsAuthenticated
from rest_framework.response import Response
from rest_framework.status import HTTP_400_BAD_REQUEST, HTTP_409_CONFLICT
from core.lib.permissions import IsAdminOrStudent, IsAdminOrSelfOrReadOnly, IsAdminOrCompany, IsAdminOrSupervisor, \
IsAdminOrSupervisorOrCompany, IsAdminOrSupervisorOrCompanyOrSelf
IsAdminOrSupervisorOrCompanyOrSelf
from core.models.accounts import Student, Company, Supervisor
from core.serializers.accounts import UserSerializer, StudentSerializer, CompanySerializer, SupervisorSerializer, \
LoginSerializer, RegisterSerializer
LoginSerializer, RegisterSerializer, StudentUpdateSerializer
class UserViewSet(viewsets.ModelViewSet):
......@@ -98,9 +98,11 @@ class LoginViewSet(viewsets.GenericViewSet):
last_name = " ".join(name)
user, created = User.objects.get_or_create(
username=username,
email=username + "@ui.ac.id",
first_name=first_name,
last_name=last_name
defaults={
'email' : username + "@ui.ac.id",
'first_name' : first_name,
'last_name' : last_name
}
)
user.set_password(password)
user.save()
......@@ -186,7 +188,7 @@ class CompanyRegisterViewSet(viewsets.GenericViewSet):
for attr in ['password', 'email', 'name', 'description', 'logo', 'address']:
data[attr] = request.data.get(attr)
if data[attr] is None:
return Response({'error': attr+' is required'}, status=HTTP_400_BAD_REQUEST)
return Response({'error': attr+' is required'}, status=status.HTTP_400_BAD_REQUEST)
user, created = User.objects.get_or_create(
username=data['email'],
......@@ -208,4 +210,20 @@ class CompanyRegisterViewSet(viewsets.GenericViewSet):
login(request, user)
return Response(serializer.data, status=status.HTTP_201_CREATED)
else:
return Response({'error': 'Company with email '+data['email']+' already exist'}, status=HTTP_409_CONFLICT)
return Response({'error': 'Company with email '+data['email']+' already exist'}, status=status.HTTP_409_CONFLICT)
class StudentProfileViewSet(viewsets.GenericViewSet):
queryset = Student.objects.all()
permission_classes = [IsAdminOrStudent]
serializer_class = StudentUpdateSerializer
parser_classes = (MultiPartParser, FormParser,)
def partial_update(self, request, pk=None):
user = self.get_object()
serializer = self.serializer_class(user, 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)
......@@ -22,7 +22,8 @@ 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, CompanyRegisterViewSet
from core.views.accounts import StudentViewSet, CompanyViewSet, SupervisorViewSet, UserViewSet, LoginViewSet, \
CompanyRegisterViewSet, StudentProfileViewSet
from core.views.vacancies import VacancyViewSet, BookmarkedVacancyByStudentViewSet, ApplicationViewSet
schema_view = get_swagger_view()
......@@ -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'profiles/students', StudentProfileViewSet)
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