<commit_before>import httplib
from pecan import (abort, response)
from pecan.rest import RestController

# TODO: Encapsulate mongoengine errors in our persistence layer. Exceptions
#       that bubble up to this layer should be core Python exceptions or
#       StackStorm defined exceptions.
from mongoengine import ValidationError

from wsme import types as wstypes
import wsmeext.pecan as wsme_pecan

from st2common import log as logging
from st2common.persistence.action import Action
from st2common.models.api.action import ActionAPI


LOG = logging.getLogger('st2actioncontroller')


class ActionsController(RestController):
    """
        Implements the RESTful web endpoint that handles
        the lifecycle of Actions in the system.
    """

    def get_by_id(self, id):
        """
            Get Action by id and abort http operation on errors.
        """
        try:
            action = Action.get_by_id(id)
        except (ValueError, ValidationError) as e:
            LOG.error('Database lookup for id="%s" resulted in exception: %s', id, e)
            abort(httplib.NOT_FOUND)

        return action
    

    # TODO: Investigate mako rendering
    @wsme_pecan.wsexpose(ActionAPI, wstypes.text)
    def get_one(self, id):
        """
            List action by id.

            Handle:
                GET /actions/1
        """

        LOG.info('GET /actions/ with id=%s', id)

        action_db = self.get_by_id(id)
        action_api = ActionAPI.from_model(action_db)

        LOG.debug('GET /actions/ with id=%s, client_result=%s', id, action_api)
        return action_api

    @wsme_pecan.wsexpose([ActionAPI])
    # TODO: support kwargs
    #def get_all(self, **kwargs):
    def get_all(self):
        """
            List all actions.

            Handles requests:
                GET /actions/
        """

        LOG.info('GET all /actions/')

        action_apis = [ActionAPI.from_model(action_db)
                                 for action_db in Action.get_all()]

        # TODO: unpack list in log message
        LOG.debug('GET all /actions/ client_result=%s', action_apis)
        return action_apis

    @wsme_pecan.wsexpose(ActionAPI, body=ActionAPI, status_code=httplib.CREATED)
    def post(self, data):
        """
            Create a new action.

            Handles requests:
                POST /actions/
        """

        LOG.info('POST /actions/ with action data=%s', data)

        action_api = ActionAPI.to_model(action)
        LOG.debug('/actions/ POST verified ActionAPI object=%s', action_api)
        # TODO: POST operations should only add to DB.
        #       If an existing object conflicts then raise error.

        action_db = Action.add_or_update(action_api)
        LOG.debug('/actions/ POST saved ActionDB object=%s', action_db)
        action_api = ActionAPI.from_model(action_db)<commit_msg>[STORM-136] Implement functional tests for Action * Fixing variable name bug.<commit_after>import httplib
from pecan import (abort, response)
from pecan.rest import RestController

# TODO: Encapsulate mongoengine errors in our persistence layer. Exceptions
#       that bubble up to this layer should be core Python exceptions or
#       StackStorm defined exceptions.
from mongoengine import ValidationError

from wsme import types as wstypes
import wsmeext.pecan as wsme_pecan

from st2common import log as logging
from st2common.persistence.action import Action
from st2common.models.api.action import ActionAPI


LOG = logging.getLogger('st2actioncontroller')


class ActionsController(RestController):
    """
        Implements the RESTful web endpoint that handles
        the lifecycle of Actions in the system.
    """

    def get_by_id(self, id):
        """
            Get Action by id and abort http operation on errors.
        """
        try:
            action = Action.get_by_id(id)
        except (ValueError, ValidationError) as e:
            LOG.error('Database lookup for id="%s" resulted in exception: %s', id, e)
            abort(httplib.NOT_FOUND)

        return action
    

    # TODO: Investigate mako rendering
    @wsme_pecan.wsexpose(ActionAPI, wstypes.text)
    def get_one(self, id):
        """
            List action by id.

            Handle:
                GET /actions/1
        """

        LOG.info('GET /actions/ with id=%s', id)

        action_db = self.get_by_id(id)
        action_api = ActionAPI.from_model(action_db)

        LOG.debug('GET /actions/ with id=%s, client_result=%s', id, action_api)
        return action_api

    @wsme_pecan.wsexpose([ActionAPI])
    # TODO: support kwargs
    #def get_all(self, **kwargs):
    def get_all(self):
        """
            List all actions.

            Handles requests:
                GET /actions/
        """

        LOG.info('GET all /actions/')

        action_apis = [ActionAPI.from_model(action_db)
                                 for action_db in Action.get_all()]

        # TODO: unpack list in log message
        LOG.debug('GET all /actions/ client_result=%s', action_apis)
        return action_apis

    @wsme_pecan.wsexpose(ActionAPI, body=ActionAPI, status_code=httplib.CREATED)
    def post(self, action):
        """
            Create a new action.

            Handles requests:
                POST /actions/
        """

        LOG.info('POST /actions/ with action data=%s', action)

        action_api = ActionAPI.to_model(action)
        LOG.debug('/actions/ POST verified ActionAPI object=%s', action_api)
        # TODO: POST operations should only add to DB.
        #       If an existing object conflicts then raise error.

        action_db = Action.add_or_update(action_api)
        LOG.debug('/actions/ POST saved ActionDB object=%s', action_db)
        action_api = ActionAPI.from_model(action_db)
{'fraction_non_alphanumeric': 0.08732345849121598, 'fraction_numerical': 0.0022390630382363073, 'mean_word_length': 2.316390633923472, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6438977', 'n_tokens_mistral': 1691, 'n_tokens_neox': 1629, 'n_words': 490}