def test_save_fail(self, logger):
        access = MagicMock()
        access.save.side_effect = Exception

        tasks.save_access(access)

        self.assertEqual(logger.exception.call_count, 1)
    def test_save_fail(self, logger):
        access = MagicMock()
        access.save.side_effect = Exception

        tasks.save_access(access)

        self.assertEqual(logger.exception.call_count, 1)
    def test_save_ok(self, logger):
        access = MagicMock()
        access.save.return_value = True

        result = tasks.save_access(access)

        self.assertEqual(access.save.call_count, 1)
        self.assertTrue(result)
    def test_save_ok(self, logger):
        access = MagicMock()
        access.save.return_value = True

        result = tasks.save_access(access)

        self.assertEqual(access.save.call_count, 1)
        self.assertTrue(result)
    def process_response(self, request, response):
        """Postprocess response.

        :param request: Http request.
        :type request: django.http.HttpRequest
        :param response: Response.
        :type response: django.http.HttpResponse
        :return: None
        """
        try:
            if self._blacklisted:
                logger.debug("<Process Response> View:%s %s", str(self._view), 'BlackList')
            elif self._disabled:
                logger.debug("<Process Response> View:%s %s", str(self._view), 'Disabled')
            else:
                logger.debug("<Process Response> View:%s", str(self._view))

            if not self._blacklisted and not self._disabled:
                # Response
                self._response = self._extract_response_data(response)

                # Time
                self._time['response'] = datetime.datetime.now()

                # Providers
                custom = {app: f(request) for app, f in self._providers.iteritems()}
                custom = {k: v for k, v in custom.iteritems() if v is not None and len(v) > 0}

                # Save Access and Process
                self._access = update_access(self._access, response=self._response, time=self._time, custom=custom)

                if not settings.RUN_ASYNC:
                    save_access(self._access)
                else:
                    save_access.apply_async((self._access, ))
                logger.info("<Process Response> View:%s", self._view['full_name'])
        except Exception:
            logger.exception("<Process Response>")

        return response
    def process_view(self, request, view_func, view_args, view_kwargs):
        """Preprocess request.

        :param request: Http request.
        :type request: django.http.HttpRequest
        :param view_func: View.
        :type view_func: callable
        :param view_args: View arguments.
        :type view_args: list
        :param view_kwargs: View keyword arguments.
        :type view_kwargs: dict
        :return: None
        """
        self._disabled = getattr(view_func, 'disable_audit', False)

        if not self._disabled:
            try:
                view_data = self._extract_view_data(view_func, view_args, view_kwargs)

                self._blacklisted = self._check_blacklist(request.path, view_data['app'])

                msg = "<Process View> View:%s %s", view_data['full_name'], 'BlackList' if self._blacklisted else ''
                logger.debug(msg)

                if not self._blacklisted:
                    self._view = view_data
                    user = self._extract_user_data(request)

                    # Time
                    self._time = {
                        'request': datetime.datetime.now(),
                        'response': None
                    }

                    # Interlink ID
                    interlink_id = get_access_interlink_id()

                    # Create access
                    access = {
                        'interlink_id': interlink_id,
                        'request': request_to_dict(request),
                        'response': None,
                        'time': self._time,
                        'view': self._view,
                        'user': user,
                        'custom': None,
                    }

                    # Extract process data
                    self._process = extract_process_data()

                    # Save Access
                    self._access = create_access(access, self._process)
                    cache.set_last_access(self._access)

                    if not settings.RUN_ASYNC:
                        save_access(self._access)
                    else:
                        save_access.apply_async((self._access, ))
                    logger.info("<Process View> View:%s", self._view['full_name'])

                    logger.debug("View:%s", str(self._view))
            except Exception:
                logger.exception("<Process View>")

        return None