~cjwatson/lazr.restful/range-factory

« back to all changes in this revision

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

  • Committer: Colin Watson
  • Date: 2015-04-09 19:53:24 UTC
  • Revision ID: cjwatson@canonical.com-20150409195324-z5rmzks7ofuouegn
Allow declaring a custom range factory for use with returned collections.

Show diffs side-by-side

added added

removed removed

Lines of Context:
613
613
        return simplejson.dumps(len(entries))
614
614
 
615
615
 
616
 
    def batch(self, entries, request):
 
616
    def batch(self, entries, request, range_factory=None):
617
617
        """Prepare a batch from a (possibly huge) list of entries.
618
618
 
619
619
        :return: a JSON string representing a hash:
637
637
        """
638
638
        if not hasattr(entries, '__len__'):
639
639
            entries = IFiniteSequence(entries)
640
 
        navigator = WebServiceBatchNavigator(entries, request)
 
640
        navigator = WebServiceBatchNavigator(
 
641
            entries, request, range_factory=range_factory)
641
642
 
642
643
        view_permission = getUtility(IWebServiceConfiguration).view_permission
643
644
        batch = { 'start' : navigator.batch.start }
1818
1819
            entries = self.collection.find()
1819
1820
        if request is None:
1820
1821
            request = self.request
1821
 
        result = super(CollectionResource, self).batch(entries, request)
 
1822
        if self.collection.range_factory_class is None:
 
1823
            range_factory = None
 
1824
        else:
 
1825
            range_factory = self.collection.range_factory_class(entries)
 
1826
        result = super(CollectionResource, self).batch(
 
1827
            entries, request, range_factory=range_factory)
1822
1828
        result += (
1823
1829
            ', "resource_type_link" : ' + simplejson.dumps(self.type_url)
1824
1830
            + '}')
2091
2097
    """A collection of entries."""
2092
2098
    implements(ICollection)
2093
2099
 
 
2100
    range_factory_class = None
 
2101
 
2094
2102
    def __init__(self, context, request):
2095
2103
        """Associate the entry with some database model object."""
2096
2104
        self.context = context
2102
2110
    implements(IScopedCollection)
2103
2111
    adapts(Interface, Interface, IWebServiceLayer)
2104
2112
 
 
2113
    range_factory_class = None
 
2114
 
2105
2115
    def __init__(self, context, collection, request):
2106
2116
        """Initialize the scoped collection.
2107
2117