-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathcommon.py
35 lines (26 loc) · 912 Bytes
/
common.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from functools import wraps
from pony.orm import ObjectNotFound, db_session
from models import db
@db_session
def renew_object(obj):
return type(obj)[obj.id]
def auto_renew_objects(func):
"""
Decorator to ensure objects are got from current db_session before executing
the function.
"""
@wraps(func)
def wrapped(*args, **kwargs):
with db_session(optimistic=False):
args = list(args)
try:
for ind, arg in enumerate(args):
if arg and issubclass(type(arg), db.Entity):
args[ind] = renew_object(arg)
for key, val in kwargs.items():
if val and issubclass(type(val), db.Entity):
kwargs[key] = renew_object(val)
except ObjectNotFound:
return
return func(*args, **kwargs)
return wrapped