from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from django.db import models
from apps.ads.models.ad_content import Advertisement


@csrf_exempt
def record_impression(request):
    if request.method == 'POST':
        ad_ids = request.POST.getlist('ad_ids[]')
        for ad_id in ad_ids:
            Advertisement.objects.filter(id=ad_id).update(
                show_count=models.F('show_count') + 1)
        return JsonResponse({'status': 'success'})


@csrf_exempt
def record_click(request):
    if request.method == 'POST':
        ad_id = request.POST.get('ad_id')
        Advertisement.objects.filter(id=ad_id).update(
            click_count=models.F('click_count') + 1)
        return JsonResponse({'status': 'success'})
