a
    Dg`                     @   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   s   | j S N)messageselfZ	log_entry r   R/var/www/lab.imftr.de/x/nb_venv/lib/python3.9/site-packages/wagtail/log_actions.pyformat_message   s    zLogFormatter.format_messagec                 C   s   | j S r   )commentr
   r   r   r   format_comment   s    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 | _n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  n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   )Z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   s    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 )NZregister_log_actionsT)r,   r   Z	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   )r/   registerr1   add)r   clslog_entry_modelr   r   r   register_modelf   s    z LogActionRegistry.register_modelc                    s@    fdd}|r8|\}}t dtf||d}|| n|S d S )Nc                    s&   |  }|j  < j |jf d S r   )r-   r.   appendr   )formatter_cls	formatteractionr   r   r   register_formatter_classk   s    
zCLogActionRegistry.register_action.<locals>.register_formatter_classZ_LogFormatter)r   r	   )r%   r   )r   r=   argsr>   r   r	   r:   r   r<   r   register_actionj   s    
z!LogActionRegistry.register_actionc                 C   s   |    | jS r   )r3   r.   r"   r   r   r   get_choices}   s    zLogActionRegistry.get_choicesc                 C   s   |    | j|jS r   )r3   r-   getr=   r
   r   r   r   get_formatter   s    zLogActionRegistry.get_formatterc                 C   s   |    || jv S r   )r3   r-   r   r=   r   r   r   action_exists   s    zLogActionRegistry.action_existsc                 C   s   |    | jS r   )r3   r1   r"   r   r   r   get_log_entry_models   s    z&LogActionRegistry.get_log_entry_modelsc                 C   s   | j | jS r   )r-   r   rD   r   r   r   get_action_label   s    z"LogActionRegistry.get_action_labelc                 C   s   |    | j|S r   )r3   r/   Z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   Z_wrappedrI   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}|p4t j}|jj||f||d|S )N)r   r   )r3   rL   r*   r   r   objectsZ
log_action)r   rK   r=   r   r   kwargsr7   r   r   r   log   s    
zLogActionRegistry.logc                 C   s4   |  |}|d u r(ddlm} |j S |j|S )Nr   )ModelLogEntry)rL   Zwagtail.modelsrP   rM   noneZfor_instance)r   rK   r7   rP   r   r   r   get_logs_for_instance   s
    

z'LogActionRegistry.get_logs_for_instance)NN)r   r   r   r   r   r3   r8   r@   rA   rC   rE   rF   rG   rI   rL   rO   rR   r   r   r   r   r+   I   s   
r+   c                 K   s   t j| |fi |S r   )registryrO   )rK   r=   rN   r   r   r   rO      s    rO   )r   Zasgiref.localr   Zdjango.utils.functionalr   Zwagtailr   Zwagtail.utils.registryr   r   r   r   r)   r!   r$   r*   r+   rS   rO   r   r   r   r   <module>   s   
l