o
    h`                     @   s   d dl Z d dlmZ d dlmZ d dlmZ d dlmZ G dd dZ	e Z
G dd	 d	Zed
dZdd Zdd Zdd ZG dd dZe Zdd ZdS )    N)Local)
LazyObject)hooks)ObjectTypeRegistryc                   @   s,   e Zd ZdZdZdZdZdd Zdd ZdS )LogFormattera6  
    Defines how to format log messages / comments for a particular action type. Messages that depend on
    log entry data should override format_message / format_comment; static messages can just be set as the
    'message' / 'comment' attribute.

    To be registered with log_registry.register_action.
     c                 C      | j S N)messageself	log_entry r   S/var/www/html/ndineBlogger/venv/lib/python3.10/site-packages/wagtail/log_actions.pyformat_message      zLogFormatter.format_messagec                 C   r   r	   )commentr   r   r   r   format_comment   r   zLogFormatter.format_commentN)	__name__
__module____qualname____doc__labelr
   r   r   r   r   r   r   r   r   
   s    r   c                   @   s*   e Zd ZdZd
ddZdd Zdd	 ZdS )
LogContextz
    Stores data about the environment in which a logged action happens -
    e.g. the active user - to be stored in the log entry for that action.
    NTc                 C   s"   || _ |rt | _d S d | _d S r	   )useruuiduuid4)r   r   generate_uuidr   r   r   __init__'   s   
zLogContext.__init__c                 C   s   t tdd | _t|  | S Nvalue)getattr_active_old_log_contextactivater   r   r   r   	__enter__.   s   zLogContext.__enter__c                 C   s   | j r
t| j  d S t  d S r	   )r#   r$   
deactivate)r   typer    	tracebackr   r   r   __exit__3   s   
zLogContext.__exit__)NT)r   r   r   r   r   r&   r*   r   r   r   r   r   !   s
    
r   F)r   c                 C   s
   | t _d S r	   r"   r    )log_contextr   r   r   r$   =   s   
r$   c                   C   s   t `d S r	   r+   r   r   r   r   r'   A   s   r'   c                   C   s   t tdtS r   )r!   r"   empty_log_contextr   r   r   r   get_active_log_contextE      r.   c                   @   sz   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zdd ZdddZdd ZdS )LogActionRegistryz
    A central store for log actions.
    The expected format for registered log actions: Namespaced action, Action label, Action message (or callable)
    c                 C   s&   d| _ i | _g | _t | _t | _d S )NF)has_scanned_for_actions
formatterschoicesr   log_entry_models_by_typesetlog_entry_modelsr%   r   r   r   r   O   s
   zLogActionRegistry.__init__c                 C   s,   | j stdD ]}||  qd| _ d S d S )Nregister_log_actionsT)r1   r   	get_hooks)r   fnr   r   r   scan_for_actions_   s
   

z"LogActionRegistry.scan_for_actionsc                 C   s    | j j||d | j| d S )N)r    )r4   registerr6   add)r   clslog_entry_modelr   r   r   register_modelf   s   z LogActionRegistry.register_modelc                    s>    fdd}|r|\}}t dtf||d}|| d S |S )Nc                    s&   |  }|j  < j |jf d S r	   )r2   r3   appendr   )formatter_cls	formatteractionr   r   r   register_formatter_classk   s   
zCLogActionRegistry.register_action.<locals>.register_formatter_class_LogFormatter)r   r
   )r(   r   )r   rD   argsrE   r   r
   rA   r   rC   r   register_actionj   s   z!LogActionRegistry.register_actionc                 C      |    | jS r	   )r:   r3   r%   r   r   r   get_choices}      zLogActionRegistry.get_choicesc                 C   s   |    | j|jS r	   )r:   r2   getrD   r   r   r   r   get_formatter   s   zLogActionRegistry.get_formatterc                 C   s   |    || jv S r	   )r:   r2   r   rD   r   r   r   action_exists   s   
zLogActionRegistry.action_existsc                 C   rI   r	   )r:   r6   r%   r   r   r   get_log_entry_models   rK   z&LogActionRegistry.get_log_entry_modelsc                 C   s   | j | jS r	   )r2   r   rN   r   r   r   get_action_label   r/   z"LogActionRegistry.get_action_labelc                 C   s   |    | j|S r	   )r:   r4   get_by_type)r   modelr   r   r   get_log_model_for_model   s   z)LogActionRegistry.get_log_model_for_modelc                 C   s   t |tr|j}| t|S r	   )
isinstancer   _wrappedrT   r(   )r   instancer   r   r   get_log_model_for_instance   s   
z,LogActionRegistry.get_log_model_for_instanceNc                 K   sR   |    | |}|d u rd S |pt j}|pt j}|jj||f||d|S )N)r   r   )r:   rX   r.   r   r   objects
log_action)r   rW   rD   r   r   kwargsr>   r   r   r   log   s   
zLogActionRegistry.logc                 C   s4   |  |}|d u rddlm} |j S |j|S )Nr   )ModelLogEntry)rX   wagtail.modelsr]   rY   nonefor_instance)r   rW   r>   r]   r   r   r   get_logs_for_instance   s
   

z'LogActionRegistry.get_logs_for_instance)NN)r   r   r   r   r   r:   r?   rH   rJ   rM   rO   rP   rQ   rT   rX   r\   ra   r   r   r   r   r0   I   s    
r0   c                 K   s   t j| |fi |S r	   )registryr\   )rW   rD   r[   r   r   r   r\      s   r\   )r   asgiref.localr   django.utils.functionalr   wagtailr   wagtail.utils.registryr   r   r"   r   r-   r$   r'   r.   r0   rb   r\   r   r   r   r   <module>   s    
l