~launchpad-pqm/launchpad/devel

8687.15.18 by Karl Fogel
Add the copyright header block to files under lib/canonical/.
1
# Copyright 2009 Canonical Ltd.  This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
5863.19.45 by Leonard Richardson
Implemented traversal and a resource type for byte-storage resources.
3
4
"""Base classes for HTTP resources."""
5
6
__metaclass__ = type
7
__all__ = [
5863.19.47 by Leonard Richardson
Refactored out some code that can move into LAZR.
8
    'LibraryBackedByteStorage',
5863.19.45 by Leonard Richardson
Implemented traversal and a resource type for byte-storage resources.
9
]
10
5863.19.46 by Leonard Richardson
Got a rudimentary GET, PUT, and DELETE working.
11
12
from cStringIO import StringIO
13
11403.1.4 by Henning Eggers
Reformatted imports using format-imports script r32.
14
from lazr.restful.interfaces import IByteStorage
12113.1.1 by Abel Deuring
Back out hack to serve restricted files directly from the internal restricted librarian port; LibraryBackedByteStorage.alias_url returns a URL with an access token for restricted files.
15
from zope.component import (
16
    getMultiAdapter,
17
    getUtility,
18
    )
5863.19.48 by Leonard Richardson
Moved code with no Launchpad dependency into LAZR.
19
from zope.interface import implements
5863.19.46 by Leonard Richardson
Got a rudimentary GET, PUT, and DELETE working.
20
14600.2.2 by Curtis Hovey
Moved webapp to lp.services.
21
from lp.services.webapp.interfaces import ICanonicalUrlData
14578.2.1 by William Grant
Move librarian stuff from canonical.launchpad to lp.services.librarian. canonical.librarian remains untouched.
22
from lp.services.librarian.interfaces import (
12113.1.1 by Abel Deuring
Back out hack to serve restricted files directly from the internal restricted librarian port; LibraryBackedByteStorage.alias_url returns a URL with an access token for restricted files.
23
    ILibraryFileAliasSet,
24
    ILibraryFileAliasWithParent,
25
    )
5863.19.46 by Leonard Richardson
Got a rudimentary GET, PUT, and DELETE working.
26
27
5863.19.47 by Leonard Richardson
Refactored out some code that can move into LAZR.
28
class LibraryBackedByteStorage:
5863.19.61 by Leonard Richardson
Response to feedback.
29
    """See `IByteStorage`."""
5863.25.4 by Leonard Richardson
Merge from parent. Added WADL test implementation.
30
    implements(IByteStorage, ICanonicalUrlData)
5863.19.47 by Leonard Richardson
Refactored out some code that can move into LAZR.
31
32
    def __init__(self, entry, field):
5863.19.53 by Leonard Richardson
Added a doctest and refactored code to get rid of hacks.
33
        """Initialize as the backing storage for one entry's field."""
5863.19.47 by Leonard Richardson
Refactored out some code that can move into LAZR.
34
        self.entry = entry
35
        self.field = field
7031.2.1 by Leonard Richardson
Moved into pristine branch.
36
        self.file_alias = getattr(self.entry, self.field.__name__)
5863.19.47 by Leonard Richardson
Refactored out some code that can move into LAZR.
37
38
    @property
5863.25.4 by Leonard Richardson
Merge from parent. Added WADL test implementation.
39
    def rootsite(self):
5863.25.8 by Leonard Richardson
Respond to feedback.
40
        """See `ICanonicalUrlData`"""
5863.25.4 by Leonard Richardson
Merge from parent. Added WADL test implementation.
41
        return None
42
43
    @property
44
    def inside(self):
5863.25.8 by Leonard Richardson
Respond to feedback.
45
        """See `ICanonicalUrlData`"""
5863.25.4 by Leonard Richardson
Merge from parent. Added WADL test implementation.
46
        return self.entry.context
47
48
    @property
49
    def path(self):
5863.25.8 by Leonard Richardson
Respond to feedback.
50
        """See `ICanonicalUrlData`"""
7031.2.1 by Leonard Richardson
Moved into pristine branch.
51
        return self.field.__name__
5863.25.4 by Leonard Richardson
Merge from parent. Added WADL test implementation.
52
53
    @property
5863.19.53 by Leonard Richardson
Added a doctest and refactored code to get rid of hacks.
54
    def alias_url(self):
5863.19.61 by Leonard Richardson
Response to feedback.
55
        """See `IByteStorage`."""
12113.1.1 by Abel Deuring
Back out hack to serve restricted files directly from the internal restricted librarian port; LibraryBackedByteStorage.alias_url returns a URL with an access token for restricted files.
56
        if self.file_alias.restricted:
57
            lfa_with_parent = getMultiAdapter(
58
                (self.file_alias, self.entry.context),
59
                ILibraryFileAliasWithParent)
60
            token = lfa_with_parent.createToken()
61
            return self.file_alias.private_url + '?token=%s' % token
5863.19.53 by Leonard Richardson
Added a doctest and refactored code to get rid of hacks.
62
        return self.file_alias.getURL()
5863.19.47 by Leonard Richardson
Refactored out some code that can move into LAZR.
63
64
    @property
65
    def filename(self):
5863.19.61 by Leonard Richardson
Response to feedback.
66
        """See `IByteStorage`."""
7031.2.1 by Leonard Richardson
Moved into pristine branch.
67
        if self.is_stored:
68
            return self.file_alias.filename
5863.19.47 by Leonard Richardson
Refactored out some code that can move into LAZR.
69
        return self.field.__name__
70
5863.19.53 by Leonard Richardson
Added a doctest and refactored code to get rid of hacks.
71
    @property
72
    def is_stored(self):
5863.19.61 by Leonard Richardson
Response to feedback.
73
        """See `IByteStorage`."""
5863.19.53 by Leonard Richardson
Added a doctest and refactored code to get rid of hacks.
74
        return self.file_alias is not None
75
7031.2.1 by Leonard Richardson
Moved into pristine branch.
76
    def createStored(self, mediaType, representation, filename=None):
5863.19.61 by Leonard Richardson
Response to feedback.
77
        """See `IByteStorage`."""
7031.2.1 by Leonard Richardson
Moved into pristine branch.
78
        if filename is None:
79
            filename = self.filename
5863.19.53 by Leonard Richardson
Added a doctest and refactored code to get rid of hacks.
80
        stored = getUtility(ILibraryFileAliasSet).create(
7031.2.1 by Leonard Richardson
Moved into pristine branch.
81
            name=filename, size=len(representation),
5863.19.61 by Leonard Richardson
Response to feedback.
82
            file=StringIO(representation), contentType=mediaType)
7031.2.1 by Leonard Richardson
Moved into pristine branch.
83
        setattr(self.entry, self.field.__name__, stored)
5863.19.47 by Leonard Richardson
Refactored out some code that can move into LAZR.
84
5863.19.53 by Leonard Richardson
Added a doctest and refactored code to get rid of hacks.
85
    def deleteStored(self):
5863.19.61 by Leonard Richardson
Response to feedback.
86
        """See `IByteStorage`."""
7031.2.1 by Leonard Richardson
Moved into pristine branch.
87
        setattr(self.entry, self.field.__name__, None)