def parse(self, widget_options, site, preview_context=False): format = widget_options.get('format', 'fullwidth_single') number_of_ads = 1 ad_columns = 12 count_view = not preview_context if format == 'fullwidth_triple': number_of_ads = 3 ad_columns = 4 context = { 'format': format, 'ad_columns': ad_columns, 'count_view': count_view, 'number_of_ads': number_of_ads, } try: ad_placements = AdPlacement.get_active_ads( site, format=format, count_view=count_view, number_of_ads=number_of_ads, ) context['ad_placements'] = ad_placements except InsufficientAdPlacements: pass return context
def advertisement(context, ad_placement=None, count_view=True, format='fullwidth_single'): """ Render either a single random ad placement, or the one provided. Args: ad_placement: (Optional) an AdPlacement instance to render. Providing this renders other arguments irrelevant. count_view: If true, increase the view counter for the selected ad placement by one. format: The advertisement format to select. """ context = context.flatten() if ad_placement is None: # No ad placement was provided, select one with the given arguments try: ad_placement = AdPlacement.get_active_ads( site=context['request'].site, count_view=count_view, format=format, number_of_ads=1, )[0] except InsufficientAdPlacements: pass context['ad_placement'] = ad_placement return context