Switch to unified view

a/Allura/allura/model/discuss.py b/Allura/allura/model/discuss.py
1
import re
2
import logging
1
import logging
3
2
4
import tg
3
import tg
5
import pymongo
4
import pymongo
6
from pylons import c, g, request
5
from pylons import c, g
7
from pymongo.bson import ObjectId
8
6
9
from ming import schema
7
from ming import schema
10
from ming.orm.base import mapper, session
8
from ming.orm.base import session
11
from ming.orm.mapped_class import MappedClass
12
from ming.orm.property import FieldProperty, RelationProperty, ForeignIdProperty
9
from ming.orm.property import FieldProperty, RelationProperty, ForeignIdProperty
13
10
14
from allura.lib import helpers as h
11
from allura.lib import helpers as h
15
from allura.lib.security import require, has_artifact_access
12
from allura.lib.security import require, has_artifact_access
16
from .auth import ProjectRole
17
from .artifact import Artifact, VersionedArtifact, Snapshot, Message, Feed, BaseAttachment
13
from .artifact import Artifact, VersionedArtifact, Snapshot, Message, Feed
14
from .attachments import BaseAttachment
18
from .types import ArtifactReference, ArtifactReferenceType
15
from .types import ArtifactReference, ArtifactReferenceType
19
16
20
log = logging.getLogger(__name__)
17
log = logging.getLogger(__name__)
21
18
22
common_suffix = tg.config.get('forgemail.domain', '.sourceforge.net')
19
common_suffix = tg.config.get('forgemail.domain', '.sourceforge.net')
...
...
54
    def post_class(cls):
51
    def post_class(cls):
55
        return cls.posts.related
52
        return cls.posts.related
56
53
57
    @classmethod
54
    @classmethod
58
    def attachment_class(cls):
55
    def attachment_class(cls):
59
        return Attachment
56
        return DiscussionAttachment
60
57
61
    def update_stats(self):
58
    def update_stats(self):
62
        self.num_topics = self.thread_class().query.find(
59
        self.num_topics = self.thread_class().query.find(
63
            dict(discussion_id=self._id)).count()
60
            dict(discussion_id=self._id)).count()
64
        self.num_posts = self.post_class().query.find(
61
        self.num_posts = self.post_class().query.find(
...
...
134
    def post_class(cls):
131
    def post_class(cls):
135
        return cls.posts.related
132
        return cls.posts.related
136
133
137
    @classmethod
134
    @classmethod
138
    def attachment_class(cls):
135
    def attachment_class(cls):
139
        return Attachment
136
        return DiscussionAttachment
140
137
141
    @property
138
    @property
142
    def artifact(self):
139
    def artifact(self):
143
        aref = ArtifactReference(self.artifact_reference)
140
        aref = ArtifactReference(self.artifact_reference)
144
        if aref is None: return self.discussion
141
        if aref is None: return self.discussion
...
...
342
    def thread_class(cls):
339
    def thread_class(cls):
343
        return cls.thread.related
340
        return cls.thread.related
344
341
345
    @classmethod
342
    @classmethod
346
    def attachment_class(cls):
343
    def attachment_class(cls):
347
        return Attachment
344
        return DiscussionAttachment
348
345
349
    @property
346
    @property
350
    def parent(self):
347
    def parent(self):
351
        return self.query.get(_id=self.parent_id)
348
        return self.query.get(_id=self.parent_id)
352
349
...
...
354
    def subject(self):
351
    def subject(self):
355
        return self.thread.subject
352
        return self.thread.subject
356
353
357
    @property
354
    @property
358
    def attachments(self):
355
    def attachments(self):
359
        return self.attachment_class().by_metadata(post_id=self._id)
356
        return self.attachment_class().by_metadata(
357
            post_id=self._id, type='attachment')
360
358
361
    def primary(self, primary_class=None):
359
    def primary(self, primary_class=None):
362
        return self.thread.primary(primary_class)
360
        return self.thread.primary(primary_class)
363
361
364
    def summary(self):
362
    def summary(self):
...
...
412
    def spam(self):
410
    def spam(self):
413
        self.status = 'spam'
411
        self.status = 'spam'
414
        g.publish('react', 'spam', dict(artifact_reference=self.dump_ref()),
412
        g.publish('react', 'spam', dict(artifact_reference=self.dump_ref()),
415
                  serializer='pickle')
413
                  serializer='pickle')
416
414
417
class Attachment(BaseAttachment):
415
class DiscussionAttachment(BaseAttachment):
418
    DiscussionClass=Discussion
416
    DiscussionClass=Discussion
419
    ThreadClass=Thread
417
    ThreadClass=Thread
420
    PostClass=Post
418
    PostClass=Post
421
    class __mongometa__:
419
    class __mongometa__:
422
        name = 'attachment.files'
420
        name = 'attachment.files'
...
...
429
    # Override the metadata schema here
427
    # Override the metadata schema here
430
    metadata=FieldProperty(dict(
428
    metadata=FieldProperty(dict(
431
            discussion_id=schema.ObjectId,
429
            discussion_id=schema.ObjectId,
432
            thread_id=str,
430
            thread_id=str,
433
            post_id=str,
431
            post_id=str,
434
            filename=str))
432
            filename=str,
433
            app_config_id=schema.ObjectId,
434
            type=str))
435
436
    @property
437
    def artifact(self):
438
        return self.post
435
439
436
    @property
440
    @property
437
    def discussion(self):
441
    def discussion(self):
438
        return self.DiscussionClass.query.get(_id=self.metadata.discussion_id)
442
        return self.DiscussionClass.query.get(_id=self.metadata.discussion_id)
439
443
...
...
443
447
444
    @property
448
    @property
445
    def post(self):
449
    def post(self):
446
        return self.PostClass.query.get(_id=self.metadata.post_id)
450
        return self.PostClass.query.get(_id=self.metadata.post_id)
447
451
452
    @classmethod
453
    def metadata_for(cls, post):
454
        return dict(
455
            post_id=post._id,
456
            thread_id=post.thread_id,
457
            discussion_id=post.discussion_id,
458
            app_config_id=post.app_config_id)
459
448
    def url(self):
460
    def url(self):
461
        if self.metadata.post_id:
462
            return self.post.url() + 'attachment/' + self.filename
463
        elif self.metadata.thread_id:
464
            return self.thread.url() + 'attachment/' + self.filename
465
        else:
449
        return self.discussion.url() + 'attachment/' + self.filename
466
            return self.discussion.url() + 'attachment/' + self.filename
450
467
451
468