1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 from sqlalchemy
.ext
.declarative
import declarative_base
19 from sqlalchemy
import inspect
21 from mediagoblin
.tools
.transition
import DISABLE_GLOBALS
23 if not DISABLE_GLOBALS
:
24 from sqlalchemy
.orm
import scoped_session
, sessionmaker
25 Session
= scoped_session(sessionmaker())
28 class GMGTableBase(object):
31 return inspect(self
).session
35 return self
._session
.bind
.app
37 if not DISABLE_GLOBALS
:
38 query
= Session
.query_property()
41 return getattr(self
, key
)
43 def setdefault(self
, key
, defaultvalue
):
44 # The key *has* to exist on sql.
45 return getattr(self
, key
)
47 def save(self
, commit
=True):
49 if sess
is None and not DISABLE_GLOBALS
:
51 assert sess
is not None, "Can't save, %r has a detached session" % self
58 def delete(self
, commit
=True):
59 """Delete the object and commit the change immediately by default"""
61 assert sess
is not None, "Not going to delete detached %r" % self
67 Base
= declarative_base(cls
=GMGTableBase
)
70 class DictReadAttrProxy(object):
72 Maps read accesses to obj['key'] to obj.key
73 and hides all the rest of the obj
75 def __init__(self
, proxied_obj
):
76 self
.proxied_obj
= proxied_obj
78 def __getitem__(self
, key
):
80 return getattr(self
.proxied_obj
, key
)
81 except AttributeError:
82 raise KeyError("%r is not an attribute on %r"
83 % (key
, self
.proxied_obj
))