Switch to unified view

a/Allura/allura/model/index.py b/Allura/allura/model/index.py
1
import re
1
import re
2
import logging
2
import logging
3
from itertools import groupby
3
from itertools import groupby
4
from cPickle import dumps, loads
4
from cPickle import dumps, loads
5
from datetime import datetime
6
from collections import defaultdict
5
from collections import defaultdict
7
6
8
import bson
7
import bson
9
import pymongo
8
import pymongo
10
from pylons import c, g
9
from pylons import c
11
10
12
import ming
11
from ming import collection, Field, Index
13
from ming import schema as S
12
from ming import schema as S
14
from ming.utils import LazyProperty
13
from ming.utils import LazyProperty
15
from ming.orm import session
14
from ming.orm import session, mapper
16
from ming.orm import FieldProperty, ForeignIdProperty, RelationProperty
15
from ming.orm import ForeignIdProperty, RelationProperty
17
from ming.orm.declarative import MappedClass
18
16
19
from allura.lib import helpers as h
17
from allura.lib import helpers as h
20
18
21
from .session import main_orm_session
19
from .session import main_doc_session, main_orm_session
22
20
23
log = logging.getLogger(__name__)
21
log = logging.getLogger(__name__)
24
22
25
class ArtifactReference(MappedClass):
23
# Collection definitions
26
    '''ArtifactReference manages the artifact graph.
24
ArtifactReferenceDoc = collection(
27
25
    'artifact_reference', main_doc_session,
28
    fields are all strs, corresponding to Solr index_ids
26
    Field('_id', str),
29
    '''
27
    Field('artifact_reference', dict(
30
    class __mongometa__:
31
        session = main_orm_session
32
        name = 'artifact_reference'
33
        indexes = [ 'references' ]
34
35
    _id = FieldProperty(str)
36
    artifact_reference = FieldProperty(S.Object(dict(
37
            cls=S.Binary,
28
            cls=S.Binary(),
38
            project_id=S.ObjectId,
29
            project_id=S.ObjectId(),
39
            app_config_id=S.ObjectId,
30
            app_config_id=S.ObjectId(),
40
            artifact_id=S.Anything(if_missing=None))))
31
            artifact_id=S.Anything(if_missing=None))),
41
    references = FieldProperty([str])
32
    Field('references', [str], index=True))
33
34
ShortlinkDoc = collection(
35
    'shortlink', main_doc_session,
36
    Field('_id', S.ObjectId()),
37
    Field('ref_id', str, index=True),
38
    Field('project_id', S.ObjectId()),
39
    Field('app_config_id', S.ObjectId()),
40
    Field('link', str),
41
    Field('url', str),
42
    Index('link, project_id', 'app_config_id'))
43
44
# Class definitions
45
class ArtifactReference(object):
42
46
43
    @classmethod
47
    @classmethod
44
    def from_artifact(cls, artifact):
48
    def from_artifact(cls, artifact):
45
        '''Upsert logic to generate an ArtifactReference object from an artifact'''
49
        '''Upsert logic to generate an ArtifactReference object from an artifact'''
46
        obj = cls.query.get(_id=artifact.index_id())
50
        obj = cls.query.get(_id=artifact.index_id())
...
...
69
                return cls.query.get(_id=aref.artifact_id)
73
                return cls.query.get(_id=aref.artifact_id)
70
        except:
74
        except:
71
            log.exception('Error loading artifact for %s: %r',
75
            log.exception('Error loading artifact for %s: %r',
72
                          self._id, aref)
76
                          self._id, aref)
73
77
74
class Shortlink(MappedClass):
78
class Shortlink(object):
75
    '''Collection mapping shorthand_ids for artifacts to ArtifactReferences'''
79
    '''Collection mapping shorthand_ids for artifacts to ArtifactReferences'''
76
    class __mongometa__:
77
        session = main_orm_session
78
        name = 'shortlink'
79
        indexes = [
80
            ('link', 'project_id', 'app_config_id'),
81
            ('ref_id',),
82
            ]
83
84
    # Stored properties
85
    _id = FieldProperty(S.ObjectId)
86
    ref_id = ForeignIdProperty(ArtifactReference)
87
    project_id = ForeignIdProperty('Project')
88
    app_config_id = ForeignIdProperty('AppConfig')
89
    link = FieldProperty(str)
90
    url = FieldProperty(str)
91
92
    # Relation Properties
93
    project = RelationProperty('Project')
94
    app_config = RelationProperty('AppConfig')
95
    ref = RelationProperty('ArtifactReference')
96
80
97
    # Regexes used to find shortlinks
81
    # Regexes used to find shortlinks
98
    _core_re = r'''(\[
82
    _core_re = r'''(\[
99
            (?:(?P<project_id>.*?):)?      # optional project ID
83
            (?:(?P<project_id>.*?):)?      # optional project ID
100
            (?:(?P<app_id>.*?):)?      # optional tool ID
84
            (?:(?P<app_id>.*?):)?      # optional tool ID
...
...
103
    re_link_1 = re.compile(r'\s' + _core_re, re.VERBOSE)
87
    re_link_1 = re.compile(r'\s' + _core_re, re.VERBOSE)
104
    re_link_2 = re.compile(r'^' +  _core_re, re.VERBOSE)
88
    re_link_2 = re.compile(r'^' +  _core_re, re.VERBOSE)
105
89
106
    def __repr__(self):
90
    def __repr__(self):
107
        with h.push_context(self.project_id):
91
        with h.push_context(self.project_id):
92
            if self.app_config:
108
            return '[%s:%s:%s] -> %s' % (
93
                return '[%s:%s:%s] -> %s' % (
109
                self.project.shortname,
94
                    self.project.shortname,
110
                self.app_config.options.mount_point,
95
                    self.app_config.options.mount_point,
111
                self.link,
96
                    self.link,
112
                self.ref_id)
97
                    self.ref_id)
98
            else:
99
                return '[%s:*:%s] -> %s' % (
100
                    self.project.shortname,
101
                    self.link,
102
                    self.ref_id)
113
103
114
    @classmethod
104
    @classmethod
115
    def lookup(cls, link):
105
    def lookup(cls, link):
116
        return cls.from_links(link)[link]
106
        return cls.from_links(link)[link]
117
107
...
...
200
                app=None,
190
                app=None,
201
                artifact=parts[0])
191
                artifact=parts[0])
202
        else:
192
        else:
203
            return None
193
            return None
204
194
195
# Mapper definitions
196
mapper(ArtifactReference, ArtifactReferenceDoc, main_orm_session)
197
mapper(Shortlink, ShortlinkDoc, main_orm_session, properties=dict(
198
    ref_id = ForeignIdProperty(ArtifactReference),
199
    project_id = ForeignIdProperty('Project'),
200
    app_config_id = ForeignIdProperty('AppConfig'),
201
    project = RelationProperty('Project'),
202
    app_config = RelationProperty('AppConfig'),
203
    ref = RelationProperty(ArtifactReference)))