Exemplo n.º 1
0
def _post_save(sender, **kwargs):
    try:
        from audit_tools.audit.models import ACTIONS
        from audit_tools.audit.tasks import save_model_action

        i = kwargs['instance']

        # Model
        model = _extract_model_data(i)

        # Old and new content
        old_data = {}
        if id(i) in _CACHE:
            old_data = _CACHE[id(i)]
            del _CACHE[id(i)]
        new_data = serialize_model_instance(i)

        content = _extract_content_data(old_data, new_data)

        # Action
        if not old_data and new_data:
            action = ACTIONS.CREATE
        else:
            action = ACTIONS.UPDATE

        # Instance
        instance = _extract_instance_data(i)

        # Timestamp
        timestamp = datetime.datetime.now()

        model_action = {
            'model': model,
            'action': action,
            'content': content,
            'instance': instance,
            'timestamp': timestamp,
        }

        # Process
        process = extract_process_data()

        try:
            # Get process
            process = cache.get_process(process)
            access = cache.get_last_access()

            if not settings.RUN_ASYNC:
                save_model_action(model_action, access, process)
            else:
                save_model_action.apply_async((model_action, access, process))
            logger.info("<%s> Model:%s ID:%s", action.capitalize(),
                        model['full_name'], instance['id'])
        except Exception:
            logger.exception("<%s> Model:%s ID:%s", action.capitalize(),
                             model['full_name'], instance['id'])
    except Exception:
        logger.exception("<Post Save>")
Exemplo n.º 2
0
def _post_save(sender, **kwargs):
    try:
        from audit_tools.audit.models import ACTIONS
        from audit_tools.audit.tasks import save_model_action

        i = kwargs["instance"]

        # Model
        model = _extract_model_data(i)

        # Old and new content
        old_data = {}
        if id(i) in _CACHE:
            old_data = _CACHE[id(i)]
            del _CACHE[id(i)]
        new_data = serialize_model_instance(i)

        content = _extract_content_data(old_data, new_data)

        # Action
        if not old_data and new_data:
            action = ACTIONS.CREATE
        else:
            action = ACTIONS.UPDATE

        # Instance
        instance = _extract_instance_data(i)

        # Timestamp
        timestamp = datetime.datetime.now()

        model_action = {
            "model": model,
            "action": action,
            "content": content,
            "instance": instance,
            "timestamp": timestamp,
        }

        # Process
        process = extract_process_data()

        try:
            # Get process
            process = cache.get_process(process)
            access = cache.get_last_access()

            if not settings.RUN_ASYNC:
                save_model_action(model_action, access, process)
            else:
                save_model_action.apply_async((model_action, access, process))
            logger.info("<%s> Model:%s ID:%s", action.capitalize(), model["full_name"], instance["id"])
        except Exception:
            logger.exception("<%s> Model:%s ID:%s", action.capitalize(), model["full_name"], instance["id"])
    except Exception:
        logger.exception("<Post Save>")
Exemplo n.º 3
0
    def test_extract_process_data_managepy(self, interlink, socket, process, sys):
        socket.gethostname.return_value = 'Machine'
        sys.argv = ['manage.py', 'foo', 'b', 'a', 'r']

        process_data = utils.extract_process_data()

        self.assertEqual(process_data['interlink_id'], 'Interlink ID')
        self.assertEqual(process_data['user'], 'Username')
        self.assertEqual(process_data['pid'], 1)
        self.assertEqual(process_data['machine'], 'Machine')
        self.assertEqual(process_data['name'], 'foo')
        self.assertEqual(process_data['args'], 'b a r')
        self.assertIn('creation_time', process_data)
Exemplo n.º 4
0
    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