from __future__ import absolute_import, division, unicode_literals

from changes.api.base import APIView
from changes.constants import Status
from changes.config import db
from changes.jobs.sync_job_step import sync_job_step
from changes.models import JobStep


class JobStepDeallocateAPIView(APIView):

    def post(self, step_id):
        to_deallocate = JobStep.query.get(step_id)

        if to_deallocate is None:
            return '', 404

        if to_deallocate.status not in (Status.in_progress, Status.allocated):
            return {
                "error": "Only allocated and running job steps may be deallocated.",
                "actual_status": to_deallocate.status.name
            }, 400

        to_deallocate.status = Status.pending_allocation
        to_deallocate.date_started = None
        to_deallocate.date_finished = None
        db.session.add(to_deallocate)
        db.session.commit()

        sync_job_step.delay(
            step_id=to_deallocate.id.hex,
            task_id=to_deallocate.id.hex,
            parent_task_id=to_deallocate.job_id.hex,
        )

        return self.respond(to_deallocate)

from __future__ import absolute_import, division, unicode_literals

from changes.api.base import APIView
from changes.constants import Status
from changes.config import db
from changes.jobs.sync_job_step import sync_job_step
from changes.models import JobStep


class JobStepDeallocateAPIView(APIView):

    def post(self, step_id):
        to_deallocate = JobStep.query.get(step_id)

        if to_deallocate is None:
            return '', 404

        if to_deallocate.status != Status.allocated:
            return {
                "error": "Only {0} job steps may be deallocated.",
                "actual_status": to_deallocate.status.name
            }, 400

        to_deallocate.status = Status.pending_allocation
        to_deallocate.date_started = None
        to_deallocate.date_finished = None
        db.session.add(to_deallocate)
        db.session.commit()

        sync_job_step.delay(
            step_id=to_deallocate.id.hex,
            task_id=to_deallocate.id.hex,
            parent_task_id=to_deallocate.job_id.hex,
        )

        return self.respond(to_deallocate)

Revert "Allow running jobsteps to be deallocated"
Revert "Allow running jobsteps to be deallocated"

This reverts commit 9b720026722ce92a8c0e05aa041d6e861c5e4e82.

  from __future__ import absolute_import, division, unicode_literals
  
  from changes.api.base import APIView
  from changes.constants import Status
  from changes.config import db
  from changes.jobs.sync_job_step import sync_job_step
  from changes.models import JobStep
  
  
  class JobStepDeallocateAPIView(APIView):
  
      def post(self, step_id):
          to_deallocate = JobStep.query.get(step_id)
  
          if to_deallocate is None:
              return '', 404
  
-         if to_deallocate.status not in (Status.in_progress, Status.allocated):
+         if to_deallocate.status != Status.allocated:
              return {
-                 "error": "Only allocated and running job steps may be deallocated.",
+                 "error": "Only {0} job steps may be deallocated.",
                  "actual_status": to_deallocate.status.name
              }, 400
  
          to_deallocate.status = Status.pending_allocation
          to_deallocate.date_started = None
          to_deallocate.date_finished = None
          db.session.add(to_deallocate)
          db.session.commit()
  
          sync_job_step.delay(
              step_id=to_deallocate.id.hex,
              task_id=to_deallocate.id.hex,
              parent_task_id=to_deallocate.job_id.hex,
          )
  
          return self.respond(to_deallocate)
  
Revert "Allow running jobsteps to be deallocated"
## Code Before:
from __future__ import absolute_import, division, unicode_literals

from changes.api.base import APIView
from changes.constants import Status
from changes.config import db
from changes.jobs.sync_job_step import sync_job_step
from changes.models import JobStep


class JobStepDeallocateAPIView(APIView):

    def post(self, step_id):
        to_deallocate = JobStep.query.get(step_id)

        if to_deallocate is None:
            return '', 404

        if to_deallocate.status not in (Status.in_progress, Status.allocated):
            return {
                "error": "Only allocated and running job steps may be deallocated.",
                "actual_status": to_deallocate.status.name
            }, 400

        to_deallocate.status = Status.pending_allocation
        to_deallocate.date_started = None
        to_deallocate.date_finished = None
        db.session.add(to_deallocate)
        db.session.commit()

        sync_job_step.delay(
            step_id=to_deallocate.id.hex,
            task_id=to_deallocate.id.hex,
            parent_task_id=to_deallocate.job_id.hex,
        )

        return self.respond(to_deallocate)

## Instruction:
Revert "Allow running jobsteps to be deallocated"
## Code After:
from __future__ import absolute_import, division, unicode_literals

from changes.api.base import APIView
from changes.constants import Status
from changes.config import db
from changes.jobs.sync_job_step import sync_job_step
from changes.models import JobStep


class JobStepDeallocateAPIView(APIView):

    def post(self, step_id):
        to_deallocate = JobStep.query.get(step_id)

        if to_deallocate is None:
            return '', 404

        if to_deallocate.status != Status.allocated:
            return {
                "error": "Only {0} job steps may be deallocated.",
                "actual_status": to_deallocate.status.name
            }, 400

        to_deallocate.status = Status.pending_allocation
        to_deallocate.date_started = None
        to_deallocate.date_finished = None
        db.session.add(to_deallocate)
        db.session.commit()

        sync_job_step.delay(
            step_id=to_deallocate.id.hex,
            task_id=to_deallocate.id.hex,
            parent_task_id=to_deallocate.job_id.hex,
        )

        return self.respond(to_deallocate)
