本文整理汇总了Python中sqlalchemy.orm.object_mapper函数的典型用法代码示例。如果您正苦于以下问题:Python object_mapper函数的具体用法?Python object_mapper怎么用?Python object_mapper使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了object_mapper函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
def __init__(self, user=None, service=None, action=None,
field_name=None, old_value=None, new_value=None, **kw):
"""
The *service* argument should be a string such as 'Scheduler' or
'XMLRPC', describing the means by which the change has been made. This
constructor will override it with something more specific (such as the
name of an external service) if appropriate.
"""
super(Activity, self).__init__(**kw)
self.user = user
self.service = service
try:
if identity.current.proxied_by_user is not None:
self.service = identity.current.proxied_by_user.user_name
except identity.RequestRequiredException:
pass
field_name_value_max_length = object_mapper(self).c.field_name.type.length
old_value_max_length = object_mapper(self).c.old_value.type.length
new_value_max_length = object_mapper(self).c.new_value.type.length
self.field_name = field_name[:field_name_value_max_length]
self.action = action
if old_value is not None:
old_value = unicode(old_value)[:old_value_max_length]
if new_value is not None:
new_value = unicode(new_value)[:new_value_max_length]
self.old_value = old_value
self.new_value = new_value
示例2: __init__
def __init__(self, user=None, service=None, action=None,
field_name=None, old_value=None, new_value=None, **kw):
"""
The *service* argument should be a string such as 'Scheduler' or
'XMLRPC', describing the means by which the change has been made. This
constructor will override it with something more specific (such as the
name of an external service) if appropriate.
"""
super(Activity, self).__init__(**kw)
self.user = user
self.service = service
try:
if identity.current.proxied_by_user is not None:
self.service = identity.current.proxied_by_user.user_name
except identity.RequestRequiredException:
pass
self.field_name = field_name
self.action = action
# These values are likely to be truncated by MySQL, so let's make sure
# we don't end up with invalid UTF-8 chars at the end
if old_value and isinstance(old_value, unicode):
old_value = unicode_truncate(old_value,
bytes_length=object_mapper(self).c.old_value.type.length)
if new_value and isinstance(new_value, unicode):
new_value = unicode_truncate(new_value,
bytes_length=object_mapper(self).c.new_value.type.length)
self.old_value = old_value
self.new_value = new_value
示例3: copy
def copy(self, obj_source):
pk_keys = set([c.key for c in object_mapper(obj_source).primary_key])
#pk_keys = []
keys = [p.key for p in object_mapper(obj_source).iterate_properties if (p.key not in pk_keys) & (isinstance(p, sqlalchemy.orm.ColumnProperty))]
obj_dest = obj_source.__class__.__new__(obj_source.__class__)
obj_dest.__init__()
if app.verbose:
src = "src(" + str(type(obj_source)) + " " + str(obj_source)
dst = "dst(" + str(type(obj_dest)) + " " + str(obj_dest) + ")"
for k in keys:
v = getattr(obj_source, k)
if (k == "password") & (obj_source != app.user):
v = "hidden_password"
else:
if type(v) is str:
v = self.unicode(v)
if app.verbose:
src += ", " + str(k) + ": " + str(type(v)) + " " + str(v)
setattr(obj_dest, k, v)
if app.verbose:
src += ")"
dst += ")"
print src + "->" + dst
return obj_dest
示例4: as_json
def as_json(self):
date_formatter = date.getLocaleFormatter(common.get_request(), "date",
"medium"
)
items = [
dict(
item_type = self.item_type,
item_id = orm.object_mapper(item).primary_key_from_instance(
item
)[0],
item_title = IDCDescriptiveProperties(item).title,
status = IWorkflow(item).get_state(item.status).title,
status_date = ( date_formatter.format(item.submission_date)
if (hasattr(item, "submission_date") and
getattr(item, "submission_date")
)
else None
),
registry_number = ( item.registry_number if
hasattr(item, "registry_number") else None
),
item_mover = ( IDCDescriptiveProperties(item.owner).title if
hasattr(item, "owner") else None
),
item_uri = "%s-%d" % (self.item_type,
orm.object_mapper(item).primary_key_from_instance(item)[0]
)
)
for item in self.query()
]
items = sorted(items, key=lambda item:item.get("status_date"),
reverse=True
)
return json.dumps(dict(items=items))
示例5: as_json
def as_json(self):
is_text = IScheduleText.implementedBy(self.domain_class)
date_formatter = date.getLocaleFormatter(common.get_request(), "date",
"medium"
)
items = [
dict(
item_type = self.item_type,
item_id = orm.object_mapper(item).primary_key_from_instance(
item
)[0],
item_title = item.text if \
is_text else IDCDescriptiveProperties(item).title,
status = IWorkflow(item).get_state(item.status).title if not \
is_text else None,
status_date = date_formatter.format(item.submission_date) if \
getattr(item, "submission_date", None) else None,
registry_number = item.registry_number if \
hasattr(item, "registry_number") else None,
item_mover = IDCDescriptiveProperties(item.owner).title if \
hasattr(item, "owner") else None,
item_uri = "%s-%d" % (self.item_type,
orm.object_mapper(item).primary_key_from_instance(item)[0]
)
)
for item in self.query()
]
items = sorted(items, key=lambda item:item.get("status_date"),
reverse=True
)
return json.dumps(dict(items=items))
示例6: to_dict
def to_dict(self, deep={}, exclude=[]):
"""Generate a JSON-style nested dict/list structure from an selfect."""
col_prop_names = [p.key for p in object_mapper(self).iterate_properties \
if isinstance(p, ColumnProperty)]
data = dict([(name, getattr(self, name))
for name in col_prop_names if name not in exclude])
# objects can provide a default view
if not deep:
deep = self.__default_deep__
if not exclude:
exclude = self.__default_exclude__
if deep:
for rname, rdeep in deep.iteritems():
dbdata = getattr(self, rname)
#FIXME: use attribute names (ie coltoprop) instead of column names
fks = object_mapper(self).get_property(rname).remote_side
exclude = [c.name for c in fks]
if dbdata is None:
data[rname] = None
elif isinstance(dbdata, list):
data[rname] = [o.to_dict(rdeep, exclude) for o in dbdata]
else:
data[rname] = dbdata.to_dict(rdeep, exclude)
return data
示例7: trigger_attribute_change_events
def trigger_attribute_change_events(object_, action):
from sqlalchemy import inspect
from sqlalchemy.orm import object_mapper, ColumnProperty
if object_mapper(object_).class_ not in registry:
return False
for mapper_property in object_mapper(object_).iterate_properties:
if isinstance(mapper_property, ColumnProperty) and \
mapper_property.class_attribute in registry[object_mapper(object_).class_]:
an_index = (object_mapper(object_).class_, mapper_property.class_attribute)
key = mapper_property.key
attribute_state = inspect(object_).attrs.get(key)
new_value = attribute_state.value
old_value = get_old_value(attribute_state)
if action == 'insert':
old_value = None
if action == 'delete':
new_value = None
if action == 'update':
if not attribute_state.history.has_changes():
new_value = ''
old_value = ''
g.functions_to_call_after_commit[an_index] = []
if new_value != old_value:
for f in registry[object_mapper(object_).class_][mapper_property.class_attribute]:
add = f(object_, new_value, old_value, action)
if add:
g.functions_to_call_after_commit[an_index].append(add)
示例8: __init__
def __init__(self, context):
self.context = context
session = Session()
trusted = removeSecurityProxy(context)
session.merge(trusted)
try:
self.oid = orm.object_mapper( trusted ).primary_key_from_instance(trusted)[0]
except UnboundExecutionError:
session.add(trusted)
self.oid = orm.object_mapper( trusted ).primary_key_from_instance(trusted)[0]
self.object_type = context.__class__.__name__.lower()
示例9: _to_dict
def _to_dict(instance, deep=None, exclude=None):
"""Returns a dictionary representing the fields of the specified `instance`
of a SQLAlchemy model.
`deep` is a dictionary containing a mapping from a relation name (for a
relation of `instance`) to either a list or a dictionary. This is a
recursive structure which represents the `deep` argument when calling
`_to_dict` on related instances. When an empty list is encountered,
`_to_dict` returns a list of the string representations of the related
instances.
`exclude` specifies the columns which will *not* be present in the returned
dictionary representation of the object.
"""
deep = deep or {}
exclude = exclude or ()
# create the dictionary mapping column name to value
columns = (p.key for p in object_mapper(instance).iterate_properties
if isinstance(p, ColumnProperty))
result = dict((col, getattr(instance, col)) for col in columns)
# Convert datetime and date objects to ISO 8601 format.
#
# TODO We can get rid of this when issue #33 is resolved.
for key, value in result.items():
if isinstance(value, datetime.date):
result[key] = value.isoformat()
# recursively call _to_dict on each of the `deep` relations
for relation, rdeep in deep.iteritems():
# exclude foreign keys of the related object for the recursive call
relationproperty = object_mapper(instance).get_property(relation)
newexclude = (key.name for key in relationproperty.remote_side)
# Get the related value so we can see if it is None, a list, a query
# (as specified by a dynamic relationship loader), or an actual
# instance of a model.
relatedvalue = getattr(instance, relation)
# HACK: In case the relatedvalue is a dynamically loaded
# relationship, we need to resolve the query into a concrete
# list of objects; see issue #89. We should also check to see
# if relatedvalue is a many-to-one relationship, in order to
# call relatedvalue.one() or something, but I don't know how
# to do that.
if isinstance(relatedvalue, (AppenderMixin, Query)):
relatedvalue = relatedvalue.all()
if relatedvalue is None:
result[relation] = None
elif isinstance(relatedvalue, list):
result[relation] = [_to_dict(inst, rdeep, newexclude)
for inst in relatedvalue]
else:
result[relation] = _to_dict(relatedvalue, rdeep, newexclude)
return result
示例10: as_json
def as_json(self):
date_formatter = date.getLocaleFormatter(common.get_request(), "date",
"medium"
)
items_json = dict(
items = [
dict(
item_type = self.item_type,
item_id = orm.object_mapper(item).primary_key_from_instance(
item
)[0],
item_title = IDCDescriptiveProperties(item).title,
status = IWorkflow(item).get_state(item.status).title,
status_date = ( date_formatter.format(item.submission_date)
if hasattr(item, "submission_date") else None
),
registry_number = ( item.registry_number if
hasattr(item, "registry_number") else None
),
item_mover = ( IDCDescriptiveProperties(item.owner).title if
hasattr(item, "owner") else None
),
item_uri = IDCDescriptiveProperties(item).uri
)
for item in self.query()
]
)
return json.dumps(items_json)
示例11: with_parent
def with_parent(self, instance, property=None):
"""add a join criterion corresponding to a relationship to the given parent instance.
instance
a persistent or detached instance which is related to class represented
by this query.
property
string name of the property which relates this query's class to the
instance. if None, the method will attempt to find a suitable property.
currently, this method only works with immediate parent relationships, but in the
future may be enhanced to work across a chain of parent mappers.
"""
from sqlalchemy.orm import properties
mapper = object_mapper(instance)
if property is None:
for prop in mapper.iterate_properties:
if isinstance(prop, properties.PropertyLoader) and prop.mapper is self.mapper:
break
else:
raise exceptions.InvalidRequestError("Could not locate a property which relates instances of class '%s' to instances of class '%s'" % (self.mapper.class_.__name__, instance.__class__.__name__))
else:
prop = mapper.get_property(property, resolve_synonyms=True)
return self.filter(Query._with_lazy_criterion(instance, prop))
示例12: create_version
def create_version(obj, session, deleted = False):
obj_mapper = object_mapper(obj)
history_mapper = obj.__history_mapper__
history_cls = history_mapper.class_
obj_state = attributes.instance_state(obj)
attr = {}
obj_changed = False
for om, hm in zip(obj_mapper.iterate_to_root(), history_mapper.iterate_to_root()):
if hm.single:
continue
for hist_col in hm.local_table.c:
if hist_col.key == 'version':
continue
obj_col = om.local_table.c[hist_col.key]
# get the value of the
# attribute based on the MapperProperty related to the
# mapped column. this will allow usage of MapperProperties
# that have a different keyname than that of the mapped column.
try:
prop = obj_mapper.get_property_by_column(obj_col)
except UnmappedColumnError:
# in the case of single table inheritance, there may be
# columns on the mapped table intended for the subclass only.
# the "unmapped" status of the subclass column on the
# base class is a feature of the declarative module as of sqla 0.5.2.
continue
# expired object attributes and also deferred cols might not be in the
# dict. force it to load no matter what by using getattr().
if prop.key not in obj_state.dict:
getattr(obj, prop.key)
a, u, d = attributes.get_history(obj, prop.key)
if d:
attr[hist_col.key] = d[0]
obj_changed = True
elif u:
attr[hist_col.key] = u[0]
else:
# if the attribute had no value.
attr[hist_col.key] = a[0]
obj_changed = True
if not obj_changed and not deleted:
return
attr['version'] = obj.version
hist = history_cls()
for key, value in attr.iteritems():
setattr(hist, key, value)
session.add(hist)
obj.version += 1
示例13: _move
def _move(self, direction, context):
"""Swap a line with another line.
If ``direction`` is ``'up'``, swap with the previous line.
If ``direction`` is ``'down'``, swap with the next line.
"""
cond = None
pkey = object_mapper(self).primary_key[0].key
if direction == 'up':
if self._order != 1:
cond = self._order_column == (self._order - 1)
values = {self._order_column: self._order}
self._set_order(self._order - 1)
elif direction == 'down':
if self._order < self._max_order(context):
cond = self._order_column == (self._order + 1)
values = {self._order_column: self._order}
self._set_order(self._order + 1)
if cond is not None and values:
# Flush it now, so that it works
self.query.session.flush()
(self.__class__
.query.filter(cond).filter(context)
.filter(getattr(self.__class__, pkey) != getattr(self, pkey))
.update(values))
示例14: create
def create(self, message, manual=False):
"""Store the existing state of the adapted context as a new version.
"""
context = self.__parent__
if manual:
if not self.has_write_permission(context):
raise Unauthorized
version = self.domain_model()
trusted = removeSecurityProxy(context)
# set values on version from context
self._copyFields(trusted, version)
# content domain ids are typically not in the interfaces
# manually inspect and look for one, by hand to save on the new version
mapper = orm.object_mapper(trusted)
version.content_id = mapper.primary_key_from_instance(trusted)[0]
version.status = None
version.manual = manual
# we rely on change handler to attach the change object to the version
event.notify(
interfaces.VersionCreated(context, self, version, message))
session = Session()
session.add(version)
version.context = context
event.notify(ObjectCreatedEvent(version))
return version
示例15: createAndAdd
def createAndAdd(self, data):
domain_model = self.domain_model
# create the object, inspect data for constructor args
try:
ob = createInstance(domain_model, data)
except TypeError:
ob = domain_model()
# apply any context values
self.finishConstruction(ob)
# apply extra form values
form.applyChanges(ob, self.form_fields, data, self.adapters)
# save the object, id is generated by db on flush
self.context[""] = ob
# flush so we have database id
bungeni.alchemist.Session().flush()
# fire an object created event
notify(ObjectCreatedEvent(ob))
# signal to add form machinery to go to next url
self._finished_add = True
mapper = orm.object_mapper(ob)
# TODO single primary key (need changes to base container)
oid = mapper.primary_key_from_instance(ob)
# retrieve the object with location and security information
return self.context[oid]