o
    h                     @   s|   d dl Z d dl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 e d	Zd
gZG dd
 d
eZdS )    N)defaultdict)urlparse)warn)ImproperlyConfigured)RemovedInWagtail70Warning   )BaseBackendzwagtail.frontendcacheCloudfrontBackendc                       s4   e Zd Z fddZdd Zdd Zdd Z  ZS )	r	   c                    s   dd l }t | |jd|d|d|dd| _z|d| _W n ty0   tdw t	| jt
rPd	|v rF| j| j  d S t| j | _d S d S )
Nr   
cloudfrontAWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEYAWS_SESSION_TOKEN)aws_access_key_idaws_secret_access_keyaws_session_tokenDISTRIBUTION_IDzIThe setting 'WAGTAILFRONTENDCACHE' requires the object 'DISTRIBUTION_ID'.	HOSTNAMES)boto3super__init__clientgetpopcloudfront_distribution_idKeyErrorr   
isinstancedict	hostnamesextendkeyslist)selfparamsr   	__class__ r/var/www/html/ndineBlogger/venv/lib/python3.10/site-packages/wagtail/contrib/frontend_cache/backends/cloudfront.pyr      s(   zCloudfrontBackend.__init__c                 C   s   t t}|D ]8}t|}d }t| jtr1tdtd |j}|| jv r)| j	|}nt
d|| n| j}|r>|| |j q| D ]\}}| |t| qCd S )NzpUsing a `DISTRIBUTION_ID` mapping is deprecated - use `HOSTNAMES` in combination with multiple backends instead.)categoryz[Couldn't purge '%s' from CloudFront. Hostname '%s' not found in the DISTRIBUTION_ID mapping)r   setr   r   r   r   r   r   hostnamer   loggerwarningaddpathitems_create_invalidationr    )r!   urlspaths_by_distribution_idurl
url_parseddistribution_idhostpathsr%   r%   r&   purge_batch/   s0   
zCloudfrontBackend.purge_batchc                 C   s   |  |g d S )N)r7   )r!   r2   r%   r%   r&   purgeM   s   zCloudfrontBackend.purgec                 C   s   dd l }z| jj|t||dtt dd W d S  |jjyH } z|D ]}t	
d|||jd d |jd d  q'W Y d }~d S d }~ww )	Nr   )QuantityItems)PathsCallerReference)DistributionIdInvalidationBatchzPCouldn't purge path '%s' from CloudFront (DistributionId=%s). ClientError: %s %sErrorCodeMessage)botocorer   create_invalidationlenstruuiduuid4
exceptionsClientErrorr*   errorresponse)r!   r4   r6   rB   er-   r%   r%   r&   r/   P   s(   
z&CloudfrontBackend._create_invalidation)__name__
__module____qualname__r   r7   r8   r/   __classcell__r%   r%   r#   r&   r	      s
    )loggingrF   collectionsr   urllib.parser   warningsr   django.core.exceptionsr   wagtail.utils.deprecationr   baser   	getLoggerr*   __all__r	   r%   r%   r%   r&   <module>   s    
