def m2m_changed(sender, instance, action, reverse, model, pk_set, using, **kwargs): """https://docs.djangoproject.com/es/1.10/ref/signals/#m2m-changed""" try: with transaction.atomic(): if not should_audit(instance): return False if action not in ("post_add", "post_remove", "post_clear"): return False object_json_repr = serializers.serialize("json", [instance]) if reverse: event_type = CRUDEvent.M2M_CHANGE_REV # add reverse M2M changes to event. must use json lib because # django serializers ignore extra fields. tmp_repr = json.loads(object_json_repr) m2m_rev_field = _m2m_rev_field_name( instance._meta.concrete_model, model) related_instances = getattr(instance, m2m_rev_field).all() related_ids = [r.pk for r in related_instances] tmp_repr[0]['m2m_rev_model'] = force_text(model._meta) tmp_repr[0]['m2m_rev_pks'] = related_ids tmp_repr[0]['m2m_rev_action'] = action object_json_repr = json.dumps(tmp_repr) else: event_type = CRUDEvent.M2M_CHANGE # user try: user = get_current_user() # validate that the user still exists user = get_user_model().objects.get(pk=user.pk) except: user = None if isinstance(user, AnonymousUser): user = None crud_event = CRUDEvent.objects.create( event_type=event_type, object_repr=str(instance), object_json_repr=object_json_repr, content_type=ContentType.objects.get_for_model(instance), object_id=instance.pk, user=user, datetime=timezone.now(), user_pk_as_string=str(user.pk) if user else user, remote_ip=get_client_ip(get_current_request()), browser=get_client_browser_info(get_current_request()), operating_system=get_client_operating_system_info( get_current_request())) except Exception: traceback.print_exc() logger.exception('easy audit had an m2m-changed exception.')
def post_save(sender, instance, created, raw, using, update_fields, **kwargs): """https://docs.djangoproject.com/es/1.10/ref/signals/#post-save""" if raw: # Return if loading Fixtures return try: with transaction.atomic(): if not should_audit(instance): return False object_json_repr = serializers.serialize("json", [instance]) # created or updated? if created: event_type = CRUDEvent.CREATE # user try: user = get_current_user() # validate that the user still exists user = get_user_model().objects.get(pk=user.pk) except: user = None if isinstance(user, AnonymousUser): user = None # callbacks kwargs['request'] = get_current_request( ) # make request available for callbacks create_crud_event = all( callback(instance, object_json_repr, created, raw, using, update_fields, **kwargs) for callback in CRUD_DIFFERENCE_CALLBACKS if callable(callback)) # create crud event only if all callbacks returned True if create_crud_event and created: crud_event = CRUDEvent.objects.create( event_type=event_type, object_repr=str(instance), object_json_repr=object_json_repr, content_type=ContentType.objects.get_for_model(instance), object_id=instance.pk, user=user, datetime=timezone.now(), user_pk_as_string=str(user.pk) if user else user, remote_ip=get_client_ip(get_current_request()), browser=get_client_browser_info(get_current_request()), operating_system=get_client_operating_system_info( get_current_request())) except Exception: traceback.print_exc() logger.exception('easy audit had a post-save exception.')
def request_started_handler(sender, environ, **kwargs): if not should_log_url(environ['PATH_INFO']): return # get the user from request user = get_current_user() if user and user.is_anonymous: user = None # get the user from cookies if not user and environ.get('HTTP_COOKIE'): cookie = SimpleCookie() # python3 compatibility cookie.load(environ['HTTP_COOKIE']) session_cookie_name = settings.SESSION_COOKIE_NAME if session_cookie_name in cookie: session_id = cookie[session_cookie_name].value try: session = Session.objects.get(session_key=session_id) except Session.DoesNotExist: session = None if session: user_id = session.get_decoded().get('_auth_user_id') try: user = get_user_model().objects.get(id=user_id) except: user = None request_event = RequestEvent.objects.create( url=environ['PATH_INFO'], method=environ['REQUEST_METHOD'], query_string=environ['QUERY_STRING'], user=user, remote_ip=get_client_ip(record('FakeRequest', { 'META': environ })), browser=get_client_browser_info(record('FakeRequest', { 'META': environ })), operating_system=get_client_operating_system_info(record('FakeRequest', { 'META': environ })), datetime=timezone.now())
def post_delete(sender, instance, using, **kwargs): """https://docs.djangoproject.com/es/1.10/ref/signals/#post-delete""" try: with transaction.atomic(): if not should_audit(instance): return False object_json_repr = serializers.serialize("json", [instance]) # user try: user = get_current_user() # validate that the user still exists user = get_user_model().objects.get(pk=user.pk) except: user = None if isinstance(user, AnonymousUser): user = None # crud event crud_event = CRUDEvent.objects.create( event_type=CRUDEvent.DELETE, object_repr=str(instance), object_json_repr=object_json_repr, content_type=ContentType.objects.get_for_model(instance), object_id=instance.pk, user=user, datetime=timezone.now(), user_pk_as_string=str(user.pk) if user else user, remote_ip=get_client_ip(get_current_request()), browser=get_client_browser_info(get_current_request()), operating_system=get_client_operating_system_info( get_current_request())) except Exception: traceback.print_exc() logger.exception('easy audit had a post-delete exception.')