from django.contrib.admin.models import LogEntry
from django.contrib.contenttypes.models import ContentType
from django.utils.encoding import force_str

def log_action(user, obj, action_flag, change_message=None):
    """
    Create a log entry for an action performed outside the admin interface

    Args:
        user: The user performing the action
        obj: The model instance being modified
        action_flag: 1 -> ADDITION, 2 -> CHANGE, or 3 -> DELETION
        change_message: Description of what changed
    """
    LogEntry.objects.log_action(
        user_id=user.id,
        content_type_id=ContentType.objects.get_for_model(obj).pk,
        object_id=obj.pk,
        object_repr=force_str(obj),
        action_flag=action_flag,
        change_message=change_message or ''
    )
