from .services import track_page_view
import logging


logger = logging.getLogger(__name__)


class CSPMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        response = self.get_response(request)
        response['Content-Security-Policy'] = "frame-ancestors 'self' https://metrika.yandex.ru https://metrika.yandex.by https://metrica.yandex.com https://metrica.yandex.com.tr https://*.webvisor.com"
        return response


class PageViewMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        # Игнорируем статические файлы и админку
        if not request.path.startswith('/static/') and not request.path.startswith('/admin/'):
            track_page_view(request)

        response = self.get_response(request)
        return response
