def log(action, *args, **kw):
    '\n    e.g. amo.log(amo.LOG.CREATE_ADDON, []),\n         amo.log(amo.LOG.ADD_FILE_TO_VERSION, file, version)\n    '
    from amo import get_user, logger_log
    from mkt.developers.models import ActivityLog, ActivityLogAttachment, AppLog, CommentLog, GroupLog, UserLog, VersionLog
    from mkt.access.models import Group
    from mkt.webapps.models import Webapp
    from mkt.users.models import UserProfile
    from mkt.versions.models import Version
    user = kw.get('user', get_user())
    if (not user):
        logger_log.warning(('Activity log called with no user: %s' % action.id))
        return
    al = ActivityLog(user=user, action=action.id)
    al.arguments = args
    if ('details' in kw):
        al.details = kw['details']
    al.save()
    if (('details' in kw) and ('comments' in al.details)):
        CommentLog(comments=al.details['comments'], activity_log=al).save()
    if ('created' in kw):
        al.created = kw['created']
        al.save()
    if ('attachments' in kw):
        formset = kw['attachments']
        storage = get_storage_class()()
        for form in formset:
            data = form.cleaned_data
            if ('attachment' in data):
                attachment = data['attachment']
                storage.save(('%s/%s' % (settings.REVIEWER_ATTACHMENTS_PATH, attachment.name)), attachment)
                ActivityLogAttachment(activity_log=al, description=data['description'], mimetype=attachment.content_type, filepath=attachment.name).save()
    for arg in args:
        if isinstance(arg, tuple):
            if (arg[0] == Webapp):
                AppLog(addon_id=arg[1], activity_log=al).save()
            elif (arg[0] == Version):
                VersionLog(version_id=arg[1], activity_log=al).save()
            elif (arg[0] == UserProfile):
                UserLog(user_id=arg[1], activity_log=al).save()
            elif (arg[0] == Group):
                GroupLog(group_id=arg[1], activity_log=al).save()
        if isinstance(arg, Webapp):
            AppLog(addon=arg, activity_log=al).save()
        elif isinstance(arg, Version):
            VersionLog(version=arg, activity_log=al).save()
        elif isinstance(arg, UserProfile):
            UserLog(activity_log=al, user=arg).save()
        elif isinstance(arg, Group):
            GroupLog(group=arg, activity_log=al).save()
    UserLog(activity_log=al, user=user).save()
    return al
e.g. amo.log(amo.LOG.CREATE_ADDON, []),
     amo.log(amo.LOG.ADD_FILE_TO_VERSION, file, version)
def log(action, *args, **kw):
    '\n    e.g. amo.log(amo.LOG.CREATE_ADDON, []),\n         amo.log(amo.LOG.ADD_FILE_TO_VERSION, file, version)\n    '
    from amo import get_user, logger_log
    from mkt.developers.models import ActivityLog, ActivityLogAttachment, AppLog, CommentLog, GroupLog, UserLog, VersionLog
    from mkt.access.models import Group
    from mkt.webapps.models import Webapp
    from mkt.users.models import UserProfile
    from mkt.versions.models import Version
    user = kw.get('user', get_user())
    if (not user):
        logger_log.warning(('Activity log called with no user: %s' % action.id))
        return
    al = ActivityLog(user=user, action=action.id)
    al.arguments = args
    if ('details' in kw):
        al.details = kw['details']
    al.save()
    if (('details' in kw) and ('comments' in al.details)):
        CommentLog(comments=al.details['comments'], activity_log=al).save()
    if ('created' in kw):
        al.created = kw['created']
        al.save()
    if ('attachments' in kw):
        formset = kw['attachments']
        storage = get_storage_class()()
        for form in formset:
            data = form.cleaned_data
            if ('attachment' in data):
                attachment = data['attachment']
                storage.save(('%s/%s' % (settings.REVIEWER_ATTACHMENTS_PATH, attachment.name)), attachment)
                ActivityLogAttachment(activity_log=al, description=data['description'], mimetype=attachment.content_type, filepath=attachment.name).save()
    for arg in args:
        if isinstance(arg, tuple):
            if (arg[0] == Webapp):
                AppLog(addon_id=arg[1], activity_log=al).save()
            elif (arg[0] == Version):
                VersionLog(version_id=arg[1], activity_log=al).save()
            elif (arg[0] == UserProfile):
                UserLog(user_id=arg[1], activity_log=al).save()
            elif (arg[0] == Group):
                GroupLog(group_id=arg[1], activity_log=al).save()
        if isinstance(arg, Webapp):
            AppLog(addon=arg, activity_log=al).save()
        elif isinstance(arg, Version):
            VersionLog(version=arg, activity_log=al).save()
        elif isinstance(arg, UserProfile):
            UserLog(activity_log=al, user=arg).save()
        elif isinstance(arg, Group):
            GroupLog(group=arg, activity_log=al).save()
    UserLog(activity_log=al, user=user).save()
    return al