def create(self, request, message_pk): """Create a response for a message""" if not self.user.is_postulant: content = { 'unauthorized': '401 - only postulant can send response' } return Response(content, status=status.HTTP_401_UNAUTHORIZED) message = UserMessage(id=message_pk, postulant=request.user) if not message: content = {'badrequest': '400 - message not found'} return Response(content, status=status.HTTP_400_BAD_REQUEST) serializer = UserMessageSerializer(data=request.data) if serializer.is_valid(): # XXX use request.data because when get from serializer.data raise # KeyError for some fields. # make the response message response = UserMessage() response.message = request.data['message'] response.employer = message.employer response.postulant = message.postulant if message.postulation: response.postulation = message.postulation elif message.cvrequest: response.cvrequest = message.cvrequest response.save() # make relation between message and response message.response = response message.save() # Send email to employer if the preference is set, this setting # depend if the message generated by spontaneous cv or by # postulation if (response.postulation and response.postulation.job.send_cvs_by_mail): response.employer.send_usermessage_email(response) elif response.employer.cv_spontany: response.employer.send_usermessage_email(response) return Response({'status': _('Mensaje enviado')}) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def create_by_postulation(self, request, postulation_pk): """Create a message from postulation""" if not request.user.is_employer: content = {'unauthorized': '401 - only employer can send message'} return Response(content, status=status.HTTP_401_UNAUTHORIZED) serializer = UserMessageSerializer(data=request.data) if serializer.is_valid(): postulation = JobPostulation(id=postulation_pk) user_message = UserMessage() user_message.employer = postulation.job.owner user_message.postulant = postulation.user user_message.message = request.data['message'] user_message.save() user_message.postulant.send_usermessage_email(user_message) return Response({'status': _('Mensaje enviado')}) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def create_by_cvrequest(self, request, cvrequest_pk): """Create a message from cvrequest""" if not request.user.is_employer: content = {'unauthorized': '401 - only employer can send message'} return Response(content, status=status.HTTP_401_UNAUTHORIZED) serializer = UserMessageSerializer(data=request.data) if serializer.is_valid(): cvrequest = CVRequest.objects.filter(id=cvrequest_pk, status=CVREQUEST_ACCEPTED) if not cvrequest: return Response(status=status.HTTP_400_BAD_REQUEST) cvrequest = cvrequest[0] user_message = UserMessage() user_message.employer = cvrequest.employer.user user_message.postulant = cvrequest.user user_message.message = request.data['message'] user_message.save() user_message.postulant.send_usermessage_email(user_message) return Response({'status': _('Mensaje enviado')}) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)