Switch to unified view

a/Allura/allura/model/session.py b/Allura/allura/model/session.py
...
...
6
from ming.datastore import ShardedDataStore
6
from ming.datastore import ShardedDataStore
7
from ming.orm.base import state, session
7
from ming.orm.base import state, session
8
from ming.orm.ormsession import ThreadLocalORMSession, SessionExtension
8
from ming.orm.ormsession import ThreadLocalORMSession, SessionExtension
9
9
10
from allura.lib import search
10
from allura.lib import search
11
from allura.lib.custom_middleware import environ
12
11
13
log = logging.getLogger(__name__)
12
log = logging.getLogger(__name__)
14
13
15
class ProjectSession(Session):
14
class ProjectSession(Session):
16
15
...
...
18
        self.main_session = main_session
17
        self.main_session = main_session
19
18
20
    @property
19
    @property
21
    def db(self):
20
    def db(self):
22
        try:
21
        try:
23
            p = self.project
24
            if p.database_uri:
22
            if c.project.database_uri:
25
                scheme, rest = p.database_uri.split('://')
23
                scheme, rest = c.project.database_uri.split('://')
26
                host, database = rest.split('/', 1)
24
                host, database = rest.split('/', 1)
27
                return ShardedDataStore.get(scheme + '://' + host, database).db
25
                return ShardedDataStore.get(scheme + '://' + host, database).db
28
            return getattr(self.main_session.bind.conn, p.database)
26
            return getattr(self.main_session.bind.conn, c.project.database)
29
        except (KeyError, AttributeError), ex:
27
        except (KeyError, AttributeError, TypeError), ex:
30
            return None
28
            return None
31
32
    @property
33
    def project(self):
34
        # Our MagicalC makes sure allura.project is set in the environ when
35
        # c.project is set
36
        p = environ.get('allura.project', None)
37
        if p is None:
38
            # But if we're not using MagicalC, as in paster shell....
39
            try:
40
                p = getattr(c, 'project', None)
41
            except TypeError:
42
                 # running without MagicalC, inside a request (likely EasyWidgets)
43
                return None
44
        if p is None: return None
45
        return p
46
29
47
    def _impl(self, cls):
30
    def _impl(self, cls):
48
        db = self.db
31
        db = self.db
49
        if db:
32
        if db:
50
            return db[cls.__mongometa__.name]
33
            return db[cls.__mongometa__.name]