def _initialize_field_type_sets():
    """Using reflection, populate _DATE_FIELDS, _ENUM_FIELDS, and _CODE_FIELDS, which are
  used when formatting JSON from participant summaries.

  We call this lazily to avoid having issues with the code getting executed while SQLAlchemy
  is still initializing itself. Locking ensures we only run throught the code once.
  """
    with _fields_lock:
        # Return if this is already initialized.
        if _DATE_FIELDS:
            return
        for prop_name in dir(ParticipantSummary):
            if prop_name.startswith("_"):
                continue
            prop = getattr(ParticipantSummary, prop_name)
            if callable(prop):
                continue
            property_type = get_property_type(prop)
            if property_type:
                if property_type == PropertyType.DATE or property_type == PropertyType.DATETIME:
                    _DATE_FIELDS.add(prop_name)
                elif property_type == PropertyType.ENUM:
                    _ENUM_FIELDS.add(prop_name)
                elif property_type == PropertyType.INTEGER:
                    fks = prop.property.columns[0].foreign_keys
                    if fks:
                        for fk in fks:
                            if fk._get_colspec() == 'code.code_id':
                                _CODE_FIELDS.add(prop_name)
                                break
示例#2
0
 def make_query_filter(self, field_name, value):
   """Attempts to make a query filter for the model property with the specified name, matching
   the specified value. If no such property exists, None is returned.
   """
   prop = getattr(self.model_type, field_name, None)
   if prop:
     property_type = get_property_type(prop)
     filter_value = None
     operator = Operator.EQUALS
     # If we're dealing with a comparable property type, look for a prefix that indicates an
     # operator other than EQUALS and strip it off
     if property_type in _COMPARABLE_PROPERTY_TYPES:
       for prefix, op in _OPERATOR_PREFIX_MAP.iteritems():
         if isinstance(value, (str, unicode)) and value.startswith(prefix):
           operator = op
           value = value[len(prefix):]
           break
     filter_value = self._parse_value(prop, property_type, value)
     return FieldFilter(field_name, operator, filter_value)
   else:
     return None
 def _from_json_value(self, prop, value):
     property_type = get_property_type(prop)
     result = self._parse_value(prop, property_type, value)
     return result