diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2007-07-27 04:08:53 +0000 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2007-07-27 04:08:53 +0000 |
commit | ed4fc64bb0ac61c27bc4af32962fb129e74a36bf (patch) | |
tree | c1cf2fb7b1cafced82a8898e23d2a0bf5ced8526 /lib/sqlalchemy/ext/assignmapper.py | |
parent | 3a8e235af64e36b3b711df1f069d32359fe6c967 (diff) | |
download | sqlalchemy-ed4fc64bb0ac61c27bc4af32962fb129e74a36bf.tar.gz |
merging 0.4 branch to trunk. see CHANGES for details. 0.3 moves to maintenance branch in branches/rel_0_3.
Diffstat (limited to 'lib/sqlalchemy/ext/assignmapper.py')
-rw-r--r-- | lib/sqlalchemy/ext/assignmapper.py | 59 |
1 files changed, 29 insertions, 30 deletions
diff --git a/lib/sqlalchemy/ext/assignmapper.py b/lib/sqlalchemy/ext/assignmapper.py index 4708afd8d..238041702 100644 --- a/lib/sqlalchemy/ext/assignmapper.py +++ b/lib/sqlalchemy/ext/assignmapper.py @@ -1,51 +1,50 @@ -from sqlalchemy import mapper, util, Query, exceptions +from sqlalchemy import util, exceptions import types - -def monkeypatch_query_method(ctx, class_, name): - def do(self, *args, **kwargs): - query = Query(class_, session=ctx.current) - return getattr(query, name)(*args, **kwargs) - try: - do.__name__ = name - except: - pass - setattr(class_, name, classmethod(do)) - -def monkeypatch_objectstore_method(ctx, class_, name): +from sqlalchemy.orm import mapper + +def _monkeypatch_session_method(name, ctx, class_): def do(self, *args, **kwargs): session = ctx.current - if name == "flush": - # flush expects a list of objects - self = [self] return getattr(session, name)(self, *args, **kwargs) try: do.__name__ = name except: pass - setattr(class_, name, do) - + if not hasattr(class_, name): + setattr(class_, name, do) + def assign_mapper(ctx, class_, *args, **kwargs): + extension = kwargs.pop('extension', None) + if extension is not None: + extension = util.to_list(extension) + extension.append(ctx.mapper_extension) + else: + extension = ctx.mapper_extension + validate = kwargs.pop('validate', False) + if not isinstance(getattr(class_, '__init__'), types.MethodType): def __init__(self, **kwargs): for key, value in kwargs.items(): if validate: - if not key in self.mapper.props: + if not self.mapper.get_property(key, resolve_synonyms=False, raiseerr=False): raise exceptions.ArgumentError("Invalid __init__ argument: '%s'" % key) setattr(self, key, value) class_.__init__ = __init__ - extension = kwargs.pop('extension', None) - if extension is not None: - extension = util.to_list(extension) - extension.append(ctx.mapper_extension) - else: - extension = ctx.mapper_extension + + class query(object): + def __getattr__(self, key): + return getattr(ctx.current.query(class_), key) + def __call__(self): + return ctx.current.query(class_) + + if not hasattr(class_, 'query'): + class_.query = query() + + for name in ['refresh', 'expire', 'delete', 'expunge', 'update']: + _monkeypatch_session_method(name, ctx, class_) + m = mapper(class_, extension=extension, *args, **kwargs) class_.mapper = m - class_.query = classmethod(lambda cls: Query(class_, session=ctx.current)) - for name in ['get', 'filter', 'filter_by', 'select', 'select_by', 'selectfirst', 'selectfirst_by', 'selectone', 'selectone_by', 'get_by', 'join_to', 'join_via', 'count', 'count_by', 'options', 'instances']: - monkeypatch_query_method(ctx, class_, name) - for name in ['flush', 'delete', 'expire', 'refresh', 'expunge', 'merge', 'save', 'update', 'save_or_update']: - monkeypatch_objectstore_method(ctx, class_, name) return m |