Switch to unified view

a/Allura/allura/controllers/attachments.py b/Allura/allura/controllers/attachments.py
1
from urllib import unquote
1
from urllib import unquote
2
from webob import exc
2
from webob import exc
3
3
4
from tg import expose, request, response, redirect
4
from tg import expose, request, response, redirect
5
from ming.utils import LazyProperty
6
5
from allura.lib.security import require, has_artifact_access
7
from allura.lib.security import require, has_artifact_access
6
from allura.controllers import BaseController
8
from .base import BaseController
7
9
8
class AttachmentsController(BaseController):
10
class AttachmentsController(BaseController):
9
    AttachmentControllerClass = None
11
    AttachmentControllerClass = None
10
12
11
    def __init__(self, artifact):
13
    def __init__(self, artifact):
...
...
15
    def _lookup(self, filename=None, *args):
17
    def _lookup(self, filename=None, *args):
16
        if filename:
18
        if filename:
17
            if not args:
19
            if not args:
18
                filename = request.path.rsplit('/', 1)[-1]
20
                filename = request.path.rsplit('/', 1)[-1]
19
            filename=unquote(filename)
21
            filename=unquote(filename)
20
            return self.AttachmentControllerClass(filename), args
22
            return self.AttachmentControllerClass(filename, self.artifact), args
21
        else:
23
        else:
22
            raise exc.HTTPNotFound
24
            raise exc.HTTPNotFound
23
25
24
class AttachmentController(BaseController):
26
class AttachmentController(BaseController):
25
    AttachmentClass = None
27
    AttachmentClass = None
26
    edit_perm = 'edit'
28
    edit_perm = 'edit'
27
29
28
    def _check_security(self):
30
    def _check_security(self):
29
        require(has_artifact_access('read', self.artifact))
31
        require(has_artifact_access('read', self.artifact))
30
32
31
    def __init__(self, filename):
33
    def __init__(self, filename, artifact):
32
        self.filename = filename
34
        self.filename = filename
33
        self.attachment = self.AttachmentClass.query.get(filename=filename)
34
        if self.attachment is None:
35
            self.attachment = self.AttachmentClass.by_metadata(filename=filename).first()
36
        if self.attachment is None:
37
            raise exc.HTTPNotFound()
38
        self.thumbnail = self.AttachmentClass.by_metadata(filename=filename).first()
39
        self.artifact = self.attachment.artifact
35
        self.artifact = artifact
40
36
37
    @LazyProperty
38
    def attachment(self):
39
        metadata = self.AttachmentClass.metadata_for(self.artifact)
40
        metadata_query = dict(
41
            ('metadata.%s' % k, v)
42
            for k, v in metadata.iteritems())
43
        attachment = self.AttachmentClass.query.get(filename=self.filename, **metadata_query)
44
        if attachment is None:
45
            attachment = self.AttachmentClass.by_metadata(filename=self.filename, **metadata).first()
46
        if attachment is None:
47
            raise exc.HTTPNotFound
48
        return attachment
49
50
    @LazyProperty
51
    def thumbnail(self):
52
        thumbnail = self.AttachmentClass.by_metadata(filename=self.attachment.filename).first()
53
        if thumbnail is None:
54
            raise exc.HTTPNotFound
55
        return thumbnail
56
        
41
    @expose()
57
    @expose()
42
    def index(self, delete=False, embed=True, **kw):
58
    def index(self, delete=False, embed=True, **kw):
43
        if request.method == 'POST':
59
        if request.method == 'POST':
44
            require(has_artifact_access(self.edit_perm, self.artifact))
60
            require(has_artifact_access(self.edit_perm, self.artifact))
45
            if delete:
61
            if delete:
46
                self.attachment.delete()
62
                self.attachment.delete()
63
                try:
47
                if self.thumbnail:
64
                    if self.thumbnail:
48
                    self.thumbnail.delete()
65
                        self.thumbnail.delete()
66
                except exc.HTTPNotFound:
67
                    pass
49
            redirect(request.referer)
68
            redirect(request.referer)
50
        with self.attachment.open() as fp:
69
        with self.attachment.open() as fp:
51
            if fp is None:
70
            if fp is None:
52
                raise exc.HTTPNotFound()
71
                raise exc.HTTPNotFound()
53
            filename = fp.metadata['filename'].encode('utf-8')
72
            filename = fp.metadata['filename'].encode('utf-8')