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(
25
    'artifact_reference', main_doc_session,
26
    Field('_id', str),
27
    Field('artifact_reference', dict(
28
            cls=S.Binary(),
29
            project_id=S.ObjectId(),
30
            app_config_id=S.ObjectId(),
31
            artifact_id=S.Anything(if_missing=None))),
32
    Field('references', [str], index=True))
27
33
28
    fields are all strs, corresponding to Solr index_ids
34
ShortlinkDoc = collection(
29
    '''
35
    'shortlink', main_doc_session,
30
    class __mongometa__:
36
    Field('_id', S.ObjectId()),
31
        session = main_orm_session
37
    Field('ref_id', str, index=True),
32
        name = 'artifact_reference'
38
    Field('project_id', S.ObjectId()),
33
        indexes = [ 'references' ]
39
    Field('app_config_id', S.ObjectId()),
40
    Field('link', str),
41
    Field('url', str),
42
    Index('link, project_id', 'app_config_id'))
34
43
35
    _id = FieldProperty(str)
44
# Class definitions
36
    artifact_reference = FieldProperty(S.Object(dict(
45
class ArtifactReference(object):
37
            cls=S.Binary,
38
            project_id=S.ObjectId,
39
            app_config_id=S.ObjectId,
40
            artifact_id=S.Anything(if_missing=None))))
41
    references = FieldProperty([str])
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
...
...
200
                app=None,
184
                app=None,
201
                artifact=parts[0])
185
                artifact=parts[0])
202
        else:
186
        else:
203
            return None
187
            return None
204
188
189
# Mapper definitions
190
mapper(ArtifactReference, ArtifactReferenceDoc, main_orm_session)
191
mapper(Shortlink, ShortlinkDoc, main_orm_session, properties=dict(
192
    ref_id = ForeignIdProperty(ArtifactReference),
193
    project_id = ForeignIdProperty('Project'),
194
    app_config_id = ForeignIdProperty('AppConfig'),
195
    project = RelationProperty('Project'),
196
    app_config = RelationProperty('AppConfig'),
197
    ref = RelationProperty(ArtifactReference)))