Skip to content

Commit

Permalink
Merge pull request #15 from soynatan/feat/registered_classes
Browse files Browse the repository at this point in the history
feat/registered classes
  • Loading branch information
soynatan authored May 14, 2017
2 parents 50b5079 + 0145084 commit 66e1dda
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
20 changes: 16 additions & 4 deletions easyaudit/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@

from easyaudit.models import CRUDEvent, LoginEvent

def get_model_list(class_list):
"""
Receives a list of strings with app_name.model_name format
and turns them into classes. If an item is already a class
it ignores it.
"""
for idx, item in enumerate(class_list):
if isinstance(item, six.string_types):
model_class = apps.get_model(item)
class_list[idx] = model_class


# default unregistered classes
UNREGISTERED_CLASSES = [CRUDEvent, LoginEvent, Migration, LogEntry, Session, Permission, ContentType,
MigrationRecorder.Migration]
Expand All @@ -21,11 +33,11 @@

# extra unregistered classes
UNREGISTERED_CLASSES.extend(getattr(settings, 'DJANGO_EASY_AUDIT_UNREGISTERED_CLASSES_EXTRA', []))
get_model_list(UNREGISTERED_CLASSES)

for idx, item in enumerate(UNREGISTERED_CLASSES):
if isinstance(item, six.string_types):
model_class = apps.get_model(item)
UNREGISTERED_CLASSES[idx] = model_class
# register only the specified classes, excepting UNREGISTERED_CLASSES
REGISTERED_CLASSES = getattr(settings, 'DJANGO_EASY_AUDIT_REGISTERED_CLASSES', [])
get_model_list(REGISTERED_CLASSES)

# should login events be registered?
WATCH_LOGIN_EVENTS = getattr(settings, 'DJANGO_EASY_AUDIT_WATCH_LOGIN_EVENTS', True)
Expand Down
32 changes: 25 additions & 7 deletions easyaudit/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,38 @@

from .middleware.easyaudit import get_current_request, get_current_user
from .models import CRUDEvent, LoginEvent
from .settings import UNREGISTERED_CLASSES, WATCH_LOGIN_EVENTS, CRUD_DIFFERENCE_CALLBACKS
from .settings import REGISTERED_CLASSES, UNREGISTERED_CLASSES, WATCH_LOGIN_EVENTS, CRUD_DIFFERENCE_CALLBACKS


logger = logging.getLogger(__name__)

def should_audit(instance):
"""Returns True or False to indicate whether the instance
should be audited or not, depending on the project settings."""

# do not audit any model listed in UNREGISTERED_CLASSES
for unregistered_class in UNREGISTERED_CLASSES:
if isinstance(instance, unregistered_class):
return False

# only audit models listed in REGISTERED_CLASSES (if it's set)
if len(REGISTERED_CLASSES) > 0:
for registered_class in REGISTERED_CLASSES:
if isinstance(instance, registered_class):
break
else:
return False

# all good
return True


# signals
def post_save(sender, instance, created, raw, using, update_fields, **kwargs):
"""https://docs.djangoproject.com/es/1.10/ref/signals/#post-save"""
try:
for unregistered_class in UNREGISTERED_CLASSES:
if isinstance(instance, unregistered_class):
return False
if not should_audit(instance):
return False

object_json_repr = serializers.serialize("json", [instance])

Expand Down Expand Up @@ -66,9 +85,8 @@ def post_save(sender, instance, created, raw, using, update_fields, **kwargs):
def post_delete(sender, instance, using, **kwargs):
"""https://docs.djangoproject.com/es/1.10/ref/signals/#post-delete"""
try:
for unregistered_class in UNREGISTERED_CLASSES:
if isinstance(instance, unregistered_class):
return False
if not should_audit(instance):
return False

object_json_repr = serializers.serialize("json", [instance])

Expand Down

0 comments on commit 66e1dda

Please sign in to comment.