def _init_caption_generator(request): """Initialze the caption generator by calculating the frequency table. Args: request (HttpRequest): request object. """ scraper = NewYorkerScraper() try: recent_winners = scraper.get_latest_winners() except RuntimeError: return None freq_table = get_freq_table(recent_winners) request.session['freq_table'] = freq_table return freq_table
def get_context_data(self, **kwargs): """Set the image location of the caption contest in the view.""" context = super(IndexView, self).get_context_data(**kwargs) # store the recent winning captions and image source in the session try: caption_image_src = self.request.session['caption_image_src'] except KeyError: scraper = NewYorkerScraper() caption_image_src = scraper.get_current_contest_image_src() self.request.session['caption_image_src'] = caption_image_src context['caption_image_src'] = caption_image_src return context