def get(self, version_id):
        self.check_version_id(version_id)

        cache_key = version_id + str(self.__class__)
        page = self.cache.get(cache_key)

        if self.cache_bust or not page:
            logging.debug('Cache miss with key: %s' % cache_key)
            retrieved_data = EmailTemplate.fetch_all(self.data_sources[version_id])
            trail_blocks = deduplication.build_unique_trailblocks(retrieved_data,
                self.priority_list[version_id],
                excluded=self.exclude_from_deduplication())
            today = datetime.datetime.now()
            date = today.strftime('%A %d %b %Y')

            template_name = self.template_names[version_id] + '.html'
            template = self.resolve_template(template_name)

            ads = {}

            if hasattr(self, 'ad_tag') and self.ad_tag:
                ad_fetcher = AdFetcher(self.ad_tag)
                for name, type in self.ad_config.iteritems():
                    ads[name] = ad_fetcher.fetch_type(type)

            page = template.render(ads=ads, date=date, data=self.additional_template_data(), **trail_blocks)
            self.cache.add(cache_key, page, 300)
        else:
            logging.debug('Cache hit with key: %s' % cache_key)

        self.response.out.write(page)
示例#2
0
    def get(self, path="film"):
        logging.info(path)

        path_mapping = immutable.make_dict({
            'film': container.for_id('6d84cd8d-d159-4e9a-ba2f-8852528d2d03'),
            'uk/opinion/v1': container.for_id('uk/commentisfree/regular-stories'),
            'film/v1': ds.FilmTodayLatestDataSource(mr.client),
        })

        if not path in path_mapping.keys():
            webapp2.abort(404, "Path {0} not mapped to a datasource".format(path))
            return

        stories_data_source = path_mapping[path]

        data_sources = {'stories': stories_data_source}
        priority_list = [('stories', 1)]
        template_data = {}
        retrieved_data = handlers.EmailTemplate.fetch_all(data_sources)
        trail_block = deduplication.build_unique_trailblocks(retrieved_data,priority_list)
        stories = trail_block.get('stories')

        headlines = [read_headline(s) for s in stories]
        if headlines:
            headline = headlines[0]
            template_data['headline'] = headline

        template = handlers.jinja_environment.get_template('headline.html')
        self.response.out.write(template.render(template_data))
    def get(self, path="film"):
        logging.info(path)

        path_mapping = immutable.make_dict({
            'film':
            container.for_id('6d84cd8d-d159-4e9a-ba2f-8852528d2d03'),
            'uk/opinion/v1':
            container.for_id('uk/commentisfree/regular-stories'),
            'film/v1':
            ds.FilmTodayLatestDataSource(mr.client),
        })

        if not path in path_mapping.keys():
            webapp2.abort(404,
                          "Path {0} not mapped to a datasource".format(path))
            return

        stories_data_source = path_mapping[path]

        data_sources = {'stories': stories_data_source}
        priority_list = [('stories', 1)]
        template_data = {}
        retrieved_data = handlers.EmailTemplate.fetch_all(data_sources)
        trail_block = deduplication.build_unique_trailblocks(
            retrieved_data, priority_list)
        stories = trail_block.get('stories')

        headlines = [read_headline(s) for s in stories]
        if headlines:
            headline = headlines[0]
            template_data['headline'] = headline

        template = handlers.jinja_environment.get_template('headline.html')
        self.response.out.write(template.render(template_data))
示例#4
0
    def get(self, edition="uk"):

        data_sources = {'top_stories': dss.headlines.for_edition(edition)}
        priority_list = [('top_stories', 1)]
        template_data = {}
        retrieved_data = handlers.EmailTemplate.fetch_all(data_sources)
        trail_block = deduplication.build_unique_trailblocks(retrieved_data,priority_list)
        stories = trail_block.get('top_stories')
        headlines = [s.get('webTitle') for s in stories]
        if headlines:
            headline = headlines[0]
            template_data['headline'] = headline
        template = handlers.jinja_environment.get_template('headline.html')
        self.response.out.write(template.render(template_data))
    def get(self, edition="uk"):

        data_sources = {'top_stories': dss.headlines.for_edition(edition)}
        priority_list = [('top_stories', 1)]
        template_data = {}
        retrieved_data = handlers.EmailTemplate.fetch_all(data_sources)
        trail_block = deduplication.build_unique_trailblocks(
            retrieved_data, priority_list)
        stories = trail_block.get('top_stories')
        headlines = [s.get('webTitle') for s in stories]
        if headlines:
            headline = headlines[0]
            template_data['headline'] = headline
        template = handlers.jinja_environment.get_template('headline.html')
        self.response.out.write(template.render(template_data))
    def test_should_be_able_to_dedupe_result_sets(self):
        size = 3
        data = {
            'sport': [{'id': 6}, {'id': 7}, {'id': 8}, {'id': 9}, {'id': 10}, {'id': 11}],
            'chess': [{'id': 1}, {'id': 2}, {'id': 3}, {'id': 4}],
            'news': [{'id': 3}, {'id': 4}, {'id': 5}, {'id': 6}, {'id': 7}, {'id': 8}],
            'travel': [{'id': 9}, {'id': 10}, {'id': 11}, {'id': 12}]}

        priority_list = [('chess', 2), ('news', 4), ('sport', 1), ('travel', 3)]

        deduped_data = build_unique_trailblocks(data, priority_list)

        self.assertEquals(deduped_data['chess'], [{'id': 1}, {'id': 2}])
        self.assertEquals(deduped_data['news'], [{'id': 3}, {'id': 4}, {'id': 5}, {'id': 6}])
        self.assertEquals(deduped_data['sport'], [{'id': 7}])
        self.assertEquals(deduped_data['travel'], [{'id': 9}, {'id': 10}, {'id': 11}])
    def test_we_never_show_dupes_even_if_we_run_out_of_items(self):
        size = 4
        data = {
            'sport': [{'id': 6}, {'id': 7}, {'id': 8}, {'id': 9}, {'id': 10}, {'id': 11}],
            'chess': [{'id': 1}, {'id': 2}, {'id': 3}, {'id': 4}],
            'news': [{'id': 3}, {'id': 4}, {'id': 5}, {'id': 6}, {'id': 7}, {'id': 8}],
            'travel': [{'id': 9}, {'id': 10}, {'id': 11}, {'id': 12}]}

        priority_list = [('chess', size), ('news', size), ('sport', size), ('travel', size)]

        deduped_data = build_unique_trailblocks(data, priority_list)

        self.assertEquals(deduped_data['chess'], [{'id': 1}, {'id': 2}, {'id': 3}, {'id': 4}])
        self.assertEquals(deduped_data['news'], [{'id': 5}, {'id': 6}, {'id': 7}, {'id': 8}])
        self.assertEquals(deduped_data['sport'], [{'id': 9}, {'id': 10}, {'id': 11}])
        self.assertEquals(deduped_data['travel'], [{'id': 12}])
示例#8
0
    def get(self, version_id):
        self.check_version_id(version_id)

        cache_key = version_id + str(self.__class__)
        page = self.cache.get(cache_key)

        if self.cache_bust or not page:
            logging.debug('Cache miss with key: %s' % cache_key)
            retrieved_data = EmailTemplate.fetch_all(
                self.data_sources[version_id])
            title_overrides = EmailTemplate.fetch_all_title_overrides(
                self.data_sources[version_id])
            trail_blocks = deduplication.build_unique_trailblocks(
                retrieved_data,
                self.priority_list[version_id],
                excluded=self.exclude_from_deduplication())
            today = datetime.datetime.now()
            date = today.strftime('%A %d %b %Y')

            template_name = self.template_names[version_id] + '.html'
            template = self.resolve_template(template_name)

            ads = {}

            page = template.render(ads=ads,
                                   date=date,
                                   data=self.additional_template_data(),
                                   title_overrides=title_overrides,
                                   **trail_blocks)

            if self.minify:
                page = htmlmin.minify(page)

            self.cache.add(cache_key, page, 300)
        else:
            logging.debug('Cache hit with key: %s' % cache_key)

        self.response.out.write(page)