Ejemplo n.º 1
0
 def perform_update(self, serializer):
     super().perform_update(serializer)
     log_command(
         CommandType.MODIFY,
         serializer.instance,
         self.request.user,
         CommandResult.SUCCESS,
     )
Ejemplo n.º 2
0
 def perform_create(self, serializer):
     serializer.save(creator=self.request.user)
     log_command(
         CommandType.CREATE,
         serializer.instance,
         self.request.user,
         CommandResult.SUCCESS,
     )
Ejemplo n.º 3
0
    def stop(self, request, pk):
        try:
            server = Server.objects.get(id=pk)
        except ObjectDoesNotExist:
            raise NotFound()

        if server.server_status == ServerStatus.RUNNING.name:
            server.server_status = ServerStatus.STOPPED.name
            server.save()
            log_command(CommandType.STOP, server, self.request.user,
                        CommandResult.SUCCESS)
            return Response()

        log_command(CommandType.STOP, server, self.request.user,
                    CommandResult.ERROR)
        raise ValidationError(
            f"Cannot stop the server because it is in status: {server.server_status}"
        )
Ejemplo n.º 4
0
 def perform_destroy(self, instance):
     if instance.server_status != ServerStatus.DELETED.name:
         instance.server_status = ServerStatus.DELETED.name
         instance.save()
         log_command(
             CommandType.DELETE,
             instance,
             self.request.user,
             CommandResult.SUCCESS,
         )
     else:
         log_command(
             CommandType.DELETE,
             instance,
             self.request.user,
             CommandResult.ERROR,
         )
         raise ValidationError("Server already deleted")
Ejemplo n.º 5
0
    def clone(self, request, pk):
        try:
            server = Server.objects.get(id=pk)
        except ObjectDoesNotExist:
            raise NotFound()

        if server.server_status not in [
                ServerStatus.IN_PROGRESS.name,
                ServerStatus.DELETED.name,
        ]:
            server.id = None
            server.server_status = ServerStatus.STOPPED.name
            server.created = timezone.now()
            server.save()
            log_command(CommandType.CLONE, server, self.request.user,
                        CommandResult.SUCCESS)
            return Response(self.serializer_class(server).data)

        log_command(CommandType.CLONE, server, self.request.user,
                    CommandResult.ERROR)
        raise ValidationError(
            f"Cannot clone the server because it is in status: {server.server_status}"
        )