<commit_before># -*- coding: utf-8 -*-

#    Copyright 2013 Mirantis, Inc.
#
#    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.

"""
Handlers dealing with releases
"""

from nailgun.api.v1.handlers.base import CollectionHandler
from nailgun.api.v1.handlers.base import content
from nailgun.api.v1.handlers.base import DeploymentTasksHandler
from nailgun.api.v1.handlers.base import SingleHandler
from nailgun.api.v1.validators.release import ReleaseNetworksValidator
from nailgun.api.v1.validators.release import ReleaseValidator
from nailgun.objects import Release
from nailgun.objects import ReleaseCollection


class ReleaseHandler(SingleHandler):
    """Release single handler
    """

    single = Release
    validator = ReleaseValidator


class ReleaseCollectionHandler(CollectionHandler):
    """Release collection handler
    """

    validator = ReleaseValidator
    collection = ReleaseCollection

    @content
    def GET(self):
        """:returns: Sorted releases' collection in JSON format
        :http: * 200 (OK)
        """
        q = sorted(self.collection.all(), reverse=True)
        return self.collection.to_json(q)


class ReleaseNetworksHandler(SingleHandler):
    """Release Handler for network metadata
    """

    single = Release
    validator = ReleaseNetworksValidator

    @content
    def GET(self, obj_id):
        """Read release networks metadata

        :returns: Release networks metadata
        :http: * 201 (object successfully created)
               * 400 (invalid object data specified)
               * 404 (release object not found)
        """
        obj = self.get_object_or_404(self.single, obj_id)
        return obj['networks_metadata']

    @content
    def PUT(self, obj_id):
        """Updates release networks metadata

        :returns: Release networks metadata<commit_msg>Disable ability to use certain bond modes in UI<commit_after># -*- coding: utf-8 -*-

#    Copyright 2013 Mirantis, Inc.
#
#    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.

"""
Handlers dealing with releases
"""

from nailgun.api.v1.handlers.base import CollectionHandler
from nailgun.api.v1.handlers.base import content
from nailgun.api.v1.handlers.base import DeploymentTasksHandler
from nailgun.api.v1.handlers.base import SingleHandler
from nailgun.api.v1.validators.release import ReleaseNetworksValidator
from nailgun.api.v1.validators.release import ReleaseValidator
from nailgun.objects import Release
from nailgun.objects import ReleaseCollection
from nailgun.settings import settings


class ReleaseHandler(SingleHandler):
    """Release single handler
    """

    single = Release
    validator = ReleaseValidator


class ReleaseCollectionHandler(CollectionHandler):
    """Release collection handler
    """

    validator = ReleaseValidator
    collection = ReleaseCollection

    @content
    def GET(self):
        """:returns: Sorted releases' collection in JSON format
        :http: * 200 (OK)
        """
        q = sorted(self.collection.all(), reverse=True)
        return self.collection.to_json(q)


class ReleaseNetworksHandler(SingleHandler):
    """Release Handler for network metadata
    """

    single = Release
    validator = ReleaseNetworksValidator

    @content
    def GET(self, obj_id):
        """Read release networks metadata

        :returns: Release networks metadata
        :http: * 201 (object successfully created)
               * 400 (invalid object data specified)
               * 404 (release object not found)
        """
        obj = self.get_object_or_404(self.single, obj_id)
        networks_metadata = dict(obj['networks_metadata'])
        if 'experimental' not in settings.VERSION['feature_groups']:
            networks_metadata['bonding']['properties']['linux'] = {
                'mode': [
                    {'values': ["balance-rr", "active-backup", "802.3ad"]}
                ],
                'xmit_hash_policy': [
                    {'values': ["layer2", "layer2+3", "layer3+4",
                                "encap2+3", "encap3+4"],
                     'for_modes': ["802.3ad"]}
                ],
                'lacp_rate': [
                    {'values': ["slow", "fast"],
                     'for_modes': ["802.3ad"]}
                ]
            }
        return obj['networks_metadata']

    @content
    def PUT(self, obj_id):
        """Updates release networks metadata

        :returns: Release networks metadata
{'fraction_non_alphanumeric': 0.0916879329201604, 'fraction_numerical': 0.014764855997083486, 'mean_word_length': 2.8263598326359833, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4658086', 'n_tokens_mistral': 1477, 'n_tokens_neox': 1396, 'n_words': 463}