# Copyright (C) 2014 Linaro Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""The RequestHandler for /boot URLs."""

from handlers.base import BaseHandler
from handlers.common import (
    BOOT_VALID_KEYS,
    get_query_spec,
)
from handlers.response import HandlerResponse
from models import BOOT_COLLECTION
from taskqueue.tasks import import_boot
from utils.db import (
    delete,
    find_one,
)


class BootHandler(BaseHandler):
    """Handle the /boot URLs."""

    def __init__(self, application, request, **kwargs):
        super(BootHandler, self).__init__(application, request, **kwargs)

    @property
    def collection(self):
        return self.db[BOOT_COLLECTION]

    @staticmethod
    def _valid_keys(method):
        return BOOT_VALID_KEYS.get(method, None)

    def _post(self, *args, **kwargs):
        response = HandlerResponse(202)
        response.reason = "Request accepted and being imported"
        response.result = None

        import_boot.apply_async([kwargs['json_obj']])

        return response

    def execute_delete(self, *args, **kwargs):
        response = None

        if kwargs and kwargs.get('id', None):
            doc_id = kwargs['id']
            if find_one(self.collection, doc_id):
                response = self._delete(doc_id)
                if response.status_code == 200:
                    response.reason = "Resource '%s' deleted" % doc_id
            else:
                response = HandlerResponse(404)
                response.reason = "Resource '%s' not found" % doc_id
        else:
            spec = get_query_spec(
                self.get_query_arguments, self._valid_keys("DELETE")
            )
            if spec:
                response = self._delete(spec)
                if response.status_code == 200:
                    response.reason = (
                        "Resources identified with '%s' deleted" % spec
                    )
            else:
                response = HandlerResponse(400)
                response.result = None
                response.reason = (
                    "No valid data provided to execute a DELETE"
                )

        return response

    def _delete(self, spec_or_id):
        response = HandlerResponse(200)
        response.result = None

        response.status_code = delete(self.collection, spec_or_id)
        response.reason = self._get_status_message(response.status_code)

        return response

@@ -815,16 +815,37 @@
 D_KEYS,%0A
+    NOT_VALID_TOKEN,%0A
     get_
@@ -1725,32 +1725,83 @@
 esponse = None%0A%0A
+        if self._validate_req_token(%22DELETE%22):%0A    
         if kwarg
@@ -1842,16 +1842,20 @@
         
+    
 doc_id =
@@ -1880,16 +1880,20 @@
         
+ 
+   
 if find_
@@ -1918,24 +1918,28 @@
 n, doc_id):%0A
+    
             
@@ -1982,32 +1982,36 @@
                 
+    
 if response.stat
@@ -2018,32 +2018,36 @@
 us_code == 200:%0A
+    
                 
@@ -2105,38 +2105,46 @@
 _id%0A            
+    
 else:%0A
+    
                 
@@ -2183,32 +2183,36 @@
                 
+    
 response.reason 
@@ -2248,38 +2248,46 @@
  doc_id%0A        
+    
 else:%0A
+    
             spec
@@ -2305,16 +2305,20 @@
 y_spec(%0A
+    
         
@@ -2382,34 +2382,42 @@
 E%22)%0A            
-)%0A
+    )%0A    
             if s
@@ -2421,16 +2421,20 @@
 f spec:%0A
+    
         
@@ -2471,16 +2471,20 @@
 e(spec)%0A
+    
         
@@ -2535,32 +2535,36 @@
                 
+    
 response.reason 
@@ -2559,32 +2559,36 @@
 onse.reason = (%0A
+    
                 
@@ -2655,34 +2655,41 @@
                 
-)%0A
+    )%0A   
             else
@@ -2676,38 +2676,43 @@
 %0A               
+ 
 else:%0A
+    
                 
@@ -2751,32 +2751,36 @@
                 
+   
+ 
 response.result 
@@ -2794,32 +2794,36 @@
                 
+    
 response.reason 
@@ -2846,16 +2846,20 @@
         
+   
+ 
 %22No vali
@@ -2911,17 +2911,125 @@
         
-)
+    )%0A        else:%0A            response = HandlerResponse(403)%0A            response.reason = NOT_VALID_TOKEN
 %0A%0A      

Refactor token validation decorator.