Beispiel #1
0
    def _validateParam(self, name, descParam, value):
        """
        Validates and transforms a single parameter that was passed. Raises
        RestException if the passed value is invalid.

        :param name: The name of the param.
        :type name: str
        :param descParam: The formal parameter in the Description.
        :type descParam: dict
        :param value: The value passed in for this param for the current request.
        :returns: The value transformed
        """
        type = descParam.get('type')

        # Coerce to the correct data type
        if type == 'string':
            value = self._handleString(name, descParam, value)
        elif type == 'boolean':
            value = toBool(value)
        elif type == 'integer':
            value = self._handleInt(name, descParam, value)
        elif type == 'number':
            value = self._handleNumber(name, descParam, value)

        # Enum validation (should be afer type coercion)
        if 'enum' in descParam and value not in descParam['enum']:
            raise RestException(
                'Invalid value for %s: "%s". Allowed values: %s.' %
                (name, value, ', '.join(str(v) for v in descParam['enum'])))

        return value
Beispiel #2
0
    def _validateParam(self, name, descParam, value):
        """
        Validates and transforms a single parameter that was passed. Raises
        RestException if the passed value is invalid.

        :param name: The name of the param.
        :type name: str
        :param descParam: The formal parameter in the Description.
        :type descParam: dict
        :param value: The value passed in for this param for the current request.
        :returns: The value transformed
        """
        type = descParam.get('type')

        # Coerce to the correct data type
        if type == 'string':
            value = self._handleString(name, descParam, value)
        elif type == 'boolean':
            value = toBool(value)
        elif type == 'integer':
            value = self._handleInt(name, descParam, value)
        elif type == 'number':
            value = self._handleNumber(name, descParam, value)

        # Enum validation (should be after type coercion)
        if 'enum' in descParam and value not in descParam['enum']:
            raise RestException('Invalid value for %s: "%s". Allowed values: %s.' % (
                name, value, ', '.join(str(v) for v in descParam['enum'])))

        return value
Beispiel #3
0
    def boolParam(self, key, params, default=None):
        """
        Coerce a parameter value from a str to a bool.

        :param key: The parameter key to test.
        :type key: str
        :param params: The request parameters.
        :type params: dict
        :param default: The default value if no key is passed.
        :type default: bool or None
        """
        if key not in params:
            return default

        return toBool(params[key])
Beispiel #4
0
    def boolParam(self, key, params, default=None):
        """
        Coerce a parameter value from a str to a bool.

        :param key: The parameter key to test.
        :type key: str
        :param params: The request parameters.
        :type params: dict
        :param default: The default value if no key is passed.
        :type default: bool or None
        """
        if key not in params:
            return default

        return toBool(params[key])
    def find_calc(self, moleculeId=None, imageName=None, inputParametersHash=None, inputGeometryHash=None, pending=None, offset=0, limit=None, sort=None):
        user = getCurrentUser()

        query = { }

        if moleculeId:
            query['moleculeId'] = ObjectId(moleculeId)

        if imageName:
            repository, tag = oc.parse_image_name(imageName)
            query['image.repository'] = repository
            query['image.tag'] = tag

        if inputParametersHash:
            query['input.parametersHash'] = inputParametersHash

        if inputGeometryHash:
            query['input.geometryHash'] = inputGeometryHash

        if pending is not None:
            pending = toBool(pending)
            query['properties.pending'] = pending
            # The absence of the field mean the calculation is not pending ...
            if not pending:
                query['properties.pending'] = {
                    '$ne': True
                }

        fields = ['image', 'input',
                  'cjson', 'cjson.vibrations.modes', 'cjson.vibrations.intensities',
                  'cjson.vibrations.frequencies', 'properties', 'fileId', 'access',
                  'moleculeId', 'public']

        calcs = self._model.find(query, fields=fields, sort=sort)
        calcs = self._model.filterResultsByPermission(calcs, user,
            AccessType.READ, limit=limit)
        calcs = [self._model.filter(x, user) for x in calcs]

        return calcs
Beispiel #6
0
def _validateBoolean(doc):
    doc['value'] = toBool(doc['value'])
Beispiel #7
0
    def find_calc(self, params):
        user = getCurrentUser()

        query = {}

        if 'moleculeId' in params:
            query['moleculeId'] = ObjectId(params['moleculeId'])
        if 'calculationType' in params:
            calculation_type = params['calculationType']
            if not isinstance(calculation_type, list):
                calculation_type = [calculation_type]

            query['properties.calculationTypes'] = {'$all': calculation_type}

        if 'functional' in params:
            query['properties.functional'] = params.get('functional').lower()

        if 'theory' in params:
            query['properties.theory'] = params.get('theory').lower()

        if 'basis' in params:
            query['properties.basisSet.name'] = params.get('basis').lower()

        if 'pending' in params:
            pending = toBool(params['pending'])
            query['properties.pending'] = pending
            # The absence of the field mean the calculation is not pending ...
            if not pending:
                query['properties.pending'] = {'$ne': True}

        limit = params.get('limit', 50)

        fields = [
            'cjson.vibrations.modes', 'cjson.vibrations.intensities',
            'cjson.vibrations.frequencies', 'properties', 'fileId', 'access',
            'public'
        ]
        sort = None
        sort_by_theory = toBool(params.get('sortByTheory', False))
        if sort_by_theory:
            sort = [('properties.theoryPriority', pymongo.DESCENDING)]
            # Exclude calculations that don't have a theoryPriority,
            # otherwise they will appear first in the list.
            query['properties.theoryPriority'] = {'$exists': True}

        calcs = self._model.find(query, fields=fields, sort=sort)
        calcs = self._model.filterResultsByPermission(calcs,
                                                      user,
                                                      AccessType.READ,
                                                      limit=int(limit))
        calcs = [self._model.filter(x, user) for x in calcs]

        not_sortable = []
        if sort_by_theory and len(calcs) < int(limit):
            # Now select any calculations without theoryPriority
            query['properties.theoryPriority'] = {'$exists': False}
            not_sortable = self._model.find(query, fields=fields)
            not_sortable = self._model.filterResultsByPermission(
                not_sortable,
                user,
                AccessType.READ,
                limit=int(limit) - len(calcs))
            not_sortable = [self._model.filter(x, user) for x in not_sortable]

        return calcs + not_sortable
Beispiel #8
0
def _validateBoolean(doc):
    doc['value'] = toBool(doc['value'])