|
1
|
|
|
# ~*~ coding: utf-8 ~*~ |
|
|
|
|
|
|
2
|
|
|
#- |
|
3
|
|
|
# OSMAlchemy - OpenStreetMap to SQLAlchemy bridge |
|
4
|
|
|
# Copyright (c) 2016 Michael Bayer (zzzeek) |
|
5
|
|
|
# Copyright (c) 2016 Dominik George <[email protected]> |
|
6
|
|
|
# |
|
7
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8
|
|
|
# of this software and associated documentation files (the "Software"), to deal |
|
9
|
|
|
# in the Software without restriction, including without limitation the rights |
|
10
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
11
|
|
|
# copies of the Software, and to permit persons to whom the Software is |
|
12
|
|
|
# furnished to do so, subject to the following conditions: |
|
13
|
|
|
# |
|
14
|
|
|
# The above copyright notice and this permission notice shall be included in all |
|
15
|
|
|
# copies or substantial portions of the Software. |
|
16
|
|
|
# |
|
17
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
18
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
19
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
20
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
21
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
22
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
23
|
|
|
# SOFTWARE. |
|
24
|
|
|
|
|
25
|
1 |
|
def monkey_patch_sqlalchemy(): |
|
|
|
|
|
|
26
|
1 |
|
from sqlalchemy.ext.associationproxy import AssociationProxy |
|
27
|
1 |
|
from sqlalchemy.util import memoized_property |
|
28
|
|
|
|
|
29
|
|
|
# Monkey patch support for chained association proxy queries into SQLAlchemy |
|
30
|
|
|
# https://bitbucket.org/zzzeek/sqlalchemy/issues/3769/chained-any-has-with-association-proxy |
|
31
|
1 |
|
if not hasattr(AssociationProxy, "_unwrap_target_assoc_proxy"): |
|
32
|
1 |
|
def _unwrap_target_assoc_proxy(self): |
|
33
|
|
|
attr = getattr(self.target_class, self.value_attr) |
|
34
|
|
|
if isinstance(attr, AssociationProxy): |
|
35
|
|
|
return attr, getattr(self.target_class, attr.target_collection) |
|
36
|
|
|
return None, None |
|
37
|
1 |
|
AssociationProxy._unwrap_target_assoc_proxy = memoized_property(_unwrap_target_assoc_proxy) |
|
|
|
|
|
|
38
|
|
|
|
|
39
|
1 |
|
orig_any = AssociationProxy.any |
|
40
|
1 |
|
def any(self, criterion=None, **kwargs): |
|
|
|
|
|
|
41
|
|
|
target_assoc, inner = self._unwrap_target_assoc_proxy |
|
|
|
|
|
|
42
|
|
|
if target_assoc is not None: |
|
43
|
|
|
if target_assoc._target_is_object and target_assoc._uselist: |
|
|
|
|
|
|
44
|
|
|
inner = inner.any(criterion=criterion, **kwargs) |
|
45
|
|
|
else: |
|
46
|
|
|
inner = inner.has(criterion=criterion, **kwargs) |
|
47
|
|
|
return self._comparator.any(inner) |
|
|
|
|
|
|
48
|
|
|
orig_any(self, criterion, **kwargs) |
|
49
|
1 |
|
AssociationProxy.any = any |
|
50
|
|
|
|
|
51
|
1 |
|
orig_has = AssociationProxy.has |
|
52
|
1 |
|
def has(self, criterion=None, **kwargs): |
|
|
|
|
|
|
53
|
|
|
target_assoc, inner = self._unwrap_target_assoc_proxy |
|
|
|
|
|
|
54
|
|
|
if target_assoc is not None: |
|
55
|
|
|
if target_assoc._target_is_object and target_assoc._uselist: |
|
|
|
|
|
|
56
|
|
|
inner = inner.any(criterion=criterion, **kwargs) |
|
57
|
|
|
else: |
|
58
|
|
|
inner = inner.has(criterion=criterion, **kwargs) |
|
59
|
|
|
return self._comparator.has(inner) |
|
|
|
|
|
|
60
|
|
|
orig_has(self, criterion, **kwargs) |
|
61
|
|
|
AssociationProxy.has = has |
|
62
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.