Esempio n. 1
0
 def target_field(self):
     """
     When filtering against this relation, returns the field on the remote
     model against which the filtering should happen.
     """
     target_fields = self.get_path_info()[-1].target_fields
     if len(target_fields) > 1:
         raise exceptions.FieldError(
             "Can't use target_field for multicolumn relations.")
     return target_fields[0]
Esempio n. 2
0
    def target_field(self):
        """
<<<<<<< HEAD
        When filtering against this relation, returns the field on the remote
=======
        When filtering against this relation, return the field on the remote
>>>>>>> 37c99181c9a6b95433d60f8c8ef9af5731096435
        model against which the filtering should happen.
        """
        target_fields = self.get_path_info()[-1].target_fields
        if len(target_fields) > 1:
            raise exceptions.FieldError("Can't use target_field for multicolumn relations.")
        return target_fields[0]
Esempio n. 3
0
 def get_transform(self, name):
     transform = super(TSVectorField, self).get_transform(name)
     if transform:
         return transform
     try:
         if name in map(lambda x: x[1],
                        self.model._meta.get_field(
                            self.dictionary).choices):
             return DictionaryTransformFactory(name)
         else:
             raise exceptions.FieldError(
                 "The '%s' is not in %s choices" %
                 (name, self.model._meta.get_field(self.dictionary)))
     except ValueError:
         pass
Esempio n. 4
0
    def get_db_prep_lookup(self,
                           lookup_type,
                           value,
                           connection,
                           prepared=False):

        if lookup_type not in self.valid_lookups:
            raise exceptions.FieldError("'%s' isn't valid Lookup for %s" %
                                        (lookup_type, self.__class__.__name__))

        return [
            self.get_db_prep_value(self._get_db_prep_lookup(
                lookup_type, value),
                                   connection=connection,
                                   prepared=prepared)
        ]
Esempio n. 5
0
    def _do_checks(self):
        assert not self.weights or (
            len(self.weights) is 4
            and all(map(lambda x: isinstance(x, (int, float)), self.weights))
        ), 'weights must be of length 4 and type float or integer'

        assert not self.normalization or all(
            map(lambda x: (isinstance(x, int) and x in self.NORMALIZATION),
                self.normalization)), 'normalization must be in (%s)' % (
                    ', '.join('%d' % i for i in self.NORMALIZATION))
        assert len(self.extra) == 1, 'to many arguments for %s' % (
            self.__class__.__name__)
        if self.srt_lookup not in TSVectorBaseField.valid_lookups:
            raise exceptions.FieldError(
                "The '%s' isn't valid Lookup for %s" %
                (self.srt_lookup, self.__class__.__name__))
Esempio n. 6
0
    def get_lookup_constraint(self, constraint_class, alias, targets, sources,
                              lookups, raw_value):
        from django.db.models.sql.where import Constraint
        assert len(targets) == len(sources)

        if isinstance(lookups, basestring):
            lookup_type = lookups
        else:
            if len(lookups) > 1:
                raise exceptions.FieldError(
                    'UnixDateTimeField fields do not support nested lookups')
            lookup_type = lookups[0]

        if lookup_type == 'isnull' and self.zero_null and raw_value:
            return (Constraint(alias, targets[0].column,
                               targets[0]), 'exact', None)
        else:
            return (Constraint(alias, targets[0].column,
                               self), lookup_type, raw_value)
Esempio n. 7
0
    def _get_lookup_constraint(self, constraint_class, alias, targets, sources,
                               lookups, raw_value):
        from django.core import exceptions
        from django.db.models.sql.where import AND

        root_constraint = constraint_class()

        # we need some checks from ForeignObject.get_lookup_constraint
        # before we could check the lookups.
        assert len(targets) == len(sources)
        if len(lookups) > 1:
            raise exceptions.FieldError('%s does not support nested lookups' %
                                        self.__class__.__name__)

        lookup_type = lookups[0]
        target = targets[0]
        source = sources[0]

        # use custom Lookups when applicable
        if lookup_type in ['isempty', 'overlap', 'contains']:
            if lookup_type == 'isempty':
                root_constraint.add(
                    IsEmptyLookup(target.get_col(alias, source), raw_value),
                    AND)
            elif lookup_type == 'overlap':
                root_constraint.add(
                    RelatedOverlapLookup(target.get_col(alias, source),
                                         raw_value), AND)
            elif lookup_type == 'contains':
                root_constraint.add(
                    RelatedContainsLookup(target.get_col(alias, source),
                                          raw_value), AND)
            return root_constraint
        elif lookup_type in ('in', 'exact', 'isnull'):
            raise TypeError(
                "%s doesn't allow exact, in or isnull. Use contains, overlap or isempty respectively"
                % self.__class__.__name__)

        return super(RelatedIteratorField,
                     self).get_lookup_constraint(constraint_class, alias,
                                                 targets, sources, lookups,
                                                 raw_value)
Esempio n. 8
0
 def vals_tuple(value: int):
     if ActivityTypeEnum.is_viable_enum(value):
         return ActivityTypeEnum.TYPES_DICT[value]
     return exceptions.FieldError(
         "type_val is not a viable ActivityTypeEnum value")
Esempio n. 9
0
def q_from_condition(condition):
    """
    Build a Lucene query from a filtering condition.
    """
    q = None
    field = condition.field
    attname = field.attname
    def escape_wilds(s):
        return str(s).replace('*','\*').replace('?','\?')
    if condition.operator is OPERATORS.EXACT:
        q = Q(attname, field.to_neo_index(condition.value))
    elif condition.operator is OPERATORS.STARTSWITH:
        q = Q(attname, '%s*' % escape_wilds(condition.value), wildcard=True)
    elif condition.operator is OPERATORS.CONTAINS:
        q = Q(attname, '*%s*' % escape_wilds(condition.value), wildcard=True)
    elif condition.operator is OPERATORS.MEMBER:
        q = Q(attname, field.member_to_neo_index(condition.value))
    elif condition.operator is OPERATORS.IN:
        q = reduce(or_, (Q(attname, field.to_neo_index(v)) for v in condition.value))
    elif condition.operator is OPERATORS.MEMBER_IN:
        q = reduce(or_, (Q(attname, field.member_to_neo_index(v)) for v in condition.value))
    #FIXME OBOE with field.MAX + exrange, not sure it's easy to fix though...
    elif condition.operator in (OPERATORS.GT, OPERATORS.GTE, OPERATORS.LT,
                                OPERATORS.LTE, OPERATORS.RANGE): 
        if not field.indexed_range:
            raise exceptions.FieldError(
                'The {0} property is not configured for range '
                'indexing.'.format(field.attname))
        fieldtype = field._property_type()
        if condition.operator in (OPERATORS.GT, OPERATORS.GTE):
            if not hasattr(field, 'MAX'):
                raise exceptions.FieldError(
                    'The {0} property is not configured for gt/gte '
                    'queries.'.format(field.attname))
            if condition.operator is OPERATORS.GT:
                q = Q(attname, exrange=(field.to_neo_index(condition.value),
                                        field.to_neo_index(fieldtype.MAX)))
            else:
                q = Q(attname, inrange=(field.to_neo_index(condition.value),
                                        field.to_neo_index(fieldtype.MAX)))
        elif condition.operator in (OPERATORS.LT, OPERATORS.LTE):
            if not hasattr(fieldtype, 'MIN'):
                raise exceptions.FieldError(
                    'The {0} property is not configured for lt/lte '
                    'queries.'.format(field.attname))
            if condition.operator is OPERATORS.LT:
                q = Q(attname, exrange=(field.to_neo_index(fieldtype.MIN),
                                        field.to_neo_index(condition.value)))
            else:
                q = Q(attname, inrange=(field.to_neo_index(fieldtype.MIN),
                                        field.to_neo_index(condition.value)))
        elif condition.operator is OPERATORS.RANGE:
            if len(condition.value) != 2:
                raise exceptions.ValidationError('Range queries need upper and'
                                                ' lower bounds.')
            q = Q(condition.field.attname,
                inrange=[condition.field.to_neo_index(v)
                         for v in condition.value])
    else:
        return None
    if condition.negate:
        q = ~q
    return q