Fakultas Ilmu Komputer UI

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

Add company update profile

parent bf0eb3ae
......@@ -4,6 +4,7 @@ from rest_framework import serializers
from core.models.accounts import Supervisor, Company, Student
from core.models.vacancies import Application
class BasicUserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
......@@ -37,11 +38,11 @@ class StudentUpdateSerializer(serializers.ModelSerializer):
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
'resume': resume,
'email': instance.user.email,
'phone_number': instance.phone_number,
'photo': photo,
'show_transcript': instance.show_transcript
}
def update(self, instance, validated_data):
......@@ -59,6 +60,36 @@ class StudentUpdateSerializer(serializers.ModelSerializer):
fields = ['resume', 'email', 'phone_number', 'photo', 'show_transcript']
class CompanyUpdateSerializer(serializers.ModelSerializer):
email = serializers.EmailField()
def to_representation(self, instance):
logo = None
if instance.logo and hasattr(instance.logo, 'url'):
logo = instance.logo.url
return {
'address': instance.address,
'description': instance.description,
'email': instance.user.email,
'logo': logo,
'name': instance.name
}
def update(self, instance, validated_data):
instance.address = validated_data.get('address', instance.address)
instance.description = validated_data.get('description', instance.description)
instance.logo = validated_data.get('logo', instance.logo)
instance.user.email = validated_data.get('email', instance.user.email)
instance.user.first_name = validated_data.get('name', instance.user.first_name)
instance.save()
instance.user.save()
return instance
class Meta:
model = Company
fields = ['address', 'description', 'email', 'logo', 'name']
class CompanySerializer(serializers.ModelSerializer):
user = BasicUserSerializer()
name = serializers.ReadOnlyField()
......@@ -93,4 +124,3 @@ class RegisterSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ('url', 'username', 'email', 'is_staff', 'company')
......@@ -12,7 +12,7 @@ from core.lib.permissions import IsAdminOrStudent, IsAdminOrSelfOrReadOnly, IsAd
IsAdminOrSupervisorOrCompanyOrSelf
from core.models.accounts import Student, Company, Supervisor
from core.serializers.accounts import BasicUserSerializer, UserSerializer, StudentSerializer, CompanySerializer, \
SupervisorSerializer, RegisterSerializer, StudentUpdateSerializer
SupervisorSerializer, RegisterSerializer, StudentUpdateSerializer, CompanyUpdateSerializer
class UserViewSet(viewsets.ModelViewSet):
......@@ -73,6 +73,21 @@ class CompanyViewSet(viewsets.ModelViewSet):
permission_classes = [IsAdminOrSelfOrReadOnly, IsAdminOrCompany]
filter_fields = ('status',)
@detail_route(methods=['patch'], permission_classes=[IsAdminOrCompany],
serializer_class=CompanyUpdateSerializer, parser_classes=(MultiPartParser, FormParser,))
def profile(self, request, pk=None):
"""
Update student {student_id}'s profile information
---
"""
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)
class SupervisorViewSet(viewsets.ModelViewSet):
queryset = Supervisor.objects.all()
......
Markdown is supported
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