Beispiel #1
0
    def groups(cls, _db, title, url, lane, annotator,
               force_refresh=False, use_materialized_works=True):
        """The acquisition feed for 'featured' items from a given lane's
        sublanes, organized into per-lane groups.
        """
        # Find or create a CachedFeed.
        cached, usable = CachedFeed.fetch(
            _db,
            lane=lane,
            type=CachedFeed.GROUPS_TYPE,
            facets=None,
            pagination=None,
            annotator=annotator,
            force_refresh=force_refresh
        )
        if usable:
            return cached

        works_and_lanes = lane.sublane_samples(
            use_materialized_works=use_materialized_works
        )
        if not works_and_lanes:
            # We did not find enough works for a groups feed.
            # Instead we need to display a flat feed--the
            # contents of what would have been the 'all' feed.
            if not isinstance(lane, Lane):
                # This is probably a top-level controller or
                # application object.  Create a dummy lane that
                # contains everything.
                lane = Lane(_db, "Everything")
            # Generate a page-type feed that is filed as a
            # groups-type feed so it will show up when the client
            # asks for it.
            cached = cls.page(
                _db, title, url, lane, annotator,
                cache_type=CachedFeed.GROUPS_TYPE,
                force_refresh=force_refresh,
                use_materialized_works=use_materialized_works
            )
            return cached

        if lane.include_all_feed:
            # Create an 'all' group so that patrons can browse every
            # book in this lane.
            works = lane.featured_works(
                use_materialized_works=use_materialized_works
            )
            for work in works:
                works_and_lanes.append((work, None))

        all_works = []
        for work, sublane in works_and_lanes:
            if sublane is None:
                # This work is in the (e.g.) 'All Science Fiction'
                # group. Whether or not this lane has sublanes,
                # the group URI will point to a linear feed, not a
                # groups feed.
                v = dict(
                    lane=lane,
                    label='All ' + lane.display_name,
                    link_to_list_feed=True,
                )
            else:
                v = dict(
                    lane=sublane
                )
            annotator.lanes_by_work[work].append(v)
            all_works.append(work)

        feed = AcquisitionFeed(
            _db, title, url, all_works, annotator,
        )

        # Render a 'start' link and an 'up' link.
        top_level_title = annotator.top_level_title() or "Collection Home"
        AcquisitionFeed.add_link_to_feed(feed=feed.feed, href=annotator.default_lane_url(), rel="start", title=top_level_title)

        if isinstance(lane, Lane):
            visible_parent = lane.visible_parent()
            if isinstance(visible_parent, Lane):
                title = visible_parent.display_name
            else:
                title = top_level_title
            up_uri = annotator.groups_url(visible_parent)
            AcquisitionFeed.add_link_to_feed(feed=feed.feed, href=up_uri, rel="up", title=title)
            feed.add_breadcrumbs(lane, annotator)
        
        annotator.annotate_feed(feed, lane)

        content = unicode(feed)
        cached.update(content)
        return cached