def createAdPool(request): """ # Provide view for inserting campaign in ad-pool if it does'nt exist already """ errors = [] if request.method == 'POST': form = CreateAdPool(request.POST) if form.is_valid(): cd = form.cleaned_data #check if campaign with the same name already exists or not try: x = Adpool.objects.get(campaignname = cd['campaignName']) #save if campaign with same name does'nt already exists except Adpool.DoesNotExist: try: p = Adpool(campaignname = cd['campaignName'], ad = cd['ad'], targetpages = cd['targetPages'], adtype = cd['adType']) p.save() except: errors.append('Unable to save Ad in Pool!') return render_to_response('createadpool.html', {'form': form, 'errors': errors}, context_instance=RequestContext(request)) return HttpResponseRedirect('/createadpool/success/') if x: errors.append('Campaign already exists!') return render_to_response('createadpool.html', {'form': form, 'errors': errors}, context_instance=RequestContext(request)) else: return render_to_response('createadpool.html', {'form': form},context_instance=RequestContext(request)) else: form = CreateAdPool() return render_to_response('createadpool.html', {'form': form},context_instance=RequestContext(request))
def checkAdPool(displaytype): try: # Checking ads all ads of current displaytype are displayed or not adsToDisplay = Adpool.objects.filter(isdisplayed = False, adtype = displaytype) # If all ads are displayed, update impression counter in internal ads and external ads, # delete Adpool, and refill it if not adsToDisplay: # Get Distinct Internal ads from Adpool of current displaytype getInternalAdsFromAdPool = Adpool.objects.filter(adtype = displaytype).exclude(internalad = None).values('internalad').distinct().order_by('internalad') # For each Internal ad getting from Adpool, update it's impression counter in Internalad for eachAd in getInternalAdsFromAdPool: x = Internalad.objects.get(id = uuid.UUID(eachAd['internalad'])) x.inoofimpressions = int(x.inoofimpressions) - int(x.priority) x.save() # Get Distinct External ads from Adpool of current displaytype getExternalAdsFromAdPool = Adpool.objects.filter(adtype = displaytype).exclude(externalad = None).values('externalad').distinct().order_by('externalad') # For each External ad getting from Adpool, update it's impression counter in Externalad for eachAd in getExternalAdsFromAdPool: x = Externalad.objects.get(id = uuid.UUID(eachAd['externalad'])) x.inoofimpressions = int(x.inoofimpressions) - int(x.priority) x.save() # Delete all ads of current displaytype from Adpool Adpool.objects.filter(adtype = displaytype).delete() # Refill Adpool # Get internal ads of current displaytype having active & expiry date greater than today, # and having impressioncounter > 0 getInternalAds = Internalad.objects.filter( Q(activedate__gte = datetime.datetime.now()) | Q(expireddate__gte = datetime.datetime.now()) , Q(adtype = displaytype), Q(inoofimpressions__gt = 0) ) # Get external ads of current displaytype having active & expiry date greater than today, # and having impressioncounter > 0 getExternalAds = Externalad.objects.filter( Q(activedate__gte = datetime.datetime.now()) | Q(expireddate__gte = datetime.datetime.now()) , Q(adtype=displaytype), Q(inoofimpressions__gt = 0) ) # Fill internal ads in Adpool for eachAd in getInternalAds: for x in range(0,eachAd.priority): p = Adpool( campaignname = str(eachAd.campaignname), ad = str(eachAd.ad), targetpages = str(eachAd.targetpages), adtype = str(eachAd.adtype), internalad_id = uuid.UUID(eachAd.id) ) p.save() # Fill external ads in Adpool for eachAd in getExternalAds: for x in range(0,eachAd.priority): p = Adpool( campaignname = str(eachAd.campaignname), ad = str(eachAd.ad), targetpages = str(eachAd.targetpages), adtype = str(eachAd.adtype), externalad_id = uuid.UUID(eachAd.id) ) p.save() except: pass