本文整理汇总了Python中sprox.sa.support.resolve_entity函数的典型用法代码示例。如果您正苦于以下问题:Python resolve_entity函数的具体用法?Python resolve_entity怎么用?Python resolve_entity使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了resolve_entity函数的17个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: _modify_params_for_relationships
def _modify_params_for_relationships(self, entity, params, delete_first=True):
entity = resolve_entity(entity)
mapper = class_mapper(entity)
relations = self.get_relations(entity)
for relation in relations:
if relation in params:
prop = mapper.get_property(relation)
target = prop.argument
target = resolve_entity(target)
value = params[relation]
if value:
if prop.uselist and isinstance(value, list):
target_obj = []
for v in value:
try:
object_mapper(v)
target_obj.append(v)
except UnmappedInstanceError:
if hasattr(target, 'primary_key'):
pk = target.primary_key
else:
pk = class_mapper(target).primary_key
if isinstance(v, string_type) and "/" in v:
v = map(self._adapt_type, v.split("/"), pk)
v = tuple(v)
else:
v = self._adapt_type(v, pk[0])
#only add those items that come back
new_v = self.session.query(target).get(v)
if new_v is not None:
target_obj.append(new_v)
elif prop.uselist:
try:
object_mapper(value)
target_obj = [value]
except UnmappedInstanceError:
mapper = target
if not isinstance(target, Mapper):
mapper = class_mapper(target)
if isinstance(mapper.primary_key[0].type, Integer):
value = int(value)
target_obj = [self.session.query(target).get(value)]
else:
try:
object_mapper(value)
target_obj = value
except UnmappedInstanceError:
if isinstance(value, string_type) and "/" in value:
value = map(self._adapt_type, value.split("/"), prop.remote_side)
value = tuple(value)
else:
value = self._adapt_type(value, list(prop.remote_side)[0])
target_obj = self.session.query(target).get(value)
params[relation] = target_obj
else:
del params[relation]
return params
开发者ID:garrettmc,项目名称:TGWikiTutorial,代码行数:58,代码来源:provider.py
示例2: _get_related_class
def _get_related_class(self, entity, relation):
entity = resolve_entity(entity)
mapper = class_mapper(entity)
prop = mapper.get_property(relation)
target = resolve_entity(prop.argument)
if not hasattr(target, 'class_'):
target = class_mapper(target)
return target.class_
开发者ID:TurboGears,项目名称:sprox,代码行数:10,代码来源:provider.py
示例3: _modify_params_for_dates
def _modify_params_for_dates(self, entity, params):
entity = resolve_entity(entity)
mapper = class_mapper(entity)
for key, value in list(params.items()):
if key in mapper.c and value is not None:
field = mapper.c[key]
if hasattr(field, 'type'):
if isinstance(field.type, DateTime):
if not isinstance(value, datetime):
dt = datetime.strptime(value[:19], '%Y-%m-%d %H:%M:%S')
params[key] = dt
elif isinstance(field.type, Date):
if not isinstance(value, date):
dt = datetime.strptime(value, '%Y-%m-%d').date()
params[key] = dt
elif isinstance(field.type, Interval):
if not isinstance(value, timedelta):
d = re.match(
r'((?P<days>\d+) days, )?(?P<hours>\d+):'
r'(?P<minutes>\d+):(?P<seconds>\d+)',
str(value)).groupdict(0)
dt = timedelta(**dict(( (key, int(value))
for key, value in list(d.items()) )))
params[key] = dt
return params
开发者ID:garrettmc,项目名称:TGWikiTutorial,代码行数:25,代码来源:provider.py
示例4: get_dropdown_options
def get_dropdown_options(self, entity, field_name, view_names=None):
if view_names is None:
view_names = ['_name', 'name', 'description', 'title']
if self.session is None:
warn('No dropdown options will be shown for %s. '
'Try passing the session into the initialization '
'of your form base object so that this sprocket '
'can have values in the drop downs'%entity)
return []
field = self.get_field(entity, field_name)
target_field = entity
if isinstance(field, PropertyLoader):
target_field = field.argument
target_field = resolve_entity(target_field)
#some kind of relation
if isinstance(target_field, Mapper):
target_field = target_field.class_
pk_fields = self.get_primary_fields(target_field)
view_name = self.get_view_field_name(target_field, view_names)
rows = self.session.query(target_field).all()
if len(pk_fields) == 1:
def build_pk(row):
return getattr(row, pk_fields[0])
else:
def build_pk(row):
return "/".join([str(getattr(row, pk)) for pk in pk_fields])
return [ (build_pk(row), getattr(row, view_name)) for row in rows ]
开发者ID:garrettmc,项目名称:TGWikiTutorial,代码行数:35,代码来源:provider.py
示例5: is_unique
def is_unique(self, entity, field_name, value):
entity = resolve_entity(entity)
field = getattr(entity, field_name)
try:
self.session.query(entity).filter(field==value).one()
except NoResultFound:
return True
return False
开发者ID:garrettmc,项目名称:TGWikiTutorial,代码行数:8,代码来源:provider.py
示例6: get_field
def get_field(self, entity, name):
entity = resolve_entity(entity)
mapper = class_mapper(entity)
try:
return getattr(mapper.c, name)
except (InvalidRequestError, AttributeError):
try:
return mapper.get_property(name)
except InvalidRequestError:
raise AttributeError
开发者ID:garrettmc,项目名称:TGWikiTutorial,代码行数:10,代码来源:provider.py
示例7: is_relation
def is_relation(self, entity, field_name):
entity = resolve_entity(entity)
mapper = class_mapper(entity)
try:
property = mapper.get_property(field_name)
except InvalidRequestError:
return False
if isinstance(property, PropertyLoader):
return True
开发者ID:garrettmc,项目名称:TGWikiTutorial,代码行数:11,代码来源:provider.py
示例8: get_fields
def get_fields(self, entity):
entity = resolve_entity(entity)
mapper = class_mapper(entity)
field_names = list(mapper.c.keys())
for prop in mapper.iterate_properties:
try:
getattr(mapper.c, prop.key)
field_names.append(prop.key)
except AttributeError:
mapper.get_property(prop.key)
field_names.append(prop.key)
return field_names
开发者ID:garrettmc,项目名称:TGWikiTutorial,代码行数:13,代码来源:provider.py
示例9: get_primary_fields
def get_primary_fields(self, entity):
entity = resolve_entity(entity)
mapper = class_mapper(entity)
fields = []
for field_name in self.get_fields(entity):
try:
value = getattr(mapper.c, field_name)
except AttributeError:
# Relations won't be attributes, but can't be primary anyway.
continue
if value.primary_key and not field_name in fields:
fields.append(field_name)
return fields
开发者ID:garrettmc,项目名称:TGWikiTutorial,代码行数:14,代码来源:provider.py
示例10: query
def query(self, entity, limit=None, offset=None, limit_fields=None,
order_by=None, desc=False, field_names=[], filters={},
substring_filters=[], **kw):
entity = resolve_entity(entity)
query = self.session.query(entity)
filters = self._modify_params_for_dates(entity, filters)
filters = self._modify_params_for_relationships(entity, filters)
for field_name, value in filters.items():
field = getattr(entity, field_name)
if self.is_relation(entity, field_name) and isinstance(value, list):
value = value[0]
query = query.filter(field.contains(value))
elif field_name in substring_filters and self.is_string(entity, field_name):
escaped_value = re.sub('[\\\\%\\[\\]_]', '\\\\\g<0>', value.lower())
query = query.filter(func.lower(field).contains(escaped_value, escape='\\'))
else:
query = query.filter(field==value)
count = query.count()
if order_by is not None:
if self.is_relation(entity, order_by):
mapper = class_mapper(entity)
class_ = None
for prop in mapper.iterate_properties:
try:
class_ = prop.mapper.class_
except (AttributeError, KeyError):
pass
query = self.session.query(entity).join(order_by)
f = self.get_view_field_name(class_, field_names)
field = self.get_field(class_, f)
else:
field = self.get_field(entity, order_by)
if desc:
field = _desc(field)
query = query.order_by(field)
if offset is not None:
query = query.offset(offset)
if limit is not None:
query = query.limit(limit)
objs = query.all()
return count, objs
开发者ID:garrettmc,项目名称:TGWikiTutorial,代码行数:49,代码来源:provider.py
示例11: create
def create(self, entity, params):
entity = resolve_entity(entity)
params = self._modify_params_for_dates(entity, params)
params = self._modify_params_for_relationships(entity, params)
obj = entity()
relations = self.get_relations(entity)
mapper = class_mapper(entity)
for key, value in params.items():
if value is not None:
if isinstance(value, FieldStorage):
value = value.file.read()
try:
if key not in relations and value and isinstance(mapper.columns[key].type, Integer):
value = int(value)
except KeyError:
pass
setattr(obj, key, value)
self.session.add(obj)
self.session.flush()
return obj
开发者ID:TurboGears,项目名称:sprox,代码行数:22,代码来源:provider.py
示例12: get_synonyms
def get_synonyms(self, entity):
entity = resolve_entity(entity)
mapper = class_mapper(entity)
return [prop.key for prop in mapper.iterate_properties if isinstance(prop, SynonymProperty)]
开发者ID:garrettmc,项目名称:TGWikiTutorial,代码行数:4,代码来源:provider.py
示例13: _find_title_column
def _find_title_column(self, entity):
entity = resolve_entity(entity)
for column in class_mapper(entity).columns:
if 'title' in column.info and column.info['title']:
return column.key
return None
开发者ID:garrettmc,项目名称:TGWikiTutorial,代码行数:6,代码来源:provider.py
示例14: query
def query(self, entity, limit=None, offset=None, limit_fields=None,
order_by=None, desc=False, filters={},
substring_filters=[], search_related=False, related_field_names=None,
**kw):
entity = resolve_entity(entity)
query = self.session.query(entity)
filters = self._modify_params_for_dates(entity, filters)
if search_related:
# Values for related fields contain the text to search
filters = self._modify_params_for_related_searches(entity, filters,
view_names=related_field_names,
substrings=substring_filters)
else:
# Values for related fields contain the primary key
filters = self._modify_params_for_relationships(entity, filters)
for field_name, value in filters.items():
field = getattr(entity, field_name)
if self.is_relation(entity, field_name) and isinstance(value, list):
related_class = self._get_related_class(entity, field_name)
related_pk = self.get_primary_field(related_class)
related_pk_col = getattr(related_class, related_pk)
related_pk_values = (getattr(v, related_pk) for v in value)
if self._relates_many(entity, field_name):
field_filter = field.any(related_pk_col.in_(related_pk_values))
else:
field_filter = field.has(related_pk_col.in_(related_pk_values))
query = query.filter(field_filter)
elif field_name in substring_filters and self.is_string(entity, field_name):
escaped_value = self._escape_like(value.lower())
query = query.filter(func.lower(field).contains(escaped_value, escape='*'))
else:
query = query.filter(field==value)
count = query.count()
if order_by is not None:
if not isinstance(order_by, (tuple, list)):
order_by = [order_by]
if not isinstance(desc, (tuple, list)):
desc = [desc]
for sort_by, sort_descending in zip_longest(order_by, desc):
if self.is_relation(entity, sort_by):
mapper = class_mapper(entity)
class_ = self._get_related_class(entity, sort_by)
query = query.outerjoin(sort_by)
f = self.get_view_field_name(class_, related_field_names)
field = self.get_field(class_, f)
else:
field = self.get_field(entity, sort_by)
if sort_descending:
field = _desc(field)
query = query.order_by(field)
if offset is not None:
query = query.offset(offset)
if limit is not None:
query = query.limit(limit)
objs = query.all()
return count, objs
开发者ID:TurboGears,项目名称:sprox,代码行数:67,代码来源:provider.py
示例15: _get_obj
def _get_obj(self, entity, pkdict):
entity = resolve_entity(entity)
pk_names = self.get_primary_fields(entity)
pks = tuple([pkdict[n] for n in pk_names])
return self.session.query(entity).get(pks)
开发者ID:garrettmc,项目名称:TGWikiTutorial,代码行数:5,代码来源:provider.py
示例16: get_relations
def get_relations(self, entity):
entity = resolve_entity(entity)
mapper = class_mapper(entity)
return [prop.key for prop in mapper.iterate_properties if isinstance(prop, PropertyLoader)]
开发者ID:garrettmc,项目名称:TGWikiTutorial,代码行数:4,代码来源:provider.py
示例17: _relates_many
def _relates_many(self, entity, field_name):
entity = resolve_entity(entity)
mapper = class_mapper(entity)
property = mapper.get_property(field_name)
return property.uselist
开发者ID:TurboGears,项目名称:sprox,代码行数:5,代码来源:provider.py
注:本文中的sprox.sa.support.resolve_entity函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论