# No shebang line, this module is meant to be imported
#
# Copyright 2014 Oliver Palmer
# Copyright 2014 Ambient Entertainment GmbH & Co. KG
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from json import dumps

try:
    from httplib import (
        BAD_REQUEST, NO_CONTENT, INTERNAL_SERVER_ERROR, NOT_FOUND, ACCEPTED)
except ImportError:  # pragma: no cover
    from http.client import (
        BAD_REQUEST, NO_CONTENT, INTERNAL_SERVER_ERROR, NOT_FOUND, ACCEPTED)

from twisted.web.server import NOT_DONE_YET

from pyfarm.agent.config import config
from pyfarm.agent.http.api.base import APIResource
from pyfarm.agent.utility import request_from_master
from pyfarm.agent.logger import getLogger

logger = getLogger("agent.http.api.tasks")


class Tasks(APIResource):
    def get(self, **kwargs):
        request = kwargs["request"]

        if request_from_master(request):
            config.master_contacted()

        try:
            current_assignments = config["current_assignments"].itervalues
        except AttributeError:  # pragma: no cover
            current_assignments = config["current_assignments"].values

        tasks = []
        for assignment in current_assignments():
            tasks += assignment["tasks"]

        return dumps(tasks)

    def delete(self, **kwargs):
        request = kwargs["request"]

        # Postpath must be exactly one element, and that needs to be an integer
        if len(request.postpath) != 1:
            request.setResponseCode(BAD_REQUEST)
            request.write({"error": "Did not specify a task id"})

        try:
            task_id = int(request.postpath[0])
        except ValueError:
            request.setResponseCode(BAD_REQUEST)
            request.write({"error": "Task id was not an integer"})

        try:
            current_assignments = config["current_assignments"].itervalues
        except AttributeError:  # pragma: no cover
            current_assignments = config["current_assignments"].values

        assignment = None
        for item in current_assignments():
            for task in item["tasks"]:
                if task["id"] == task_id:
                    assignment = item

        if not assignment:
            logger.info("Cannot cancel task %s: not found", task_id)
            request.setResponseCode(NO_CONTENT)
            request.finish()
            return NOT_DONE_YET

        if ("jobtype" in assignment
            and "id" in assignment["jobtype"]):
            jobtype = config["jobtypes"][assignment["jobtype"]["id"]]
            logger.info("Stopping assignment %s", assignment["id"])
            jobtype.stop()
        else:
            logger.error("Tried stopping assigment %s, but found no jobtype "
                         "instance", assignment.id)
            request.setResponseCode(INTERNAL_SERVER_ERROR)
            request.write({"error": "Assignment found, but no jobtype instance "
                                    "exists."})
            request.finish()
            return NOT_DONE_YET

        request.setResponseCode(ACCEPTED)
        request.finish()

        return NOT_DONE_YET

@@ -2060,16 +2060,48 @@
 sk id%22%7D)
+%0A            return NOT_DONE_YET
 %0A%0A      
@@ -2296,16 +2296,48 @@
 teger%22%7D)
+%0A            return NOT_DONE_YET
 %0A%0A      

Return early on bad request