from django.middleware.csrf import CsrfViewMiddleware, REASON_NO_CSRF_COOKIE, REASON_BAD_TOKEN

from corehq.util.soft_assert import soft_assert
from django.conf import settings


class HQCsrfViewMiddleWare(CsrfViewMiddleware):

    def _reject(self, request, reason):
        if settings.CSRF_ALWAYS_OFF and reason in [REASON_NO_CSRF_COOKIE, REASON_BAD_TOKEN]:

            warning = "Request at {url} doesn't contain a csrf token. Letting the request pass through for now. "\
                      "Check if we are sending csrf_token in the corresponding POST form, if not fix it. "\
                      "Read more here https://github.com/dimagi/commcare-hq/pull/9227".format(
                          url=request.path
                      )
            _assert = soft_assert(notify_admins=True, exponential_backoff=True)
            _assert(False, warning)

            return self._accept(request)
        else:
            return super(HQCsrfViewMiddleWare, self)._reject(request, reason)

import logging

from django.middleware.csrf import CsrfViewMiddleware, REASON_NO_CSRF_COOKIE, REASON_BAD_TOKEN
from django.conf import settings


logger = logging.getLogger('')


class HQCsrfViewMiddleWare(CsrfViewMiddleware):

    def _reject(self, request, reason):
        if settings.CSRF_ALWAYS_OFF and reason in [REASON_NO_CSRF_COOKIE, REASON_BAD_TOKEN]:

            warning = "Request at {url} doesn't contain a csrf token. Letting the request pass through for now. "\
                      "Check if we are sending csrf_token in the corresponding POST form, if not fix it. "\
                      "Read more here https://github.com/dimagi/commcare-hq/pull/9227".format(
                          url=request.path
                      )
            logger.error(warning)
            return self._accept(request)
        else:
            return super(HQCsrfViewMiddleWare, self)._reject(request, reason)

Revert "Revert "log to file, don't email""
Revert "Revert "log to file, don't email""

This reverts commit 95245bb7fab6efe5a72cb8abbf4380a26b72a720.

+ import logging
+ 
  from django.middleware.csrf import CsrfViewMiddleware, REASON_NO_CSRF_COOKIE, REASON_BAD_TOKEN
+ from django.conf import settings
  
- from corehq.util.soft_assert import soft_assert
- from django.conf import settings
+ 
+ logger = logging.getLogger('')
  
  
  class HQCsrfViewMiddleWare(CsrfViewMiddleware):
  
      def _reject(self, request, reason):
          if settings.CSRF_ALWAYS_OFF and reason in [REASON_NO_CSRF_COOKIE, REASON_BAD_TOKEN]:
  
              warning = "Request at {url} doesn't contain a csrf token. Letting the request pass through for now. "\
                        "Check if we are sending csrf_token in the corresponding POST form, if not fix it. "\
                        "Read more here https://github.com/dimagi/commcare-hq/pull/9227".format(
                            url=request.path
                        )
+             logger.error(warning)
-             _assert = soft_assert(notify_admins=True, exponential_backoff=True)
-             _assert(False, warning)
- 
              return self._accept(request)
          else:
              return super(HQCsrfViewMiddleWare, self)._reject(request, reason)
  
Revert "Revert "log to file, don't email""
## Code Before:
from django.middleware.csrf import CsrfViewMiddleware, REASON_NO_CSRF_COOKIE, REASON_BAD_TOKEN

from corehq.util.soft_assert import soft_assert
from django.conf import settings


class HQCsrfViewMiddleWare(CsrfViewMiddleware):

    def _reject(self, request, reason):
        if settings.CSRF_ALWAYS_OFF and reason in [REASON_NO_CSRF_COOKIE, REASON_BAD_TOKEN]:

            warning = "Request at {url} doesn't contain a csrf token. Letting the request pass through for now. "\
                      "Check if we are sending csrf_token in the corresponding POST form, if not fix it. "\
                      "Read more here https://github.com/dimagi/commcare-hq/pull/9227".format(
                          url=request.path
                      )
            _assert = soft_assert(notify_admins=True, exponential_backoff=True)
            _assert(False, warning)

            return self._accept(request)
        else:
            return super(HQCsrfViewMiddleWare, self)._reject(request, reason)

## Instruction:
Revert "Revert "log to file, don't email""
## Code After:
import logging

from django.middleware.csrf import CsrfViewMiddleware, REASON_NO_CSRF_COOKIE, REASON_BAD_TOKEN
from django.conf import settings


logger = logging.getLogger('')


class HQCsrfViewMiddleWare(CsrfViewMiddleware):

    def _reject(self, request, reason):
        if settings.CSRF_ALWAYS_OFF and reason in [REASON_NO_CSRF_COOKIE, REASON_BAD_TOKEN]:

            warning = "Request at {url} doesn't contain a csrf token. Letting the request pass through for now. "\
                      "Check if we are sending csrf_token in the corresponding POST form, if not fix it. "\
                      "Read more here https://github.com/dimagi/commcare-hq/pull/9227".format(
                          url=request.path
                      )
            logger.error(warning)
            return self._accept(request)
        else:
            return super(HQCsrfViewMiddleWare, self)._reject(request, reason)
