U
    {h5                     @   s   d dl Z d dlmZ d dlZd dlmZ d dlmZ d dlm	Z	m
Z
 d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ d dlmZ ddlmZ dd Zdd Z dd Z!G dd deZ"G dd deZ#dS )    N)defaultdict)get_user_model)ContentType)IntegerFieldValue)	force_str)gettext_lazy)AdminURLFinder)ContentTypeFilter)PageHistoryFilterSet)get_content_type_label)registry)PageLogEntry   )
ReportViewc                 C   sF   t  }t D ]}||j|   qt }|jj|d	|j
S )NZpk__in)setlog_action_registryget_log_entry_modelsupdateobjectsviewable_by_userZget_user_idsr   filterorder_byZUSERNAME_FIELD)userZuser_ids	log_modelUser r   q/var/www/dating/data/www/fatepal.com/env/lib/python3.8/site-packages/wagtail/admin/views/reports/audit_logging.pyget_users_for_filter   s
    r   c                 C   s>   t  }t D ]}||j|   qtjj|d	dS )Nr   model)
r   r   r   r   r   r   Zget_content_type_idsr   r   r   )r   Zcontent_type_idsr   r   r   r   get_content_types_for_filter    s    r!   c                    s@   t   t D ]} |j|   q fddt D S )Nc                    s   g | ]}|d   kr|qS )r   r   ).0actionactionsr   r   
<listcomp>0   s     z*get_actions_for_filter.<locals>.<listcomp>)r   r   r   r   r   r   Zget_actionsZget_choices)r   r   r   r$   r   get_actions_for_filter*   s    
r'   c                   @   sZ   e Zd ZejedddZeedddd dZd	d
 Z	dd Z
dd ZG dd dZdS )SiteHistoryReportFilterSetNameZ	icontains)labelZlookup_exprTypefilter_object_typec                 C   s
   t | jS N)r!   r   )requestr   r   r   <lambda>:       z#SiteHistoryReportFilterSet.<lambda>)r*   methodquerysetc                 C   s
   | |S r-   )Zfilter_on_content_type)selfr2   namevaluer   r   r   r,   =   s    z-SiteHistoryReportFilterSet.filter_object_typec                 C   s   t | jjS r-   )r'   r.   r   r3   r   r   r   get_action_choices@   s    z-SiteHistoryReportFilterSet.get_action_choicesc                 C   s   t | jjS r-   )r   r.   r   r6   r   r   r   get_user_choicesC   s    z+SiteHistoryReportFilterSet.get_user_choicesc                   @   s   e Zd ZeZg ZdS )zSiteHistoryReportFilterSet.MetaN)__name__
__module____qualname__r   r    fieldsr   r   r   r   MetaF   s   r=   N)r9   r:   r;   django_filtersZ
CharFilter_r*   r
   Zobject_typer,   r7   r8   r=   r   r   r   r   r(   5   s   r(   c                       s   e Zd ZdZedZdZeZdZ	dZ
edededed	ed
eddZddddddgZ fddZdd Zdd Zdd Zdd Z  ZS )LogEntriesViewz.wagtailadmin/reports/site_history_results.htmlzSite historyhistoryz!wagtailadmin_reports:site_historyz)wagtailadmin_reports:site_history_resultsZIDr)   r+   zAction typer   z	Date/Time)	object_idr*   content_typer#   user_display_name	timestamprB   r*   rC   r#   rD   rE   c                    sN   t  jf | | j | _| j| j| j| ji| jd< | jt| jti| jd< d S )Nr#   rC   )super__init__Zcustom_field_preprocesscopyZ
FORMAT_CSVget_action_labelZFORMAT_XLSXr   )r3   kwargs	__class__r   r   rG   d   s      
  zLogEntriesView.__init__c                 C   s   d tj dS )Nzaudit-log-{}z%Y-%m-%d)formatdatetimetodaystrftimer6   r   r   r   get_filenameq   s    zLogEntriesView.get_filenamec                 C   s   d}t t | _t| jD ]\\}}|j| jj	ddj
t|t dd}| |}| }|dkrn|}q||}q|dS )a  
        Since this report combines records from multiple log models, the standard pattern of
        returning a queryset from get_queryset() to be filtered by filter_queryset() is not
        possible - the subquery for each log model must be filtered separately before joining
        with union().

        Additionally, a union() on standard model-based querysets will return a queryset based on
        the first model in the union, so instances of the other model(s) would be returned as the
        wrong type. To avoid this, we construct values() querysets as follows:

        1. For each model, construct a values() queryset consisting of id, timestamp and an
           annotation to indicate which model it is, and filter this with filter_queryset
        2. Form a union() queryset from these queries, and order it by -timestamp
           (this is the result returned from get_filtered_queryset)
        3. Apply pagination (done in MultipleObjectMixin.get_context_data)
        4. (In decorate_paginated_queryset:) For each model included in the result set, look up
           the set of model instances by ID. Use these to form a final list of model instances
           in the same order as the query.
        NpkrE   )Zoutput_field)log_model_indexz
-timestamp)listr   r   
log_models	enumerater   r   r.   r   valuesZannotater   r   Zfilter_querysetr   union)r3   r2   rS   r   Zsub_querysetr   r   r   get_filtered_querysett   s      
z$LogEntriesView.get_filtered_querysetc           
         s   t t}|D ]}||d  |d  qt| jj}i  | D ]P\}}| j| j	ddj
|d }|D ]"\}}	||	|_| ||jf< qlq@ fdd|D S )NrS   rR   Zuser__wagtail_userprofilerC   r   c                    s    g | ]} |d  |d f qS )rS   rR   r   )r"   rowZobject_lookupr   r   r&      s     z>LogEntriesView.decorate_paginated_queryset.<locals>.<listcomp>)r   rT   appendr	   r.   r   itemsrU   r   Zprefetch_relatedr   Zwith_instancesZget_edit_urlZedit_urlrR   )
r3   r2   Zpks_by_log_model_indexrZ   Z
url_finderrS   ZpksZlog_entriesZ	log_entryinstancer   r[   r   decorate_paginated_queryset   s"     z*LogEntriesView.decorate_paginated_querysetc                 C   s   t t|S r-   )r   r   rI   )r3   r#   r   r   r   rI      s    zLogEntriesView.get_action_label)r9   r:   r;   Zresults_template_namer?   Z
page_titleZheader_iconr(   Zfilterset_classZindex_url_nameZindex_results_url_nameZexport_headingsZlist_exportrG   rQ   rY   r_   rI   __classcell__r   r   rK   r   r@   K   s2   		-r@   )$rN   collectionsr   r>   Zdjango.contrib.authr   Z"django.contrib.contenttypes.modelsr   Zdjango.db.modelsr   r   Zdjango.utils.encodingr   Zdjango.utils.translationr   r?   Zwagtail.admin.admin_url_finderr	   Zwagtail.admin.filtersr
   Z!wagtail.admin.views.pages.historyr   Zwagtail.coreutilsr   Zwagtail.log_actionsr   r   Zwagtail.modelsr   baser   r   r!   r'   r(   r@   r   r   r   r   <module>   s&   	
