Attach the MediaGoblinApp to the engine, and provide a way for models to access
[mediagoblin.git] / mediagoblin / db / base.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
3 #
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.
8 #
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.
13 #
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/>.
16
17
18 from sqlalchemy.ext.declarative import declarative_base
19 from sqlalchemy import inspect
20
21 from mediagoblin.tools.transition import DISABLE_GLOBALS
22
23 if not DISABLE_GLOBALS:
24 from sqlalchemy.orm import scoped_session, sessionmaker
25 Session = scoped_session(sessionmaker())
26
27
28 class GMGTableBase(object):
29 @property
30 def _session(self):
31 return inspect(self).session
32
33 @property
34 def _app(self):
35 return self._session.bind.app
36
37 if not DISABLE_GLOBALS:
38 query = Session.query_property()
39
40 def get(self, key):
41 return getattr(self, key)
42
43 def setdefault(self, key, defaultvalue):
44 # The key *has* to exist on sql.
45 return getattr(self, key)
46
47 def save(self, commit=True):
48 sess = self._session
49 if sess is None and not DISABLE_GLOBALS:
50 sess = Session()
51 assert sess is not None, "Can't save, %r has a detached session" % self
52 sess.add(self)
53 if commit:
54 sess.commit()
55 else:
56 sess.flush()
57
58 def delete(self, commit=True):
59 """Delete the object and commit the change immediately by default"""
60 sess = self._session
61 assert sess is not None, "Not going to delete detached %r" % self
62 sess.delete(self)
63 if commit:
64 sess.commit()
65
66
67 Base = declarative_base(cls=GMGTableBase)
68
69
70 class DictReadAttrProxy(object):
71 """
72 Maps read accesses to obj['key'] to obj.key
73 and hides all the rest of the obj
74 """
75 def __init__(self, proxied_obj):
76 self.proxied_obj = proxied_obj
77
78 def __getitem__(self, key):
79 try:
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))