def update(self, request, *args, **kwargs): """Overwrite response when a object is created""" instance = self.get_object() serializer = self.get_serializer(instance, data=request.data) try: serializer.is_valid(raise_exception=True) self.perform_update(serializer) except ModelValidationError as ex: return ErrorRestResponse( message=ex.messages[0], status=status.HTTP_500_INTERNAL_SERVER_ERROR ) except RESTValidationError as ex: return ErrorRestResponse( message=_('There are problems with the data'), status=status.HTTP_400_BAD_REQUEST, data=ex.detail ) except DatabaseError as ex: return ErrorRestResponse(str(ex)) if getattr(instance, '_prefetched_objects_cache', None): # En caso contenta relaciones foreanas se actualizan en la base de datos instance = self.get_object() serializer = self.get_serializer(instance) else: instance = self.get_object() serializer = self.get_serializer(instance) return SuccessRestResponse( message=_('Object was updated successly'), data=serializer.data, )
def api_detail_tiposoporte_view(request, pk): try: tiposoporte = TipoSoporte.objects.get(id=pk, fecha_eliminacion__isnull=True) serializer = TipoSoporteSerializer(tiposoporte) return SuccessRestResponse(serializer.data) except TipoSoporte.DoesNotExist: return ErrorRestResponse(message='No existe el tipo de soporte', status=status.HTTP_400_BAD_REQUEST)
def api_delete_tiposoporte_view(request, pk): try: tiposoporte = TipoSoporte.objects.get(id=pk) tiposoporte.fecha_eliminacion = dt.datetime.now() tiposoporte.usuario_eliminacion_id = request.user.id tiposoporte.save() return SuccessRestResponse(message='Tipo de Soporte Eliminado', status=status.HTTP_202_ACCEPTED) except TipoSoporte.DoesNotExist: return ErrorRestResponse(message='No existe el Tipo de soporte', status=status.HTTP_404_NOT_FOUND)
def api_get_tiposoporte_view(request): try: tiposoporte = TipoSoporte.objects.filter( fecha_eliminacion__isnull=True).order_by('-fecha_creacion') serializer = TipoSoporteSerializer(tiposoporte, many=True) return SuccessRestResponse(message='', status=status.HTTP_202_ACCEPTED, data=serializer.data) except TipoSoporte.DoesNotExist: return ErrorRestResponse(message='No existe el tipo de soporte', status=status.HTTP_400_BAD_REQUEST)
def destroy(self, request, *args, **kwargs): """Overwrite response when a object is deleted""" instance = self.get_object() try: instance.delete() except DatabaseError as ex: return ErrorRestResponse(str(ex)) return SuccessRestResponse( message=_('Object was deleted successly'), status=status.HTTP_204_NO_CONTENT )
def api_create_tiposoporte_view(request): tiposoporte = TipoSoporte() request.data['usuario_creacion'] = request.user.id request.data['fecha_creacion'] = dt.datetime.now() print(request.data) serializer = TipoSoporteSerializer(tiposoporte, data=request.data) if serializer.is_valid(): serializer.save() return SuccessRestResponse( message='Tipo de Soporte registrado correctamente', status=status.HTTP_200_OK, data=serializer.data) return ErrorRestResponse(message='No se puede registrar', status=status.HTTP_400_BAD_REQUEST, data=serializer.errors)
def api_update_tiposoporte_view(request, pk): try: tiposoporte = TipoSoporte.objects.get(id=pk, fecha_eliminacion__isnull=True) request.data['fecha_modificacion'] = dt.datetime.now() request.data['usuario_modificacion'] = request.user.id serializer = TipoSoporteSerializer(tiposoporte, data=request.data) if serializer.is_valid(): serializer.save() return SuccessRestResponse( message='Tipo de soporte actualizado de manera correcta', status=status.HTTP_202_ACCEPTED, data=serializer.data) except TipoSoporte.DoesNotExist: return ErrorRestResponse(message='No existe el tipo de soporte', status=status.HTTP_400_BAD_REQUEST)
def create(self, request, *args, **kwargs): """Overrite response when a model is created""" serializer = self.get_serializer(data=request.data) try: serializer.is_valid(raise_exception=True) self.perform_create(serializer) except ModelValidationError as ex: return ErrorRestResponse( message=ex.message, status=status.HTTP_500_INTERNAL_SERVER_ERROR ) except RESTValidationError as ex: return ErrorRestResponse( message=_('There are problems with the data'), data=ex.detail, status=status.HTTP_400_BAD_REQUEST ) except DatabaseError as ex: return ErrorRestResponse(str(ex)) return SuccessRestResponse( message=_('Operation finish successly'), status=status.HTTP_201_CREATED, data=serializer.data )