Esempio n. 1
0
 def _unique_filter_from(model, datum):
     unique_columns = [c for c in model.__mapper__.columns if c.unique]
     for unique_column in unique_columns:
         if unique_column.key in datum:
             unique_value = datum[unique_column.key]
             if unique_value:
                 return {
                     unique_column.key:
                     dehumanize_if_needed(unique_column, unique_value)
                 }
Esempio n. 2
0
    def _filter_from(model, datum):
        if '__SEARCH_BY__' not in datum or not datum['__SEARCH_BY__']:
            unique_filter = model._unique_filter_from(datum)
            if unique_filter:
                return unique_filter
            return model._primary_filter_from(datum)

        search_by_keys = datum['__SEARCH_BY__']
        if not isinstance(search_by_keys, list):
            search_by_keys = [search_by_keys]
        search_by_keys = set(search_by_keys)

        filter_dict = {}

        columns = model.__mapper__.columns
        column_keys = set(columns.keys()).intersection(search_by_keys)
        for key in column_keys:
            column = columns[key]
            value = dehumanize_if_needed(column, datum.get(key))
            filter_dict[key] = value

        relationships = model.__mapper__.relationships
        relationship_keys = set(
            relationships.keys()).intersection(search_by_keys)
        for key in relationship_keys:
            if key in search_by_keys:
                filter_dict[key] = datum.get(key)

        synonyms = model.__mapper__.synonyms
        synonym_keys = set(synonyms.keys()).intersection(search_by_keys)
        for key in synonym_keys:
            column = synonyms[key]._proxied_property.columns[0]
            if key in search_by_keys:
                value = dehumanize_if_needed(column, datum.get(key))
                filter_dict[key] = value

        return filter_dict
Esempio n. 3
0
 def _try_to_set_attribute(self, column, key, value):
     value = dehumanize_if_needed(column, value)
     if isinstance(value, str):
         if isinstance(column.type, Integer):
             self._try_to_set_attribute_with_decimal_value(
                 column, key, value, 'integer')
         elif isinstance(column.type, (Float, Numeric)):
             self._try_to_set_attribute_with_decimal_value(
                 column, key, value, 'float')
         elif isinstance(column.type, DateTime):
             self._try_to_set_attribute_with_deserialized_datetime(
                 column, key, value)
         elif isinstance(column.type, String):
             setattr(self, key, value)
         elif isinstance(column.type, UUID):
             self._try_to_set_attribute_with_uuid(column, key, value)
     elif not isinstance(value, datetime) and isinstance(
             column.type, DateTime):
         self._try_to_set_attribute_with_deserialized_datetime(
             column, key, value)
     else:
         setattr(self, key, value)
Esempio n. 4
0
 def _primary_filter_from(model, datum):
     return dict([(column.key,
                   dehumanize_if_needed(column, datum.get(column.key)))
                  for column in model.__mapper__.primary_key])