<commit_before># Copyright 2014 Algolia
#
# 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.

"""Receives documents from the oplog worker threads and indexes them
    into the backend.

    This file is a document manager for the Algolia search engine.
    """
import logging
import re
import json
from datetime import datetime
import bson.json_util as bsjson
import bson
import copy

from algoliasearch import algoliasearch
from mongo_connector import errors
from mongo_connector.doc_managers import DocManagerBase
from threading import Timer, RLock

decoder = json.JSONDecoder()

def clean_path(dirty):
    # handle python dictionary subscription style, e.g. `"['key1']['key2']"`:
    if re.match(r'^\[', dirty):
        return re.split(r'\'\]\[\'', re.sub(r'^\[\'|\'\]$', '', dirty))
    # handle mongo op dot-notation style, e.g. `"key1.key2"`:
    return dirty.split('.')

def get_at(doc, path, create_anyway=False):
    node = doc
    last = len(path) - 1
    if last == 0:
        return doc.get(path[0])
    for index, edge in enumerate(path):
        if edge in node:
            node = node[edge]
        elif index == last or not create_anyway:
            # the key doesn't exist, and this is the end of the path:
            return None
        else:
            # create anyway will create any missing nodes:
            node = node[edge] = {}
    return node

def set_at(doc, path, value):
    node = get_at(doc, path[:-1], create_anyway=True)
    node[path[-1]] = value

def put_at(doc, path, value, append=False):
    if append:
        get_at(doc, path).append(value)
    else:
        set_at(doc, path, value)

def unix_time(dt=datetime.now()):
    epoch = datetime.utcfromtimestamp(0)
    delta = dt - epoch
    return delta.total_seconds()

def unix_time_millis(dt=datetime.now()):
    return int(round(unix_time(dt) * 1000.0))

def serialize(value):
    if isinstance(value, bson.objectid.ObjectId):
        return str(value)
    else:
        return value

def filter_value(value, filter):
    if filter == "":
        return True
    try:
        return eval(re.sub(r'\$_', 'value', filter))
    except Exception as e:
        logging.warn("""
            Error raised from expression: {filter} with value {value}
            """.format(**locals()))
        logging.warn(e)
        # return false to prevent potentially sensitive data from being synced:
        return False

class DocManager(DocManagerBase):
    """The DocManager class creates a connection to the Algolia engine and
        adds/removes documents, and in the case of rollback, searches for them.

        Algolia's native 'objectID' field is used to store the unique_key.
        """

    BATCH_SIZE = 1000
    AUTO_COMMIT_DELAY_S = 10

    def __init__(self, url, unique_key='_id', **kwargs):
        """Establish a connection to Algolia using target url
            'APPLICATION_ID:API_KEY:INDEX_NAME'
        """
        application_id, api_key, index = url.split(':')
        self.algolia = algoliasearch.Client(application_id, api_key)
        self.index = self.algolia.initIndex(index)
        self.unique_key = unique_key
        self.last_object_id = None
        self.batch = []
        self.mutex = RLock()
        self.auto_commit = True
        self.run_auto_commit()
        try:
            json = open("algolia_fields_" + index + ".json", 'r')
            self.attributes_filter = decoder.decode(json.read())
            logging.info("Algolia Connector: Start with filter.")
        except IOError:  # No "fields" filter file
            self.attributes_filter = None
            logging.info("Algolia Connector: Start without filter.")
        try:
            json = open("algolia_remap_" + index + ".json", 'r')
            self.attributes_remap = decoder.decode(json.read())
            logging.info("Algolia Connector: Start with remapper.")
        except IOError:  # No "remap" filter file
            self.attributes_remap = None
            logging.info("Algolia Connector: Start without remapper.")
        try:
            f = open("algolia_postproc_" + index + ".py", 'r')
            self.postproc = f.read()
            logging.info("Algolia Connector: Start with post processing.")
        except IOError:  # No "postproc" filter file
            self.postproc = None
            logging.info("Algolia Connector: Start without post processing.")

    def stop(self):
        """ Stops the instance
        """
        self.auto_commit = False

    def apply_filter(self, doc, filter):
        """
        Recursively copy the values of user-defined fields from the source
        document to a new target document by testing each value against a
        corresponding user-defined expression. If the expression returns true
        for a given value, copy that value to the corresponding field in the
        target document. If the special `*all*` filter is used for a given
        document and an adjacent field's expression returns false for a given
        value, remove the document containing that field from its parent in the
        tree of the target document.
        """
        if not filter:
            # alway return a new object:
            return (copy.deepcopy(doc), True)
        filtered_doc = {}
        all_or_nothing = '*all*' in filter
        for raw_key, expr in filter.iteritems():
            if raw_key == '*all*':
                continue
            key = clean_path(raw_key)
            values = get_at(doc, key)
            state = True
            if type(values) == list:
                append = True
                set_at(filtered_doc, key, [])
            else:
                append = False
                values = [values]
            for value in values:
                if isinstance(value, dict):
                    sub, sub_state = self.apply_filter(value, filter[raw_key])
                    if sub_state:
                        put_at(filtered_doc, key, serialize(sub), append)
                    elif all_or_nothing:
                        node = get_at(filtered_doc, key[:-1])
                        del node[key[-1]]
                        return filtered_doc, False
                elif filter_value(value, filter[raw_key]):
                    put_at(filtered_doc, key, serialize(value), append)
                elif all_or_nothing:
                    return filtered_doc, False
                else:
                    state = False
        return (filtered_doc, state)

    def apply_remap(self, doc):
        """
        Copy the values of user-defined fields from the source document to
        user-defined fields in a new target document, then return the target
        document.
        """
        if not self.attributes_remap:
            return doc
        remapped_doc = {}
        for raw_source_key, raw_target_key in self.attributes_remap.items():
            # clean the keys, making a list from possible notations:
            source_key = clean_path(raw_source_key)
            target_key = clean_path(raw_target_key)

            # get the value from the source doc:
            value = get_at(doc, source_key)

            # special case for "_ts" field:
            if source_key == ['_ts'] and target_key == ["*ts*"]:
                value = value if value else str(unix_time_millis())

            set_at(remapped_doc, target_key, value)
        return remapped_doc

    def update(self, doc, update_spec):
        self.upsert(self.apply_update(doc, update_spec))

    def upsert(self, doc):
        """ Update or insert a document into Algolia
        """
        with self.mutex:
            last_object_id = str(doc[self.unique_key])
            filtered_doc, state = self.apply_filter(self.apply_remap(doc),
                                                    self.attributes_filter)
            filtered_doc['objectID'] = last_object_id

            if not state:  # delete in case of update
                self.batch.append({'action': 'deleteObject',
                                   'body': {'objectID': last_object_id}})
                return

            if self.postproc is not None:
                exec(re.sub(r"_\$", "filtered_doc", self.postproc))

            self.batch.append({'action': 'addObject', 'body': filtered_doc})
            if len(self.batch) >= DocManager.BATCH_SIZE:
                self.commit()

    def remove(self, doc):
        """ Removes documents from Algolia
        """
        with self.mutex:
            self.batch.append(
                {'action': 'deleteObject',
                 'body': {'objectID': str(doc[self.unique_key])}})
            if len(self.batch) >= DocManager.BATCH_SIZE:
                self.commit()

    def search(self, start_ts, end_ts):
        """ Called to query Algolia for documents in a time range.
        """
        try:
            params = {
                'numericFilters': '_ts>=%d,_ts<=%d' % (start_ts, end_ts),
                'exhaustive': True,
                'hitsPerPage': 100000000
            }
            return self.index.search('', params)['hits']
        except algoliasearch.AlgoliaException as e:
            raise errors.ConnectionFailed(
                "Could not connect to Algolia Search: %s" % e)

    def commit(self):
        """ Send the current batch of updates
        """
        try:
            request = {}
            with self.mutex:
                if len(self.batch) == 0:
                    return
                self.index.batch({'requests': self.batch})
                self.index.setSettings(
                    {'userData': {'lastObjectID': self.last_object_id}})
                self.batch = []
        except algoliasearch.AlgoliaException as e:
            raise errors.ConnectionFailed(
                "Could not connect to Algolia Search: %s" % e)

    def run_auto_commit(self):
        """ Periodically commits to Algolia.
        """
        self.commit()
        if self.auto_commit:
            Timer(DocManager.AUTO_COMMIT_DELAY_S, self.run_auto_commit).start()

    def get_last_doc(self):
        """ Returns the last document stored in Algolia.
        """
        last_object_id = self.get_last_object_id()
        if last_object_id is None:
            return None
        try:
            return self.index.getObject(last_object_id)
        except algoliasearch.AlgoliaException as e:
            raise errors.ConnectionFailed(
                "Could not connect to Algolia Search: %s" % e)

    def get_last_object_id(self):
        try:
            return (self.index.getSettings().get('userData', {})).get(
                'lastObjectID', None)
        except algoliasearch.AlgoliaException as e:
            raise errors.ConnectionFailed(
                "Could not connect to Algolia Search: %s" % e)
<commit_msg>code style: move `apply_remap` _above_ `apply_filter`<commit_after># Copyright 2014 Algolia
#
# 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.

"""Receives documents from the oplog worker threads and indexes them
    into the backend.

    This file is a document manager for the Algolia search engine.
    """
import logging
import re
import json
from datetime import datetime
import bson.json_util as bsjson
import bson
import copy

from algoliasearch import algoliasearch
from mongo_connector import errors
from mongo_connector.doc_managers import DocManagerBase
from threading import Timer, RLock

decoder = json.JSONDecoder()

def clean_path(dirty):
    # handle python dictionary subscription style, e.g. `"['key1']['key2']"`:
    if re.match(r'^\[', dirty):
        return re.split(r'\'\]\[\'', re.sub(r'^\[\'|\'\]$', '', dirty))
    # handle mongo op dot-notation style, e.g. `"key1.key2"`:
    return dirty.split('.')

def get_at(doc, path, create_anyway=False):
    node = doc
    last = len(path) - 1
    if last == 0:
        return doc.get(path[0])
    for index, edge in enumerate(path):
        if edge in node:
            node = node[edge]
        elif index == last or not create_anyway:
            # the key doesn't exist, and this is the end of the path:
            return None
        else:
            # create anyway will create any missing nodes:
            node = node[edge] = {}
    return node

def set_at(doc, path, value):
    node = get_at(doc, path[:-1], create_anyway=True)
    node[path[-1]] = value

def put_at(doc, path, value, append=False):
    if append:
        get_at(doc, path).append(value)
    else:
        set_at(doc, path, value)

def unix_time(dt=datetime.now()):
    epoch = datetime.utcfromtimestamp(0)
    delta = dt - epoch
    return delta.total_seconds()

def unix_time_millis(dt=datetime.now()):
    return int(round(unix_time(dt) * 1000.0))

def serialize(value):
    if isinstance(value, bson.objectid.ObjectId):
        return str(value)
    else:
        return value

def filter_value(value, filter):
    if filter == "":
        return True
    try:
        return eval(re.sub(r'\$_', 'value', filter))
    except Exception as e:
        logging.warn("""
            Error raised from expression: {filter} with value {value}
            """.format(**locals()))
        logging.warn(e)
        # return false to prevent potentially sensitive data from being synced:
        return False

class DocManager(DocManagerBase):
    """The DocManager class creates a connection to the Algolia engine and
        adds/removes documents, and in the case of rollback, searches for them.

        Algolia's native 'objectID' field is used to store the unique_key.
        """

    BATCH_SIZE = 1000
    AUTO_COMMIT_DELAY_S = 10

    def __init__(self, url, unique_key='_id', **kwargs):
        """Establish a connection to Algolia using target url
            'APPLICATION_ID:API_KEY:INDEX_NAME'
        """
        application_id, api_key, index = url.split(':')
        self.algolia = algoliasearch.Client(application_id, api_key)
        self.index = self.algolia.initIndex(index)
        self.unique_key = unique_key
        self.last_object_id = None
        self.batch = []
        self.mutex = RLock()
        self.auto_commit = True
        self.run_auto_commit()
        try:
            json = open("algolia_fields_" + index + ".json", 'r')
            self.attributes_filter = decoder.decode(json.read())
            logging.info("Algolia Connector: Start with filter.")
        except IOError:  # No "fields" filter file
            self.attributes_filter = None
            logging.info("Algolia Connector: Start without filter.")
        try:
            json = open("algolia_remap_" + index + ".json", 'r')
            self.attributes_remap = decoder.decode(json.read())
            logging.info("Algolia Connector: Start with remapper.")
        except IOError:  # No "remap" filter file
            self.attributes_remap = None
            logging.info("Algolia Connector: Start without remapper.")
        try:
            f = open("algolia_postproc_" + index + ".py", 'r')
            self.postproc = f.read()
            logging.info("Algolia Connector: Start with post processing.")
        except IOError:  # No "postproc" filter file
            self.postproc = None
            logging.info("Algolia Connector: Start without post processing.")

    def stop(self):
        """ Stops the instance
        """
        self.auto_commit = False

    def apply_remap(self, doc):
        """
        Copy the values of user-defined fields from the source document to
        user-defined fields in a new target document, then return the target
        document.
        """
        if not self.attributes_remap:
            return doc
        remapped_doc = {}
        for raw_source_key, raw_target_key in self.attributes_remap.items():
            # clean the keys, making a list from possible notations:
            source_key = clean_path(raw_source_key)
            target_key = clean_path(raw_target_key)

            # get the value from the source doc:
            value = get_at(doc, source_key)

            # special case for "_ts" field:
            if source_key == ['_ts'] and target_key == ["*ts*"]:
                value = value if value else str(unix_time_millis())

            set_at(remapped_doc, target_key, value)
        return remapped_doc

    def apply_filter(self, doc, filter):
        """
        Recursively copy the values of user-defined fields from the source
        document to a new target document by testing each value against a
        corresponding user-defined expression. If the expression returns true
        for a given value, copy that value to the corresponding field in the
        target document. If the special `*all*` filter is used for a given
        document and an adjacent field's expression returns false for a given
        value, remove the document containing that field from its parent in the
        tree of the target document.
        """
        if not filter:
            # alway return a new object:
            return (copy.deepcopy(doc), True)
        filtered_doc = {}
        all_or_nothing = '*all*' in filter
        for raw_key, expr in filter.iteritems():
            if raw_key == '*all*':
                continue
            key = clean_path(raw_key)
            values = get_at(doc, key)
            state = True
            if type(values) == list:
                append = True
                set_at(filtered_doc, key, [])
            else:
                append = False
                values = [values]
            for value in values:
                if isinstance(value, dict):
                    sub, sub_state = self.apply_filter(value, filter[raw_key])
                    if sub_state:
                        put_at(filtered_doc, key, serialize(sub), append)
                    elif all_or_nothing:
                        node = get_at(filtered_doc, key[:-1])
                        del node[key[-1]]
                        return filtered_doc, False
                elif filter_value(value, filter[raw_key]):
                    put_at(filtered_doc, key, serialize(value), append)
                elif all_or_nothing:
                    return filtered_doc, False
                else:
                    state = False
        return (filtered_doc, state)

    def update(self, doc, update_spec):
        self.upsert(self.apply_update(doc, update_spec))

    def upsert(self, doc):
        """ Update or insert a document into Algolia
        """
        with self.mutex:
            last_object_id = str(doc[self.unique_key])
            filtered_doc, state = self.apply_filter(self.apply_remap(doc),
                                                    self.attributes_filter)
            filtered_doc['objectID'] = last_object_id

            if not state:  # delete in case of update
                self.batch.append({'action': 'deleteObject',
                                   'body': {'objectID': last_object_id}})
                return

            if self.postproc is not None:
                exec(re.sub(r"_\$", "filtered_doc", self.postproc))

            self.batch.append({'action': 'addObject', 'body': filtered_doc})
            if len(self.batch) >= DocManager.BATCH_SIZE:
                self.commit()

    def remove(self, doc):
        """ Removes documents from Algolia
        """
        with self.mutex:
            self.batch.append(
                {'action': 'deleteObject',
                 'body': {'objectID': str(doc[self.unique_key])}})
            if len(self.batch) >= DocManager.BATCH_SIZE:
                self.commit()

    def search(self, start_ts, end_ts):
        """ Called to query Algolia for documents in a time range.
        """
        try:
            params = {
                'numericFilters': '_ts>=%d,_ts<=%d' % (start_ts, end_ts),
                'exhaustive': True,
                'hitsPerPage': 100000000
            }
            return self.index.search('', params)['hits']
        except algoliasearch.AlgoliaException as e:
            raise errors.ConnectionFailed(
                "Could not connect to Algolia Search: %s" % e)

    def commit(self):
        """ Send the current batch of updates
        """
        try:
            request = {}
            with self.mutex:
                if len(self.batch) == 0:
                    return
                self.index.batch({'requests': self.batch})
                self.index.setSettings(
                    {'userData': {'lastObjectID': self.last_object_id}})
                self.batch = []
        except algoliasearch.AlgoliaException as e:
            raise errors.ConnectionFailed(
                "Could not connect to Algolia Search: %s" % e)

    def run_auto_commit(self):
        """ Periodically commits to Algolia.
        """
        self.commit()
        if self.auto_commit:
            Timer(DocManager.AUTO_COMMIT_DELAY_S, self.run_auto_commit).start()

    def get_last_doc(self):
        """ Returns the last document stored in Algolia.
        """
        last_object_id = self.get_last_object_id()
        if last_object_id is None:
            return None
        try:
            return self.index.getObject(last_object_id)
        except algoliasearch.AlgoliaException as e:
            raise errors.ConnectionFailed(
                "Could not connect to Algolia Search: %s" % e)

    def get_last_object_id(self):
        try:
            return (self.index.getSettings().get('userData', {})).get(
                'lastObjectID', None)
        except algoliasearch.AlgoliaException as e:
            raise errors.ConnectionFailed(
                "Could not connect to Algolia Search: %s" % e)
<|endoftext|>


<commit_before>#
# Copyright (C) 2016 Red Hat, 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.
#

import functools
import testtools

from neutron.tests.unit.extensions import test_securitygroup
from neutron.tests.unit.plugins.ml2 import test_plugin

from networking_odl.common import constants as odl_const
from networking_odl.tests.functional import base

class _DriverTest(base.OdlTestsBase):

    def test_network_create(self):
        with self.network() as network:
            self.assert_resource_created(odl_const.ODL_NETWORK, network)

    def test_network_update(self):
        with self.network() as network:
            self.resource_update_test(odl_const.ODL_NETWORK, network)

    def test_network_delete(self):
        with self.network() as network:
            self.resource_delete_test(odl_const.ODL_NETWORK, network)

    def test_subnet_create(self):
        with self.network() as network:
            with self.subnet(network=network) as subnet:
                self.assert_resource_created(odl_const.ODL_SUBNET, subnet)

    def test_subnet_update(self):
        with self.network() as network:
            with self.subnet(network=network) as subnet:
                self.resource_update_test(odl_const.ODL_SUBNET, subnet)

    def test_subnet_delete(self):
        with self.network() as network:
            with self.subnet(network=network) as subnet:
                self.resource_delete_test(odl_const.ODL_SUBNET, subnet)

    def test_port_create(self):
        with self.network() as network:
            with self.subnet(network=network) as subnet:
                with self.port(subnet=subnet) as port:
                    self.assert_resource_created(odl_const.ODL_PORT, port)

    def test_port_update(self):
        with self.network() as network:
            with self.subnet(network=network) as subnet:
                with self.port(subnet=subnet) as port:
                    self.resource_update_test(odl_const.ODL_PORT, port)

    def test_port_delete(self):
        with self.network() as network:
            with self.subnet(network=network) as subnet:
                with self.port(subnet=subnet) as port:
                    self.resource_delete_test(odl_const.ODL_PORT, port)

class _DriverSecGroupsTests(base.OdlTestsBase):

    # Override default behavior so that extension manager is used, otherwise
    # we can't test security groups.
    def setup_parent(self):
        """Perform parent setup with the common plugin configuration class."""
        ext_mgr = (
            test_securitygroup.SecurityGroupTestExtensionManager())
        # Ensure that the parent setup can be called without arguments
        # by the common configuration setUp.
        parent_setup = functools.partial(
            super(test_plugin.Ml2PluginV2TestCase, self).setUp,
            plugin=test_plugin.PLUGIN_NAME,
            ext_mgr=ext_mgr,
        )
        self.useFixture(test_plugin.Ml2ConfFixture(parent_setup))

    def test_security_group_create(self):
        with self.security_group() as sg:
            self.assert_resource_created(odl_const.ODL_SG, sg)

    # TODO(yamahata):
    # https://review.openstack.org/#/c/475649/
    # once the above patch is merged, remove this skip.
    @testtools.skip("bug/154690")
    def test_security_group_update(self):
        with self.security_group() as sg:
            self.resource_update_test(odl_const.ODL_SG, sg)

    def test_security_group_delete(self):
        with self.security_group() as sg:
            self.resource_delete_test(odl_const.ODL_SG, sg)

    def test_security_group_rule_create(self):
        with self.security_group() as sg:
            sg_id = sg[odl_const.ODL_SG]['id']
            with self.security_group_rule(security_group_id=sg_id) as sg_rule:
                self.assert_resource_created(odl_const.ODL_SG_RULE, sg_rule)

    def test_security_group_rule_delete(self):
        with self.security_group() as sg:
            sg_id = sg[odl_const.ODL_SG]['id']
            with self.security_group_rule(security_group_id=sg_id) as sg_rule:
                self.resource_delete_test(odl_const.ODL_SG_RULE, sg_rule)

class TestV1Driver(_DriverTest, test_plugin.Ml2PluginV2TestCase):
    _mechanism_drivers = ['opendaylight']

class TestV1DriverSecGroups(_DriverSecGroupsTests,
                            test_securitygroup.SecurityGroupsTestCase,
                            test_plugin.Ml2PluginV2TestCase):
    _mechanism_drivers = ['opendaylight']

class TestV2Driver(base.V2DriverAdjustment, _DriverTest,
                   test_plugin.Ml2PluginV2TestCase):
    _mechanism_drivers = ['opendaylight_v2']

class TestV2DriverSecGroups(base.V2DriverAdjustment, _DriverSecGroupsTests,
                            test_securitygroup.SecurityGroupsTestCase,
                            test_plugin.Ml2PluginV2TestCase):
    _mechanism_drivers = ['opendaylight_v2']
<commit_msg>enable test_security_group_update<commit_after>#
# Copyright (C) 2016 Red Hat, 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.
#

import functools

from neutron.tests.unit.extensions import test_securitygroup
from neutron.tests.unit.plugins.ml2 import test_plugin

from networking_odl.common import constants as odl_const
from networking_odl.tests.functional import base

class _DriverTest(base.OdlTestsBase):

    def test_network_create(self):
        with self.network() as network:
            self.assert_resource_created(odl_const.ODL_NETWORK, network)

    def test_network_update(self):
        with self.network() as network:
            self.resource_update_test(odl_const.ODL_NETWORK, network)

    def test_network_delete(self):
        with self.network() as network:
            self.resource_delete_test(odl_const.ODL_NETWORK, network)

    def test_subnet_create(self):
        with self.network() as network:
            with self.subnet(network=network) as subnet:
                self.assert_resource_created(odl_const.ODL_SUBNET, subnet)

    def test_subnet_update(self):
        with self.network() as network:
            with self.subnet(network=network) as subnet:
                self.resource_update_test(odl_const.ODL_SUBNET, subnet)

    def test_subnet_delete(self):
        with self.network() as network:
            with self.subnet(network=network) as subnet:
                self.resource_delete_test(odl_const.ODL_SUBNET, subnet)

    def test_port_create(self):
        with self.network() as network:
            with self.subnet(network=network) as subnet:
                with self.port(subnet=subnet) as port:
                    self.assert_resource_created(odl_const.ODL_PORT, port)

    def test_port_update(self):
        with self.network() as network:
            with self.subnet(network=network) as subnet:
                with self.port(subnet=subnet) as port:
                    self.resource_update_test(odl_const.ODL_PORT, port)

    def test_port_delete(self):
        with self.network() as network:
            with self.subnet(network=network) as subnet:
                with self.port(subnet=subnet) as port:
                    self.resource_delete_test(odl_const.ODL_PORT, port)

class _DriverSecGroupsTests(base.OdlTestsBase):

    # Override default behavior so that extension manager is used, otherwise
    # we can't test security groups.
    def setup_parent(self):
        """Perform parent setup with the common plugin configuration class."""
        ext_mgr = (
            test_securitygroup.SecurityGroupTestExtensionManager())
        # Ensure that the parent setup can be called without arguments
        # by the common configuration setUp.
        parent_setup = functools.partial(
            super(test_plugin.Ml2PluginV2TestCase, self).setUp,
            plugin=test_plugin.PLUGIN_NAME,
            ext_mgr=ext_mgr,
        )
        self.useFixture(test_plugin.Ml2ConfFixture(parent_setup))

    def test_security_group_create(self):
        with self.security_group() as sg:
            self.assert_resource_created(odl_const.ODL_SG, sg)

    def test_security_group_update(self):
        with self.security_group() as sg:
            self.resource_update_test(odl_const.ODL_SG, sg)

    def test_security_group_delete(self):
        with self.security_group() as sg:
            self.resource_delete_test(odl_const.ODL_SG, sg)

    def test_security_group_rule_create(self):
        with self.security_group() as sg:
            sg_id = sg[odl_const.ODL_SG]['id']
            with self.security_group_rule(security_group_id=sg_id) as sg_rule:
                self.assert_resource_created(odl_const.ODL_SG_RULE, sg_rule)

    def test_security_group_rule_delete(self):
        with self.security_group() as sg:
            sg_id = sg[odl_const.ODL_SG]['id']
            with self.security_group_rule(security_group_id=sg_id) as sg_rule:
                self.resource_delete_test(odl_const.ODL_SG_RULE, sg_rule)

class TestV1Driver(_DriverTest, test_plugin.Ml2PluginV2TestCase):
    _mechanism_drivers = ['opendaylight']

class TestV1DriverSecGroups(_DriverSecGroupsTests,
                            test_securitygroup.SecurityGroupsTestCase,
                            test_plugin.Ml2PluginV2TestCase):
    _mechanism_drivers = ['opendaylight']

class TestV2Driver(base.V2DriverAdjustment, _DriverTest,
                   test_plugin.Ml2PluginV2TestCase):
    _mechanism_drivers = ['opendaylight_v2']

class TestV2DriverSecGroups(base.V2DriverAdjustment, _DriverSecGroupsTests,
                            test_securitygroup.SecurityGroupsTestCase,
                            test_plugin.Ml2PluginV2TestCase):
    _mechanism_drivers = ['opendaylight_v2']
<|endoftext|>


<commit_before><commit_msg>Remove "plugins" directory<commit_after><|endoftext|>


<commit_before>""" Testing resample function
"""

import numpy as np

from ....core.api import Image, vox2mni
from ..resample import resample
from ..affine import Affine

from numpy.testing import (assert_array_almost_equal,
                           assert_array_equal)

from nose.tools import assert_true, assert_equal, assert_raises

def test_resample():
    # Check basic cases of resampling
    arr = np.zeros((2, 3, 4))
    img = Image(arr, vox2mni(np.eye(4)))
    T = Affine()
    img2 = resample(img, T, interp_order=0)
    assert_array_almost_equal(img2.get_data(), img.get_data())
    img2 = resample(img, T, img)
    assert_array_almost_equal(img2.get_data(), img.get_data())
    in_arr = np.zeros((3, 4, 5))
    in_arr[1:, 1:, 1:] = arr
    mov = Image(in_arr, vox2mni(np.eye(4)))
    exp_arr = np.zeros((3, 4, 5))
    exp_arr[:-1, :-1, :-1] = arr
    ref2mov = Affine([1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0])
    img4 = resample(mov, ref2mov)
    assert_array_almost_equal(img4.get_data(), exp_arr)
<commit_msg>rewrote tests for the resampling function<commit_after>""" Testing resample function
"""

import numpy as np

from ....core.image.image_spaces import (as_xyz_image,
                                         xyz_affine)
from ....core.api import Image, vox2mni
from ..resample import resample
from ..affine import Affine

from numpy.testing import assert_array_almost_equal

def test_resample():
    # Check basic cases of resampling
    arr = np.random.rand(10, 11, 12)
    img = Image(arr, vox2mni(np.eye(4)))
    T = Affine()
    img2 = resample(img, T, interp_order=0)
    assert_array_almost_equal(img2.get_data(), img.get_data())
    img2 = resample(img, T, img)
    assert_array_almost_equal(img2.get_data(), img.get_data())
    img_aff = as_xyz_image(img)
    img2 = resample(img, T, reference=(img_aff.shape, xyz_affine(img_aff)))
    assert_array_almost_equal(img2.get_data(), img.get_data())
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""
Created on Wed Jul 15 13:39:36 2020

@author: mario
"""

from github import Github
import os

file = open("../../credits.md")
for line in file:
    print(line)

g = Github( os.getenv("GITHUB_TOKEN"))
# Github Enterprise with custom hostname

# Then play with your Github objects:
repo = g.get_repo(os.getenv("GITHUB_REPO"))

contents = repo.get_contents("")
while contents:
    file_content = contents.pop(0)
    if file_content.type == "dir":
        contents.extend(repo.get_contents(file_content.path))
    else:
        if file_content[-3:] == ".md":
            print(file_content)
<commit_msg>Update fuzz.py<commit_after># -*- coding: utf-8 -*-
"""
Created on Wed Jul 15 13:39:36 2020

@author: mario
"""

from github import Github
import os

file = open("../../credits.md")
for line in file:
    print(line)

g = Github( os.getenv("GITHUB_TOKEN"))
# Github Enterprise with custom hostname

# Then play with your Github objects:
repo = g.get_repo(os.getenv("GITHUB_REPO"))

contents = repo.get_contents("")
while contents:
    file_content = contents.pop(0)
    if file_content.type == "dir":
        contents.extend(repo.get_contents(file_content.path))
    else:
        print(file_content)
        if file_content[-3:] == ".md":
            print("selected: "+ file_content)

print("finished")<|endoftext|>


<commit_before>from django.conf import settings
from django.contrib.admin.views.main import ALL_VAR, EMPTY_CHANGELIST_VALUE
from django.contrib.admin.views.main import ORDER_VAR, ORDER_TYPE_VAR, PAGE_VAR, SEARCH_VAR
from django.core.exceptions import ObjectDoesNotExist
from django.db import models
from django.utils import dateformat
from django.utils.html import escape
from django.utils.text import capfirst
from django.utils.translation import get_date_formats, get_partial_date_formats
from django.template import Library
import datetime

register = Library()

DOT = '.'

def paginator_number(cl,i):
    if i == DOT:
        return '... '
    elif i == cl.page_num:
        return '%d ' % (i+1)
    else:
        return '%d ' % (cl.get_query_string({PAGE_VAR: i}), (i == cl.paginator.pages-1 and ' class="end"' or ''), i+1)
paginator_number = register.simple_tag(paginator_number)

def pagination(cl):
    paginator, page_num = cl.paginator, cl.page_num

    pagination_required = (not cl.show_all or not cl.can_show_all) and cl.multi_page
    if not pagination_required:
        page_range = []
    else:
        ON_EACH_SIDE = 3
        ON_ENDS = 2

        # If there are 10 or fewer pages, display links to every page.
        # Otherwise, do some fancy
        if paginator.pages <= 10:
            page_range = range(paginator.pages)
        else:
            # Insert "smart" pagination links, so that there are always ON_ENDS
            # links at either end of the list of pages, and there are always
            # ON_EACH_SIDE links at either end of the "current page" link.
            page_range = []
            if page_num > (ON_EACH_SIDE + ON_ENDS):
                page_range.extend(range(0, ON_EACH_SIDE - 1))
                page_range.append(DOT)
                page_range.extend(range(page_num - ON_EACH_SIDE, page_num + 1))
            else:
                page_range.extend(range(0, page_num + 1))
            if page_num < (paginator.pages - ON_EACH_SIDE - ON_ENDS - 1):
                page_range.extend(range(page_num + 1, page_num + ON_EACH_SIDE + 1))
                page_range.append(DOT)
                page_range.extend(range(paginator.pages - ON_ENDS, paginator.pages))
            else:
                page_range.extend(range(page_num + 1, paginator.pages))

    need_show_all_link = cl.can_show_all and not cl.show_all and cl.multi_page
    return {
        'cl': cl,
        'pagination_required': pagination_required,
        'show_all_url': need_show_all_link and cl.get_query_string({ALL_VAR: ''}),
        'page_range': page_range,
        'ALL_VAR': ALL_VAR,
        '1': 1,
    }
pagination = register.inclusion_tag('admin/pagination.html')(pagination)

def result_headers(cl):
    lookup_opts = cl.lookup_opts

    for i, field_name in enumerate(lookup_opts.admin.list_display):
        try:
            f = lookup_opts.get_field(field_name)
        except models.FieldDoesNotExist:
            # For non-field list_display values, check for the function
            # attribute "short_description". If that doesn't exist, fall
            # back to the method name. And __str__ is a special-case.
            if field_name == '__str__':
                header = lookup_opts.verbose_name
            else:
                attr = getattr(cl.model, field_name) # Let AttributeErrors propagate.
                try:
                    header = attr.short_description
                except AttributeError:
                    header = field_name.replace('_', ' ')

            # It is a non-field, but perhaps one that is sortable
            if not getattr(getattr(cl.model, field_name), "admin_order_field", None):
                yield {"text": header}
                continue

            # So this _is_ a sortable non-field.  Go to the yield
            # after the else clause.
        else:
            if isinstance(f.rel, models.ManyToOneRel) and f.null:
                yield {"text": f.verbose_name}
                continue
            else:
                header = f.verbose_name

        th_classes = []
        new_order_type = 'asc'
        if field_name == cl.order_field:
            th_classes.append('sorted %sending' % cl.order_type.lower())
            new_order_type = {'asc': 'desc', 'desc': 'asc'}[cl.order_type.lower()]

        yield {"text": header,
               "sortable": True,
               "url": cl.get_query_string({ORDER_VAR: i, ORDER_TYPE_VAR: new_order_type}),
               "class_attrib": (th_classes and ' class="%s"' % ' '.join(th_classes) or '')}

def _boolean_icon(field_val):
    BOOLEAN_MAPPING = {True: 'yes', False: 'no', None: 'unknown'}
    return '' % (settings.ADMIN_MEDIA_PREFIX, BOOLEAN_MAPPING[field_val], field_val)

def items_for_result(cl, result):
    first = True
    pk = cl.lookup_opts.pk.attname
    for field_name in cl.lookup_opts.admin.list_display:
        row_class = ''
        try:
            f = cl.lookup_opts.get_field(field_name)
        except models.FieldDoesNotExist:
            # For non-field list_display values, the value is either a method
            # or a property.
            try:
                attr = getattr(result, field_name)
                allow_tags = getattr(attr, 'allow_tags', False)
                boolean = getattr(attr, 'boolean', False)
                if callable(attr):
                    attr = attr()
                if boolean:
                    allow_tags = True
                    result_repr = _boolean_icon(attr)
                else:
                    result_repr = str(attr)
            except (AttributeError, ObjectDoesNotExist):
                result_repr = EMPTY_CHANGELIST_VALUE
            else:
                # Strip HTML tags in the resulting text, except if the
                # function has an "allow_tags" attribute set to True.
                if not allow_tags:
                    result_repr = escape(result_repr)
        else:
            field_val = getattr(result, f.attname)

            if isinstance(f.rel, models.ManyToOneRel):
                if field_val is not None:
                    result_repr = escape(getattr(result, f.name))
                else:
                    result_repr = EMPTY_CHANGELIST_VALUE
            # Dates and times are special: They're formatted in a certain way.
            elif isinstance(f, models.DateField) or isinstance(f, models.TimeField):
                if field_val:
                    (date_format, datetime_format, time_format) = get_date_formats()
                    if isinstance(f, models.DateTimeField):
                        result_repr = capfirst(dateformat.format(field_val, datetime_format))
                    elif isinstance(f, models.TimeField):
                        result_repr = capfirst(dateformat.time_format(field_val, time_format))
                    else:
                        result_repr = capfirst(dateformat.format(field_val, date_format))
                else:
                    result_repr = EMPTY_CHANGELIST_VALUE
                row_class = ' class="nowrap"'
            # Booleans are special: We use images.
            elif isinstance(f, models.BooleanField) or isinstance(f, models.NullBooleanField):
                result_repr = _boolean_icon(field_val)
            # DecimalFields are special: Zero-pad the decimals.
            elif isinstance(f, models.DecimalField):
                if field_val is not None:
                    result_repr = ('%%.%sf' % f.decimal_places) % field_val
                else:
                    result_repr = EMPTY_CHANGELIST_VALUE
            # Fields with choices are special: Use the representation
            # of the choice.
            elif f.choices:
                result_repr = dict(f.choices).get(field_val, EMPTY_CHANGELIST_VALUE)
            else:
                result_repr = escape(str(field_val))
        if result_repr == '':
            result_repr = ' '
        # If list_display_links not defined, add the link tag to the first field
        if (first and not cl.lookup_opts.admin.list_display_links) or field_name in cl.lookup_opts.admin.list_display_links:
            table_tag = {True:'th', False:'td'}[first]
            first = False
            url = cl.url_for_result(result)
            result_id = str(getattr(result, pk)) # str() is needed in case of 23L (long ints)
            yield ('<%s%s>%s</%s>' % \
                (table_tag, row_class, url, (cl.is_popup and ' onclick="opener.dismissRelatedLookupPopup(window, %r); return false;"' % result_id or ''), result_repr, table_tag))
        else:
            yield ('%s	' % (row_class, result_repr))

def results(cl):
    for res in cl.result_list:
        yield list(items_for_result(cl,res))

def result_list(cl):
    return {'cl': cl,
            'result_headers': list(result_headers(cl)),
            'results': list(results(cl))}
result_list = register.inclusion_tag("admin/change_list_results.html")(result_list)

def date_hierarchy(cl):
    if cl.lookup_opts.admin.date_hierarchy:
        field_name = cl.lookup_opts.admin.date_hierarchy
        year_field = '%s__year' % field_name
        month_field = '%s__month' % field_name
        day_field = '%s__day' % field_name
        field_generic = '%s__' % field_name
        year_lookup = cl.params.get(year_field)
        month_lookup = cl.params.get(month_field)
        day_lookup = cl.params.get(day_field)
        year_month_format, month_day_format = get_partial_date_formats()

        link = lambda d: cl.get_query_string(d, [field_generic])

        if year_lookup and month_lookup and day_lookup:
            day = datetime.date(int(year_lookup), int(month_lookup), int(day_lookup))
            return {
                'show': True,
                'back': {
                    'link': link({year_field: year_lookup, month_field: month_lookup}),
                    'title': dateformat.format(day, year_month_format)
                },
                'choices': [{'title': dateformat.format(day, month_day_format)}]
            }
        elif year_lookup and month_lookup:
            days = cl.query_set.filter(**{year_field: year_lookup, month_field: month_lookup}).dates(field_name, 'day')
            return {
                'show': True,
                'back': {
                    'link': link({year_field: year_lookup}),
                    'title': year_lookup
                },
                'choices': [{
                    'link': link({year_field: year_lookup, month_field: month_lookup, day_field: day.day}),
                    'title': dateformat.format(day, month_day_format)
                } for day in days]
            }
        elif year_lookup:
            months = cl.query_set.filter(**{year_field: year_lookup}).dates(field_name, 'month')
            return {
                'show' : True,
                'back': {
                    'link' : link({}),
                    'title': _('All dates')
                },
                'choices': [{
                    'link': link({year_field: year_lookup, month_field: month.month}),
                    'title': dateformat.format(month, year_month_format)
                } for month in months]
            }
        else:
            years = cl.query_set.dates(field_name, 'year')
            return {
                'show': True,
                'choices': [{
                    'link': link({year_field: year.year}),
                    'title': year.year
                } for year in years]
            }
date_hierarchy = register.inclusion_tag('admin/date_hierarchy.html')(date_hierarchy)

def search_form(cl):
    return {
        'cl': cl,
        'show_result_count': cl.result_count != cl.full_result_count and not cl.opts.one_to_one_field,
        'search_var': SEARCH_VAR
    }
search_form = register.inclusion_tag('admin/search_form.html')(search_form)

def filter(cl, spec):
    return {'title': spec.title(), 'choices' : list(spec.choices(cl))}
filter = register.inclusion_tag('admin/filter.html')(filter)

def filters(cl):
    return {'cl': cl}
filters = register.inclusion_tag('admin/filters.html')(filters)
<commit_msg>Fixed #3397 (again): admin_order_field is now displayed correctly in the admin views. Thanks, kent37@tds.net<commit_after>from django.conf import settings
from django.contrib.admin.views.main import ALL_VAR, EMPTY_CHANGELIST_VALUE
from django.contrib.admin.views.main import ORDER_VAR, ORDER_TYPE_VAR, PAGE_VAR, SEARCH_VAR
from django.core.exceptions import ObjectDoesNotExist
from django.db import models
from django.utils import dateformat
from django.utils.html import escape
from django.utils.text import capfirst
from django.utils.translation import get_date_formats, get_partial_date_formats
from django.template import Library
import datetime

register = Library()

DOT = '.'

def paginator_number(cl,i):
    if i == DOT:
        return '... '
    elif i == cl.page_num:
        return '%d ' % (i+1)
    else:
        return '%d ' % (cl.get_query_string({PAGE_VAR: i}), (i == cl.paginator.pages-1 and ' class="end"' or ''), i+1)
paginator_number = register.simple_tag(paginator_number)

def pagination(cl):
    paginator, page_num = cl.paginator, cl.page_num

    pagination_required = (not cl.show_all or not cl.can_show_all) and cl.multi_page
    if not pagination_required:
        page_range = []
    else:
        ON_EACH_SIDE = 3
        ON_ENDS = 2

        # If there are 10 or fewer pages, display links to every page.
        # Otherwise, do some fancy
        if paginator.pages <= 10:
            page_range = range(paginator.pages)
        else:
            # Insert "smart" pagination links, so that there are always ON_ENDS
            # links at either end of the list of pages, and there are always
            # ON_EACH_SIDE links at either end of the "current page" link.
            page_range = []
            if page_num > (ON_EACH_SIDE + ON_ENDS):
                page_range.extend(range(0, ON_EACH_SIDE - 1))
                page_range.append(DOT)
                page_range.extend(range(page_num - ON_EACH_SIDE, page_num + 1))
            else:
                page_range.extend(range(0, page_num + 1))
            if page_num < (paginator.pages - ON_EACH_SIDE - ON_ENDS - 1):
                page_range.extend(range(page_num + 1, page_num + ON_EACH_SIDE + 1))
                page_range.append(DOT)
                page_range.extend(range(paginator.pages - ON_ENDS, paginator.pages))
            else:
                page_range.extend(range(page_num + 1, paginator.pages))

    need_show_all_link = cl.can_show_all and not cl.show_all and cl.multi_page
    return {
        'cl': cl,
        'pagination_required': pagination_required,
        'show_all_url': need_show_all_link and cl.get_query_string({ALL_VAR: ''}),
        'page_range': page_range,
        'ALL_VAR': ALL_VAR,
        '1': 1,
    }
pagination = register.inclusion_tag('admin/pagination.html')(pagination)

def result_headers(cl):
    lookup_opts = cl.lookup_opts

    for i, field_name in enumerate(lookup_opts.admin.list_display):
        try:
            f = lookup_opts.get_field(field_name)
            admin_order_field = None
        except models.FieldDoesNotExist:
            # For non-field list_display values, check for the function
            # attribute "short_description". If that doesn't exist, fall
            # back to the method name. And __str__ is a special-case.
            if field_name == '__str__':
                header = lookup_opts.verbose_name
            else:
                attr = getattr(cl.model, field_name) # Let AttributeErrors propagate.
                try:
                    header = attr.short_description
                except AttributeError:
                    header = field_name.replace('_', ' ')

            # It is a non-field, but perhaps one that is sortable
            admin_order_field = getattr(getattr(cl.model, field_name), "admin_order_field", None)
            if not admin_order_field:
                yield {"text": header}
                continue

            # So this _is_ a sortable non-field.  Go to the yield
            # after the else clause.
        else:
            if isinstance(f.rel, models.ManyToOneRel) and f.null:
                yield {"text": f.verbose_name}
                continue
            else:
                header = f.verbose_name

        th_classes = []
        new_order_type = 'asc'
        if field_name == cl.order_field or admin_order_field == cl.order_field:
            th_classes.append('sorted %sending' % cl.order_type.lower())
            new_order_type = {'asc': 'desc', 'desc': 'asc'}[cl.order_type.lower()]

        yield {"text": header,
               "sortable": True,
               "url": cl.get_query_string({ORDER_VAR: i, ORDER_TYPE_VAR: new_order_type}),
               "class_attrib": (th_classes and ' class="%s"' % ' '.join(th_classes) or '')}

def _boolean_icon(field_val):
    BOOLEAN_MAPPING = {True: 'yes', False: 'no', None: 'unknown'}
    return '' % (settings.ADMIN_MEDIA_PREFIX, BOOLEAN_MAPPING[field_val], field_val)

def items_for_result(cl, result):
    first = True
    pk = cl.lookup_opts.pk.attname
    for field_name in cl.lookup_opts.admin.list_display:
        row_class = ''
        try:
            f = cl.lookup_opts.get_field(field_name)
        except models.FieldDoesNotExist:
            # For non-field list_display values, the value is either a method
            # or a property.
            try:
                attr = getattr(result, field_name)
                allow_tags = getattr(attr, 'allow_tags', False)
                boolean = getattr(attr, 'boolean', False)
                if callable(attr):
                    attr = attr()
                if boolean:
                    allow_tags = True
                    result_repr = _boolean_icon(attr)
                else:
                    result_repr = str(attr)
            except (AttributeError, ObjectDoesNotExist):
                result_repr = EMPTY_CHANGELIST_VALUE
            else:
                # Strip HTML tags in the resulting text, except if the
                # function has an "allow_tags" attribute set to True.
                if not allow_tags:
                    result_repr = escape(result_repr)
        else:
            field_val = getattr(result, f.attname)

            if isinstance(f.rel, models.ManyToOneRel):
                if field_val is not None:
                    result_repr = escape(getattr(result, f.name))
                else:
                    result_repr = EMPTY_CHANGELIST_VALUE
            # Dates and times are special: They're formatted in a certain way.
            elif isinstance(f, models.DateField) or isinstance(f, models.TimeField):
                if field_val:
                    (date_format, datetime_format, time_format) = get_date_formats()
                    if isinstance(f, models.DateTimeField):
                        result_repr = capfirst(dateformat.format(field_val, datetime_format))
                    elif isinstance(f, models.TimeField):
                        result_repr = capfirst(dateformat.time_format(field_val, time_format))
                    else:
                        result_repr = capfirst(dateformat.format(field_val, date_format))
                else:
                    result_repr = EMPTY_CHANGELIST_VALUE
                row_class = ' class="nowrap"'
            # Booleans are special: We use images.
            elif isinstance(f, models.BooleanField) or isinstance(f, models.NullBooleanField):
                result_repr = _boolean_icon(field_val)
            # DecimalFields are special: Zero-pad the decimals.
            elif isinstance(f, models.DecimalField):
                if field_val is not None:
                    result_repr = ('%%.%sf' % f.decimal_places) % field_val
                else:
                    result_repr = EMPTY_CHANGELIST_VALUE
            # Fields with choices are special: Use the representation
            # of the choice.
            elif f.choices:
                result_repr = dict(f.choices).get(field_val, EMPTY_CHANGELIST_VALUE)
            else:
                result_repr = escape(str(field_val))
        if result_repr == '':
            result_repr = ' '
        # If list_display_links not defined, add the link tag to the first field
        if (first and not cl.lookup_opts.admin.list_display_links) or field_name in cl.lookup_opts.admin.list_display_links:
            table_tag = {True:'th', False:'td'}[first]
            first = False
            url = cl.url_for_result(result)
            result_id = str(getattr(result, pk)) # str() is needed in case of 23L (long ints)
            yield ('<%s%s>%s</%s>' % \
                (table_tag, row_class, url, (cl.is_popup and ' onclick="opener.dismissRelatedLookupPopup(window, %r); return false;"' % result_id or ''), result_repr, table_tag))
        else:
            yield ('%s	' % (row_class, result_repr))

def results(cl):
    for res in cl.result_list:
        yield list(items_for_result(cl,res))

def result_list(cl):
    return {'cl': cl,
            'result_headers': list(result_headers(cl)),
            'results': list(results(cl))}
result_list = register.inclusion_tag("admin/change_list_results.html")(result_list)

def date_hierarchy(cl):
    if cl.lookup_opts.admin.date_hierarchy:
        field_name = cl.lookup_opts.admin.date_hierarchy
        year_field = '%s__year' % field_name
        month_field = '%s__month' % field_name
        day_field = '%s__day' % field_name
        field_generic = '%s__' % field_name
        year_lookup = cl.params.get(year_field)
        month_lookup = cl.params.get(month_field)
        day_lookup = cl.params.get(day_field)
        year_month_format, month_day_format = get_partial_date_formats()

        link = lambda d: cl.get_query_string(d, [field_generic])

        if year_lookup and month_lookup and day_lookup:
            day = datetime.date(int(year_lookup), int(month_lookup), int(day_lookup))
            return {
                'show': True,
                'back': {
                    'link': link({year_field: year_lookup, month_field: month_lookup}),
                    'title': dateformat.format(day, year_month_format)
                },
                'choices': [{'title': dateformat.format(day, month_day_format)}]
            }
        elif year_lookup and month_lookup:
            days = cl.query_set.filter(**{year_field: year_lookup, month_field: month_lookup}).dates(field_name, 'day')
            return {
                'show': True,
                'back': {
                    'link': link({year_field: year_lookup}),
                    'title': year_lookup
                },
                'choices': [{
                    'link': link({year_field: year_lookup, month_field: month_lookup, day_field: day.day}),
                    'title': dateformat.format(day, month_day_format)
                } for day in days]
            }
        elif year_lookup:
            months = cl.query_set.filter(**{year_field: year_lookup}).dates(field_name, 'month')
            return {
                'show' : True,
                'back': {
                    'link' : link({}),
                    'title': _('All dates')
                },
                'choices': [{
                    'link': link({year_field: year_lookup, month_field: month.month}),
                    'title': dateformat.format(month, year_month_format)
                } for month in months]
            }
        else:
            years = cl.query_set.dates(field_name, 'year')
            return {
                'show': True,
                'choices': [{
                    'link': link({year_field: year.year}),
                    'title': year.year
                } for year in years]
            }
date_hierarchy = register.inclusion_tag('admin/date_hierarchy.html')(date_hierarchy)

def search_form(cl):
    return {
        'cl': cl,
        'show_result_count': cl.result_count != cl.full_result_count and not cl.opts.one_to_one_field,
        'search_var': SEARCH_VAR
    }
search_form = register.inclusion_tag('admin/search_form.html')(search_form)

def filter(cl, spec):
    return {'title': spec.title(), 'choices' : list(spec.choices(cl))}
filter = register.inclusion_tag('admin/filter.html')(filter)

def filters(cl):
    return {'cl': cl}
filters = register.inclusion_tag('admin/filters.html')(filters)
<|endoftext|>


<commit_before>from flask.ext.wtf import Form
from wtforms import IntegerField
from wtforms.validators import DataRequired, ValidationError, Length, Optional, Regexp
from dmutils.forms import StripWhitespaceStringField, EmailField, EmailValidator

def word_length(limit=None, message=None):
    message = message or 'Must not be more than %d words'
    message = message % limit

    def _length(form, field):
        if not field.data or not limit:
            return field

        if len(field.data.split()) > limit:
            raise ValidationError(message)

    return _length

class EditSupplierForm(Form):
    description = StripWhitespaceStringField('Supplier summary', validators=[
        word_length(50, 'Your summary must not be more than %d words')
    ])

class EditContactInformationForm(Form):
    id = IntegerField()
    contactName = StripWhitespaceStringField('Contact name', validators=[
        DataRequired(message="You must provide a contact name"),
    ])
    email = EmailField('Contact email', validators=[
        DataRequired(message="You must provide an email address"),
    ])
    phoneNumber = StripWhitespaceStringField('Contact phone number')
    address1 = StripWhitespaceStringField('Building and street')
    city = StripWhitespaceStringField('Town or city')
    postcode = StripWhitespaceStringField('Postcode')

class DunsNumberForm(Form):
    duns_number = StripWhitespaceStringField('DUNS Number', validators=[
        DataRequired(message="You must enter a DUNS number with 9 digits."),
        Regexp(r'^\d{9}$', message="You must enter a DUNS number with 9 digits."),
    ])

class CompaniesHouseNumberForm(Form):
    companies_house_number = StripWhitespaceStringField('Companies house number', validators=[
        Optional(),
        Regexp(r'^([0-9]{2}|[A-Za-z]{2})[0-9]{6}$',
               message="Companies House numbers must have either 8 digits or 2 letters followed by 6 digits."
               )
    ])

class CompanyNameForm(Form):
    company_name = StripWhitespaceStringField('Company name', validators=[
        DataRequired(message="You must provide a company name."),
        Length(max=255, message="You must provide a company name under 256 characters.")
    ])

class CompanyContactDetailsForm(Form):
    contact_name = StripWhitespaceStringField('Contact name', validators=[
        DataRequired(message="You must provide a contact name."),
        Length(max=255, message="You must provide a contact name under 256 characters.")
    ])
    email_address = StripWhitespaceStringField('Contact email address', validators=[
        DataRequired(message="You must provide an email address."),
        EmailValidator(message="You must provide a valid email address."),
    ])
    phone_number = StripWhitespaceStringField('Contact phone number', validators=[
        DataRequired(message="You must provide a phone number."),
        Length(max=20, message="You must provide a phone number under 20 characters.")
    ])

class EmailAddressForm(Form):
    email_address = StripWhitespaceStringField('Email address', validators=[
        DataRequired(message="You must provide an email address."),
        EmailValidator(message="You must provide a valid email address."),
    ])
<commit_msg>EditContactInformationForm: change email field label to match wording in original CompanyContactDetailsForm<commit_after>from flask.ext.wtf import Form
from wtforms import IntegerField
from wtforms.validators import DataRequired, ValidationError, Length, Optional, Regexp
from dmutils.forms import StripWhitespaceStringField, EmailField, EmailValidator

def word_length(limit=None, message=None):
    message = message or 'Must not be more than %d words'
    message = message % limit

    def _length(form, field):
        if not field.data or not limit:
            return field

        if len(field.data.split()) > limit:
            raise ValidationError(message)

    return _length

class EditSupplierForm(Form):
    description = StripWhitespaceStringField('Supplier summary', validators=[
        word_length(50, 'Your summary must not be more than %d words')
    ])

class EditContactInformationForm(Form):
    id = IntegerField()
    contactName = StripWhitespaceStringField('Contact name', validators=[
        DataRequired(message="You must provide a contact name"),
    ])
    email = EmailField('Contact email address', validators=[
        DataRequired(message="You must provide an email address"),
    ])
    phoneNumber = StripWhitespaceStringField('Contact phone number')
    address1 = StripWhitespaceStringField('Building and street')
    city = StripWhitespaceStringField('Town or city')
    postcode = StripWhitespaceStringField('Postcode')

class DunsNumberForm(Form):
    duns_number = StripWhitespaceStringField('DUNS Number', validators=[
        DataRequired(message="You must enter a DUNS number with 9 digits."),
        Regexp(r'^\d{9}$', message="You must enter a DUNS number with 9 digits."),
    ])

class CompaniesHouseNumberForm(Form):
    companies_house_number = StripWhitespaceStringField('Companies house number', validators=[
        Optional(),
        Regexp(r'^([0-9]{2}|[A-Za-z]{2})[0-9]{6}$',
               message="Companies House numbers must have either 8 digits or 2 letters followed by 6 digits."
               )
    ])

class CompanyNameForm(Form):
    company_name = StripWhitespaceStringField('Company name', validators=[
        DataRequired(message="You must provide a company name."),
        Length(max=255, message="You must provide a company name under 256 characters.")
    ])

class CompanyContactDetailsForm(Form):
    contact_name = StripWhitespaceStringField('Contact name', validators=[
        DataRequired(message="You must provide a contact name."),
        Length(max=255, message="You must provide a contact name under 256 characters.")
    ])
    email_address = StripWhitespaceStringField('Contact email address', validators=[
        DataRequired(message="You must provide an email address."),
        EmailValidator(message="You must provide a valid email address."),
    ])
    phone_number = StripWhitespaceStringField('Contact phone number', validators=[
        DataRequired(message="You must provide a phone number."),
        Length(max=20, message="You must provide a phone number under 20 characters.")
    ])

class EmailAddressForm(Form):
    email_address = StripWhitespaceStringField('Email address', validators=[
        DataRequired(message="You must provide an email address."),
        EmailValidator(message="You must provide a valid email address."),
    ])
<|endoftext|>


<commit_before>#!/usr/bin/env python
from flask.ext.assets import ManageAssets
from flask.ext.script import Manager
from flask_migrate import MigrateCommand

from jazzband.app import create_app
from jazzband.assets import assets
from jazzband.commands import SyncMembers, SyncProjects

app = create_app('jazzband.config')

manager = Manager(app)
manager.add_command('assets', ManageAssets(assets))
manager.add_command('db', MigrateCommand)
manager.add_command('sync_members', SyncMembers())
manager.add_command('sync_projects', SyncProjects())

if __name__ == "__main__":
    manager.run()
<commit_msg>Proxy fix for GitHub webhooks.<commit_after>#!/usr/bin/env python
import os
from flask.ext.assets import ManageAssets
from flask.ext.script import Manager
from flask_migrate import MigrateCommand
from werkzeug.contrib.fixers import ProxyFix

from jazzband.app import create_app
from jazzband.assets import assets
from jazzband.commands import SyncMembers, SyncProjects

app = create_app('jazzband.config')
if 'HEROKU_APP_NAME' in os.environ:
    app.wsgi_app = ProxyFix(app.wsgi_app)

manager = Manager(app)
manager.add_command('assets', ManageAssets(assets))
manager.add_command('db', MigrateCommand)
manager.add_command('sync_members', SyncMembers())
manager.add_command('sync_projects', SyncProjects())

if __name__ == "__main__":
    manager.run()
<|endoftext|>


<commit_before>from flask import render_template, request, redirect, url_for, abort, current_app
from flask_login import login_required, current_user, flash

from .. import main
from . import get_template_data
from ... import data_api_client
from ..forms import EmailAddressForm
from ..auth import role_required
from dmutils.apiclient.errors import HTTPError
from dmutils.audit import AuditTypes
from dmutils.email import send_email, \
    generate_token, MandrillException

@main.route('/suppliers', methods=['GET'])
@login_required
@role_required('admin', 'admin-ccs-category')
def find_suppliers():
    suppliers = data_api_client.find_suppliers(prefix=request.args.get("supplier_name_prefix"))

    return render_template(
        "view_suppliers.html",
        suppliers=suppliers['suppliers'],
        **get_template_data()
    )

@main.route('/suppliers/<string:supplier_id>/edit/name', methods=['GET'])
@login_required
@role_required('admin', 'admin-ccs-category')
def edit_supplier_name(supplier_id):

    supplier = get_supplier(supplier_id)

    return render_template(
        "edit_supplier_name.html",
        supplier=supplier["suppliers"],
        **get_template_data()
    )

@main.route('/suppliers/<string:supplier_id>/edit/name', methods=['POST'])
def update_supplier_name(supplier_id):

    supplier = get_supplier(supplier_id)
    new_supplier_name = request.form.get('new_supplier_name', '')

    data_api_client.update_supplier(
        supplier['suppliers']['id'], {'name': new_supplier_name}, current_user.email_address
    )

    return redirect(url_for('.find_suppliers', supplier_name_prefix=new_supplier_name[:1]))

@main.route('/suppliers/users', methods=['GET'])
@login_required
@role_required('admin', 'admin-ccs-category')
def find_supplier_users():
    form = EmailAddressForm()

    supplier = get_supplier()
    users = data_api_client.find_users(request.args.get("supplier_id"))

    return render_template(
        "view_supplier_users.html",
        users=users["users"],
        form=form,
        supplier=supplier["suppliers"],
        **get_template_data()
    )

@main.route('/suppliers/users/<int:user_id>/unlock', methods=['POST'])
@login_required
@role_required('admin')
def unlock_user(user_id):
    user = data_api_client.update_user(user_id, locked=False)
    return redirect(url_for('.find_supplier_users', supplier_id=user['users']['supplier']['supplierId']))

@main.route('/suppliers/users/<int:user_id>/activate', methods=['POST'])
@login_required
@role_required('admin')
def activate_user(user_id):
    user = data_api_client.update_user(user_id, active=True)
    return redirect(url_for('.find_supplier_users', supplier_id=user['users']['supplier']['supplierId']))

@main.route('/suppliers/users/<int:user_id>/deactivate', methods=['POST'])
@login_required
@role_required('admin')
def deactivate_user(user_id):
    user = data_api_client.update_user(user_id, active=False)
    return redirect(url_for('.find_supplier_users', supplier_id=user['users']['supplier']['supplierId']))

@main.route('/suppliers/services', methods=['GET'])
@login_required
@role_required('admin', 'admin-ccs-category')
def find_supplier_services():

    supplier = get_supplier()
    services = data_api_client.find_services(request.args.get("supplier_id"))

    return render_template(
        "view_supplier_services.html",
        services=services["services"],
        supplier=supplier["suppliers"],
        **get_template_data()
    )

@main.route('/suppliers/<int:supplier_id>/invite-user', methods=['POST'])
@login_required
@role_required('admin')
def invite_user(supplier_id):
    form = EmailAddressForm()

    try:
        suppliers = data_api_client.get_supplier(supplier_id)
        users = data_api_client.find_users(supplier_id)
    except HTTPError as e:
        current_app.logger.error(str(e), supplier_id)
        if e.status_code != 404:
            raise
        else:
            abort(404, "Supplier not found")

    if form.validate_on_submit():
        token = generate_token(
            {
                "supplier_id": supplier_id,
                "supplier_name": suppliers['suppliers']['name'],
                "email_address": form.email_address.data
            },
            current_app.config['SHARED_EMAIL_KEY'],
            current_app.config['INVITE_EMAIL_SALT']
        )

        url = "{}{}/{}".format(
            request.url_root,
            current_app.config['CREATE_USER_PATH'],
            format(token)
        )

        email_body = render_template(
            "emails/invite_user_email.html",
            url=url,
            supplier=suppliers['suppliers']['name'])

        try:
            send_email(
                form.email_address.data,
                email_body,
                current_app.config['DM_MANDRILL_API_KEY'],
                current_app.config['INVITE_EMAIL_SUBJECT'],
                current_app.config['INVITE_EMAIL_FROM'],
                current_app.config['INVITE_EMAIL_NAME'],
                ["user-invite"]
            )
        except MandrillException as e:
            current_app.logger.error(
                "Invitation email failed to send error {} to {} supplier {} supplier id {} ".format(
                    str(e),
                    form.email_address.data,
                    current_user.supplier_name,
                    current_user.supplier_id)
            )
            abort(503, "Failed to send user invite reset")

        data_api_client.create_audit_event(
            audit_type=AuditTypes.invite_user,
            user=current_user.email_address,
            object_type='suppliers',
            object_id=supplier_id,
            data={'invitedEmail': form.email_address.data})

        flash('user_invited', 'success')
        return redirect(url_for('.find_supplier_users', supplier_id=supplier_id))
    else:
        return render_template(
            "view_supplier_users.html",
            form=form,
            users=users["users"],
            supplier=suppliers["suppliers"],
            **get_template_data()
        ), 400

def get_supplier(supplier_id=None):

    if supplier_id is None:
        if "supplier_id" not in request.args or len(request.args.get("supplier_id")) <= 0:
            abort(404, "Supplier not found")
        supplier_id = request.args.get("supplier_id")

    try:
        int(supplier_id)
    except ValueError:
        abort(400, "invalid supplier id {}".format(supplier_id))

    try:
        return data_api_client.get_supplier(supplier_id)
    except HTTPError as e:
        if e.status_code != 404:
            raise
        else:
            abort(404, "Supplier not found")
<commit_msg>Use appropriate roles for editing supplier name<commit_after>from flask import render_template, request, redirect, url_for, abort, current_app
from flask_login import login_required, current_user, flash

from .. import main
from . import get_template_data
from ... import data_api_client
from ..forms import EmailAddressForm
from ..auth import role_required
from dmutils.apiclient.errors import HTTPError
from dmutils.audit import AuditTypes
from dmutils.email import send_email, \
    generate_token, MandrillException

@main.route('/suppliers', methods=['GET'])
@login_required
@role_required('admin', 'admin-ccs-category')
def find_suppliers():
    suppliers = data_api_client.find_suppliers(prefix=request.args.get("supplier_name_prefix"))

    return render_template(
        "view_suppliers.html",
        suppliers=suppliers['suppliers'],
        **get_template_data()
    )

@main.route('/suppliers/<string:supplier_id>/edit/name', methods=['GET'])
@login_required
@role_required('admin')
def edit_supplier_name(supplier_id):

    supplier = get_supplier(supplier_id)

    return render_template(
        "edit_supplier_name.html",
        supplier=supplier["suppliers"],
        **get_template_data()
    )

@main.route('/suppliers/<string:supplier_id>/edit/name', methods=['POST'])
@login_required
@role_required('admin')
def update_supplier_name(supplier_id):

    supplier = get_supplier(supplier_id)
    new_supplier_name = request.form.get('new_supplier_name', '')

    data_api_client.update_supplier(
        supplier['suppliers']['id'], {'name': new_supplier_name}, current_user.email_address
    )

    return redirect(url_for('.find_suppliers', supplier_name_prefix=new_supplier_name[:1]))

@main.route('/suppliers/users', methods=['GET'])
@login_required
@role_required('admin', 'admin-ccs-category')
def find_supplier_users():
    form = EmailAddressForm()

    supplier = get_supplier()
    users = data_api_client.find_users(request.args.get("supplier_id"))

    return render_template(
        "view_supplier_users.html",
        users=users["users"],
        form=form,
        supplier=supplier["suppliers"],
        **get_template_data()
    )

@main.route('/suppliers/users/<int:user_id>/unlock', methods=['POST'])
@login_required
@role_required('admin')
def unlock_user(user_id):
    user = data_api_client.update_user(user_id, locked=False)
    return redirect(url_for('.find_supplier_users', supplier_id=user['users']['supplier']['supplierId']))

@main.route('/suppliers/users/<int:user_id>/activate', methods=['POST'])
@login_required
@role_required('admin')
def activate_user(user_id):
    user = data_api_client.update_user(user_id, active=True)
    return redirect(url_for('.find_supplier_users', supplier_id=user['users']['supplier']['supplierId']))

@main.route('/suppliers/users/<int:user_id>/deactivate', methods=['POST'])
@login_required
@role_required('admin')
def deactivate_user(user_id):
    user = data_api_client.update_user(user_id, active=False)
    return redirect(url_for('.find_supplier_users', supplier_id=user['users']['supplier']['supplierId']))

@main.route('/suppliers/services', methods=['GET'])
@login_required
@role_required('admin', 'admin-ccs-category')
def find_supplier_services():

    supplier = get_supplier()
    services = data_api_client.find_services(request.args.get("supplier_id"))

    return render_template(
        "view_supplier_services.html",
        services=services["services"],
        supplier=supplier["suppliers"],
        **get_template_data()
    )

@main.route('/suppliers/<int:supplier_id>/invite-user', methods=['POST'])
@login_required
@role_required('admin')
def invite_user(supplier_id):
    form = EmailAddressForm()

    try:
        suppliers = data_api_client.get_supplier(supplier_id)
        users = data_api_client.find_users(supplier_id)
    except HTTPError as e:
        current_app.logger.error(str(e), supplier_id)
        if e.status_code != 404:
            raise
        else:
            abort(404, "Supplier not found")

    if form.validate_on_submit():
        token = generate_token(
            {
                "supplier_id": supplier_id,
                "supplier_name": suppliers['suppliers']['name'],
                "email_address": form.email_address.data
            },
            current_app.config['SHARED_EMAIL_KEY'],
            current_app.config['INVITE_EMAIL_SALT']
        )

        url = "{}{}/{}".format(
            request.url_root,
            current_app.config['CREATE_USER_PATH'],
            format(token)
        )

        email_body = render_template(
            "emails/invite_user_email.html",
            url=url,
            supplier=suppliers['suppliers']['name'])

        try:
            send_email(
                form.email_address.data,
                email_body,
                current_app.config['DM_MANDRILL_API_KEY'],
                current_app.config['INVITE_EMAIL_SUBJECT'],
                current_app.config['INVITE_EMAIL_FROM'],
                current_app.config['INVITE_EMAIL_NAME'],
                ["user-invite"]
            )
        except MandrillException as e:
            current_app.logger.error(
                "Invitation email failed to send error {} to {} supplier {} supplier id {} ".format(
                    str(e),
                    form.email_address.data,
                    current_user.supplier_name,
                    current_user.supplier_id)
            )
            abort(503, "Failed to send user invite reset")

        data_api_client.create_audit_event(
            audit_type=AuditTypes.invite_user,
            user=current_user.email_address,
            object_type='suppliers',
            object_id=supplier_id,
            data={'invitedEmail': form.email_address.data})

        flash('user_invited', 'success')
        return redirect(url_for('.find_supplier_users', supplier_id=supplier_id))
    else:
        return render_template(
            "view_supplier_users.html",
            form=form,
            users=users["users"],
            supplier=suppliers["suppliers"],
            **get_template_data()
        ), 400

def get_supplier(supplier_id=None):

    if supplier_id is None:
        if "supplier_id" not in request.args or len(request.args.get("supplier_id")) <= 0:
            abort(404, "Supplier not found")
        supplier_id = request.args.get("supplier_id")

    try:
        int(supplier_id)
    except ValueError:
        abort(400, "invalid supplier id {}".format(supplier_id))

    try:
        return data_api_client.get_supplier(supplier_id)
    except HTTPError as e:
        if e.status_code != 404:
            raise
        else:
            abort(404, "Supplier not found")
<|endoftext|>


<commit_before>#!/usr/bin/env python
import getopt
import importlib
import logging
import os
import site
import sys

ROOT = os.path.dirname(os.path.abspath(__file__))
if os.path.splitext(os.path.basename(__file__))[0] == 'cProfile':
    if os.environ.get('ZAMBONI_PATH'):
        ROOT = os.environ['ZAMBONI_PATH']
    else:
        print 'When using cProfile you must set $ZAMBONI_PATH'
        sys.exit(2)

path = lambda *a: os.path.join(ROOT, *a)

prev_sys_path = list(sys.path)

# Boo, path manipulation we need to stop these.
site.addsitedir(path('apps'))
site.addsitedir(path('vendor'))
site.addsitedir(path('vendor/lib/python'))

# Move the new items to the front of sys.path. (via virtualenv)
new_sys_path = []
for item in list(sys.path):
    if item not in prev_sys_path:
        new_sys_path.append(item)
        sys.path.remove(item)
sys.path[:0] = new_sys_path

# No third-party imports until we've added all our sitedirs!
from django.core.management import (call_command, execute_manager,
                                    setup_environ)

# Allow a user to pass in settings into manage.py and use that for our
# own purposes. If you don't use that we'll fall back to whatever is
# defined for DJANGO_SETTINGS_MODULE.
found, sys.argv[1:] = getopt.getopt(sys.argv[1:], 's:', 'settings=')
try:
    setting = dict(found).values()[0]
    if setting:
        os.environ['DJANGO_SETTINGS_MODULE'] = setting
except IndexError:
    pass

setting = os.environ.get('DJANGO_SETTINGS_MODULE', '')

# The average Django user will have DJANGO_SETTINGS_MODULE set to settings
# for our purposes that means, load the default site, so if nothing is
# specified by now, use the default.
if setting in ('settings', ''):
    setting = 'default'

# Because I'm lazy and want to type less characters, let's assume
# settings_local if not specified.
if not setting.endswith(('.settings_local', '.settings')):
    setting = setting + '.settings_local'

settings = importlib.import_module(setting)
setup_environ(settings)

# Hardcore monkeypatching action.
import safe_django_forms
safe_django_forms.monkeypatch()

import session_csrf
session_csrf.monkeypatch()

# Fix jinja's Markup class to not crash when localizers give us bad format
# strings.
from jinja2 import Markup
mod = Markup.__mod__
trans_log = logging.getLogger('z.trans')

def new(self, arg):
    try:
        return mod(self, arg)
    except Exception:
        trans_log.error(unicode(self))
        return ''

Markup.__mod__ = new

logging = getattr(settings, 'ZAMBONI_LOGGING_FILE', None)
if logging:
    importlib.import_module(logging)

import djcelery
djcelery.setup_loader()

from lib.misc import safe_signals
safe_signals.start_the_machine()

if __name__ == "__main__":
    # If product details aren't present, get them.
    from product_details import product_details
    if not product_details.last_update:
        print 'Product details missing, downloading...'
        call_command('update_product_details')
        product_details.__init__()  # reload the product details

    execute_manager(settings)
<commit_msg>importlib is 2.7 fun<commit_after>#!/usr/bin/env python
import getopt
import logging
import os
import site
import sys

ROOT = os.path.dirname(os.path.abspath(__file__))
if os.path.splitext(os.path.basename(__file__))[0] == 'cProfile':
    if os.environ.get('ZAMBONI_PATH'):
        ROOT = os.environ['ZAMBONI_PATH']
    else:
        print 'When using cProfile you must set $ZAMBONI_PATH'
        sys.exit(2)

path = lambda *a: os.path.join(ROOT, *a)

prev_sys_path = list(sys.path)

# Boo, path manipulation we need to stop these.
site.addsitedir(path('apps'))
site.addsitedir(path('vendor'))
site.addsitedir(path('vendor/lib/python'))

# Move the new items to the front of sys.path. (via virtualenv)
new_sys_path = []
for item in list(sys.path):
    if item not in prev_sys_path:
        new_sys_path.append(item)
        sys.path.remove(item)
sys.path[:0] = new_sys_path

# No third-party imports until we've added all our sitedirs!
from django.core.management import (call_command, execute_manager,
                                    setup_environ)
from django.utils import importlib

# Allow a user to pass in settings into manage.py and use that for our
# own purposes. If you don't use that we'll fall back to whatever is
# defined for DJANGO_SETTINGS_MODULE.
found, sys.argv[1:] = getopt.getopt(sys.argv[1:], 's:', 'settings=')
try:
    setting = dict(found).values()[0]
    if setting:
        os.environ['DJANGO_SETTINGS_MODULE'] = setting
except IndexError:
    pass

setting = os.environ.get('DJANGO_SETTINGS_MODULE', '')

# The average Django user will have DJANGO_SETTINGS_MODULE set to settings
# for our purposes that means, load the default site, so if nothing is
# specified by now, use the default.
if setting in ('settings', ''):
    setting = 'default'

# Because I'm lazy and want to type less characters, let's assume
# settings_local if not specified.
if not setting.endswith(('.settings_local', '.settings')):
    setting = setting + '.settings_local'

settings = importlib.import_module(setting)
setup_environ(settings)

# Hardcore monkeypatching action.
import safe_django_forms
safe_django_forms.monkeypatch()

import session_csrf
session_csrf.monkeypatch()

# Fix jinja's Markup class to not crash when localizers give us bad format
# strings.
from jinja2 import Markup
mod = Markup.__mod__
trans_log = logging.getLogger('z.trans')

def new(self, arg):
    try:
        return mod(self, arg)
    except Exception:
        trans_log.error(unicode(self))
        return ''

Markup.__mod__ = new

logging = getattr(settings, 'ZAMBONI_LOGGING_FILE', None)
if logging:
    importlib.import_module(logging)

import djcelery
djcelery.setup_loader()

from lib.misc import safe_signals
safe_signals.start_the_machine()

if __name__ == "__main__":
    # If product details aren't present, get them.
    from product_details import product_details
    if not product_details.last_update:
        print 'Product details missing, downloading...'
        call_command('update_product_details')
        product_details.__init__()  # reload the product details

    execute_manager(settings)
<|endoftext|>