~cjwatson/lazr.restful/test-fixes

« back to all changes in this revision

Viewing changes to src/lazr/restful/_resource.py

  • Committer: Colin Watson
  • Date: 2016-02-17 01:07:21 UTC
  • Revision ID: cjwatson@canonical.com-20160217010721-4d5fqqml5q97ss59
Switch zope.interface, zope.component, and lazr.delegates users from class advice to class decorators.

Show diffs side-by-side

added added

removed removed

Lines of Context:
47
47
     sha_constructor = sha.new
48
48
 
49
49
from zope.app.pagetemplate.engine import TrustedAppPT
50
 
from zope import component
51
50
from zope.component import (
52
 
    adapts,
 
51
    adapter,
53
52
    getAdapters,
54
53
    getAllUtilitiesRegisteredFor,
55
54
    getGlobalSiteManager,
64
63
from zope.interface import (
65
64
    alsoProvides,
66
65
    implementer,
67
 
    implements,
68
66
    implementedBy,
69
67
    providedBy,
70
68
    Interface,
173
171
    return cgi.escape(unicode(value))
174
172
 
175
173
 
176
 
@component.adapter(Interface, IField, IWebServiceClientRequest)
 
174
@adapter(Interface, IField, IWebServiceClientRequest)
177
175
@implementer(IFieldHTMLRenderer)
178
176
def render_field_to_html(object, field, request):
179
177
    """Turn a field's current value into an XHTML snippet.
236
234
        return IJSONPublishable(obj).toDataForJSON(self.media_type)
237
235
 
238
236
 
 
237
@implementer(IJSONPublishable)
 
238
@adapter(BaseItem)
239
239
class JSONItem:
240
240
    """JSONPublishable adapter for lazr.enum."""
241
 
    implements(IJSONPublishable)
242
 
    adapts(BaseItem)
243
241
 
244
242
    def __init__(self, context):
245
243
        self.context = context
249
247
        return str(self.context.title)
250
248
 
251
249
 
 
250
@implementer(IHTTPResource)
252
251
class RedirectResource:
253
252
    """A resource that redirects to another URL."""
254
 
    implements(IHTTPResource)
255
253
 
256
254
    def __init__(self, url, request):
257
255
        self.url = url
263
261
        self.request.response.setHeader("Location", url)
264
262
 
265
263
 
 
264
@implementer(IHTTPResource)
266
265
class HTTPResource:
267
266
    """See `IHTTPResource`."""
268
 
    implements(IHTTPResource)
269
267
 
270
268
    # Some interesting media types.
271
269
    WADL_TYPE = 'application/vnd.sun.wadl+xml'
1262
1260
        pass
1263
1261
 
1264
1262
 
 
1263
@implementer(IEntryFieldResource, IJSONPublishable)
1265
1264
class EntryFieldResource(FieldUnmarshallerMixin, EntryManipulatingResource):
1266
1265
    """An individual field of an entry."""
1267
 
    implements(IEntryFieldResource, IJSONPublishable)
1268
1266
 
1269
1267
    SUPPORTED_CONTENT_TYPES = [HTTPResource.JSON_TYPE,
1270
1268
                               HTTPResource.XHTML_TYPE]
1326
1324
                    % media_type)
1327
1325
 
1328
1326
 
 
1327
@implementer(IEntryField, ILocation)
1329
1328
class EntryField:
1330
1329
    """A schema field bound by name to a particular entry."""
1331
 
    implements(IEntryField, ILocation)
1332
1330
 
1333
1331
    def __init__(self, entry, field, name):
1334
1332
        """Initialize with respect to a named field of an entry."""
1341
1339
        self.__name__ = self.name
1342
1340
 
1343
1341
 
 
1342
@adapter(EntryField, IHTTPRequest)
 
1343
@implementer(IAbsoluteURL)
1344
1344
class EntryFieldURL(AbsoluteURL):
1345
1345
    """An IAbsoluteURL adapter for EntryField objects."""
1346
 
    component.adapts(EntryField, IHTTPRequest)
1347
 
    implements(IAbsoluteURL)
1348
1346
 
1349
1347
    def __init__(self, entryfield, request):
1350
1348
        self.context = entryfield
1374
1372
 
1375
1373
 
1376
1374
 
 
1375
@implementer(IEntryResource, IJSONPublishable)
1377
1376
class EntryResource(CustomOperationResourceMixin,
1378
1377
                    FieldUnmarshallerMixin, EntryManipulatingResource):
1379
1378
    """An individual object, published to the web."""
1380
 
    implements(IEntryResource, IJSONPublishable)
1381
1379
 
1382
1380
    SUPPORTED_CONTENT_TYPES = [HTTPResource.WADL_TYPE,
1383
1381
                               HTTPResource.DEPRECATED_WADL_TYPE,
1765
1763
        return None
1766
1764
 
1767
1765
 
 
1766
@implementer(ICollectionResource)
1768
1767
class CollectionResource(BatchingResourceMixin,
1769
1768
                         CustomOperationResourceMixin,
1770
1769
                         ReadOnlyResource):
1771
1770
    """A resource that serves a list of entry resources."""
1772
 
    implements(ICollectionResource)
1773
1771
 
1774
1772
    def __init__(self, context, request):
1775
1773
        """Associate this resource with a specific object and request."""
1843
1841
            return adapter.collection_type_link
1844
1842
 
1845
1843
 
 
1844
@implementer(IServiceRootResource, IJSONPublishable)
1846
1845
class ServiceRootResource(HTTPResource):
1847
1846
    """A resource that responds to GET by describing the service."""
1848
 
    implements(IServiceRootResource, IJSONPublishable)
1849
1847
 
1850
1848
    # A preparsed template file for WADL representations of the root.
1851
1849
    WADL_TEMPLATE = LazrPageTemplateFile('templates/wadl-root.pt')
2077
2075
            adapter.singular_type)
2078
2076
 
2079
2077
 
 
2078
@implementer(IEntry)
2080
2079
class Entry:
2081
2080
    """An individual entry."""
2082
 
    implements(IEntry)
2083
2081
 
2084
2082
    def __init__(self, context, request):
2085
2083
        """Associate the entry with some database model object."""
2087
2085
        self.request = request
2088
2086
 
2089
2087
 
 
2088
@implementer(ICollection)
2090
2089
class Collection:
2091
2090
    """A collection of entries."""
2092
 
    implements(ICollection)
2093
2091
 
2094
2092
    def __init__(self, context, request):
2095
2093
        """Associate the entry with some database model object."""
2097
2095
        self.request = request
2098
2096
 
2099
2097
 
 
2098
@implementer(IScopedCollection)
 
2099
@adapter(Interface, Interface, IWebServiceLayer)
2100
2100
class ScopedCollection:
2101
2101
    """A collection associated with some parent object."""
2102
 
    implements(IScopedCollection)
2103
 
    adapts(Interface, Interface, IWebServiceLayer)
2104
2102
 
2105
2103
    def __init__(self, context, collection, request):
2106
2104
        """Initialize the scoped collection.