Beispiel #1
0
    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
Beispiel #2
0
def create_placement(request, site):
    active_site = Site.get_cached_by_id(site)
    try:
        ad = Ad.objects.get(id=request.POST['ad'], site=active_site)
        if request.POST['adplacement_type'] == 'time':
            start_date = datetime.strptime(request.POST['start_date'], "%d.%m.%Y")
            end_date = datetime.strptime(request.POST['end_date'], "%d.%m.%Y")
            view_limit = None
        else:
            start_date = datetime.strptime(request.POST['start_date'], "%d.%m.%Y")
            end_date = None
            view_limit = request.POST['view_limit']
        ap = AdPlacement(
            ad=ad,
            start_date=start_date,
            end_date=end_date,
            view_limit=view_limit,
            site=active_site,
        )
        ap.save()
    except ValueError:
        messages.error(request, 'invalid_date')
    return redirect('admin:sites.ads.list', active_site.id)
Beispiel #3
0
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