from astral.api.handlers.base import BaseHandler
from astral.models.node import Node
from astral.api.client import NodesAPI
import sys


import logging
log = logging.getLogger(__name__)


class NodeHandler(BaseHandler):
    def delete(self, node_uuid=None):
        """Remove the requesting node from the list of known nodes,
        unregistering the from the network.
        """

        if node_uuid:
            node = Node.get_by(uuid=node_uuid)
        else:
            node = Node.me()
            temp = node
        node.delete()

        if node_uuid:
            node = Node.get_by(uuid=node_uuid)
        else:
            node = Node.me()
        node.delete()

        closest_supernode = Node.closest_supernode()
        if closest_supernode:
            log.info("Notifying closest supernode %s that %s was deleted",
                    closest_supernode, node)
            NodesAPI(closest_supernode.absolute_url()).unregister(node)

        sys.exit()

        if temp == Node.me():
            # TODO kind of a shortcut to shutting down, but should be a bit more
            # formal
            GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0) 
            """shut = Daemon()
            shut.stop()
            raise KeyboardInterrupt"""

        if node == Node.me():
            # TODO kind of a shortcut to shutting down, but should be a bit more
            # formal
            raise KeyboardInterrupt

from astral.api.handlers.base import BaseHandler
from astral.models.node import Node
from astral.api.client import NodesAPI

import logging
log = logging.getLogger(__name__)


class NodeHandler(BaseHandler):
    def delete(self, node_uuid=None):
        """Remove the requesting node from the list of known nodes,
        unregistering the from the network.
        """

        if not node_uuid:
            log.info("Shutting down because of request from %s",
                    self.request.remote_ip)
            self.stop()
            return

        node = Node.get_by(uuid=node_uuid)
        closest_supernode = Node.closest_supernode()
        if closest_supernode:
            log.info("Notifying closest supernode %s that %s was deleted",
                    closest_supernode, node)
            NodesAPI(closest_supernode.absolute_url()).unregister(node)
        node.delete()

Clean up Node delete handler.
Clean up Node delete handler.

  from astral.api.handlers.base import BaseHandler
  from astral.models.node import Node
  from astral.api.client import NodesAPI
- import sys
- 
  
  import logging
  log = logging.getLogger(__name__)
  
  
  class NodeHandler(BaseHandler):
      def delete(self, node_uuid=None):
          """Remove the requesting node from the list of known nodes,
          unregistering the from the network.
          """
  
-         if node_uuid:
+         if not node_uuid:
+             log.info("Shutting down because of request from %s",
+                     self.request.remote_ip)
+             self.stop()
+             return
-             node = Node.get_by(uuid=node_uuid)
-         else:
-             node = Node.me()
-             temp = node
-         node.delete()
  
-         if node_uuid:
-             node = Node.get_by(uuid=node_uuid)
+         node = Node.get_by(uuid=node_uuid)
-         else:
-             node = Node.me()
-         node.delete()
- 
          closest_supernode = Node.closest_supernode()
          if closest_supernode:
              log.info("Notifying closest supernode %s that %s was deleted",
                      closest_supernode, node)
              NodesAPI(closest_supernode.absolute_url()).unregister(node)
+         node.delete()
  
-         sys.exit()
- 
-         if temp == Node.me():
-             # TODO kind of a shortcut to shutting down, but should be a bit more
-             # formal
-             GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0) 
-             """shut = Daemon()
-             shut.stop()
-             raise KeyboardInterrupt"""
- 
-         if node == Node.me():
-             # TODO kind of a shortcut to shutting down, but should be a bit more
-             # formal
-             raise KeyboardInterrupt
- 
Clean up Node delete handler.
## Code Before:
from astral.api.handlers.base import BaseHandler
from astral.models.node import Node
from astral.api.client import NodesAPI
import sys


import logging
log = logging.getLogger(__name__)


class NodeHandler(BaseHandler):
    def delete(self, node_uuid=None):
        """Remove the requesting node from the list of known nodes,
        unregistering the from the network.
        """

        if node_uuid:
            node = Node.get_by(uuid=node_uuid)
        else:
            node = Node.me()
            temp = node
        node.delete()

        if node_uuid:
            node = Node.get_by(uuid=node_uuid)
        else:
            node = Node.me()
        node.delete()

        closest_supernode = Node.closest_supernode()
        if closest_supernode:
            log.info("Notifying closest supernode %s that %s was deleted",
                    closest_supernode, node)
            NodesAPI(closest_supernode.absolute_url()).unregister(node)

        sys.exit()

        if temp == Node.me():
            # TODO kind of a shortcut to shutting down, but should be a bit more
            # formal
            GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0) 
            """shut = Daemon()
            shut.stop()
            raise KeyboardInterrupt"""

        if node == Node.me():
            # TODO kind of a shortcut to shutting down, but should be a bit more
            # formal
            raise KeyboardInterrupt

## Instruction:
Clean up Node delete handler.
## Code After:
from astral.api.handlers.base import BaseHandler
from astral.models.node import Node
from astral.api.client import NodesAPI

import logging
log = logging.getLogger(__name__)


class NodeHandler(BaseHandler):
    def delete(self, node_uuid=None):
        """Remove the requesting node from the list of known nodes,
        unregistering the from the network.
        """

        if not node_uuid:
            log.info("Shutting down because of request from %s",
                    self.request.remote_ip)
            self.stop()
            return

        node = Node.get_by(uuid=node_uuid)
        closest_supernode = Node.closest_supernode()
        if closest_supernode:
            log.info("Notifying closest supernode %s that %s was deleted",
                    closest_supernode, node)
            NodesAPI(closest_supernode.absolute_url()).unregister(node)
        node.delete()
