def featured_works(self, use_materialized_works=True): if not use_materialized_works: qu = self.works() else: qu = self.materialized_works() # Aliasing Edition here allows this query to function # regardless of work_model and existing joins. work_edition = aliased(Edition) qu = qu.join(work_edition).order_by(work_edition.series_position, work_edition.title) target_size = Configuration.featured_lane_size() qu = qu.limit(target_size) return qu.all()
def randomized_sample_works(self, query, use_min_size=False): """Find a random sample of works for a feed""" offset = 0 target_size = Configuration.featured_lane_size() smallest_sample_size = target_size if use_min_size: smallest_sample_size = self.MINIMUM_SAMPLE_SIZE or (target_size - 5) total_size = query.count() if total_size < smallest_sample_size: # There aren't enough works here. Ignore the lane. return [] if total_size > target_size: # We have enough results to randomly offset the selection. offset = random.randint(0, total_size - target_size) works = query.offset(offset).limit(target_size).all() random.shuffle(works) return works