o
    h%                     @   s   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mZmZmZ d dlmZ d d	lmZ d d
lmZ d ZdZdZdZdd ZG dd dejZeddZdZ dZ!G dd dej"Z#dS )    )
namedtuple)apps)settings)cache)ValidationError)models)CaseIntegerFieldQWhen)Lower)split_domain_port)gettext_lazy         c              	   C   s   t d}t|jjtt| |tdt| dtdtdt	dt
t ddt| dtdd	B d
d}|rYt|dksF|d jttfv rJ|d S |d jt	krY|t|dk S | )zCReturn the wagtailcore.Site object for the given hostname and port.zwagtailcore.Site)hostnameportthenT)r   is_default_siter   )r   r   )defaultoutput_field)match)r   r   r   	root_pager   r   r   )r   	get_modellistobjectsannotater   r   MATCH_HOSTNAME_PORTMATCH_HOSTNAME_DEFAULTMATCH_DEFAULTMATCH_HOSTNAMEr	   filterr
   order_byselect_relatedlenr   DoesNotExist)r   r   Sitesites r*   T/var/www/html/ndineBlogger/venv/lib/python3.10/site-packages/wagtail/models/sites.pyget_site_for_hostname   s4   

r,   c                       s$   e Zd Z fddZdd Z  ZS )SiteManagerc                    s   t   tdS )Nr   )superget_querysetr$   r   self	__class__r*   r+   r/   A   s   zSiteManager.get_querysetc                 C   s   | j ||dS )Nr   r   )get)r1   r   r   r*   r*   r+   get_by_natural_keyD   s   zSiteManager.get_by_natural_key)__name__
__module____qualname__r/   r6   __classcell__r*   r*   r2   r+   r-   @   s    r-   SiteRootPathz(site_id root_path root_url language_codewagtail_site_root_pathsc                       s   e Zd ZejeddddZejeddeddZejed	dded
dZ	ej
deddejdZejeddeddZe ZG dd dZdd Zdd Zdd Zedd Zedd Zedd  Zd( fd"d#	Zed$d% Zed&d' Z  ZS ))r(   r      T)verbose_name
max_lengthdb_indexr   P   zSet this to something other than 80 if you need a specific port number to appear in URLs (e.g. development on port 8000). Does not affect request handling (so port forwarding still works).)r>   r   	help_textz	site namez!Human-readable name for the site.)r>   r?   blankrB   Pagez	root pagesites_rooted_here)r>   related_name	on_deletezis default siteFzjIf true, this site will handle requests for all other hostnames that do not have a site entry of their ownc                   @   s    e Zd ZdZedZedZdS )z	Site.Metar4   siter)   N)r7   r8   r9   unique_together_r>   verbose_name_pluralr*   r*   r*   r+   Metaq   s    rL   c                 C   s   | j | jfS Nr4   r0   r*   r*   r+   natural_keyv   s   zSite.natural_keyc                 C   sX   d td}| jr| j| jr| S d S | j| jdkrdnd| j  | jr)| S d S )Nz [{}]r    rA   z:%d)formatrJ   	site_namer   r   r   )r1   default_suffixr*   r*   r+   __str__y   s   zSite.__str__c                 C   s   | j  | _ d S rM   )r   lowerr0   r*   r*   r+   clean   s   z
Site.cleanc                 C   s2   | du rdS t | dst| }t| d| | jS )a+  
        Find the site object responsible for responding to this HTTP
        request object. Try:

        * unique hostname first
        * then hostname and port
        * if there is no matching hostname at all, or no matching
          hostname:port combination, fall back to the unique default site,
          or raise an exception

        NB this means that high-numbered ports on an extant hostname may
        still be routed to a different hostname which is set as the default

        The site will be cached via request._wagtail_site
        N_wagtail_site)hasattrr(   _find_for_requestsetattrrV   )requestrH   r*   r*   r+   find_for_request   s   

zSite.find_for_requestc                 C   sD   t |  d }|  }d }zt||}W |S  tjy!   Y |S w )Nr   )r   _get_raw_hostget_portr,   r(   r'   )rZ   r   r   rH   r*   r*   r+   rX      s   zSite._find_for_requestc                 C   s8   | j dkr
d| j S | j dkrd| j S d| j| j f S )NrA   z	http://%si  z
https://%szhttp://%s:%d)r   r   r0   r*   r*   r+   root_url   s
   



zSite.root_urlNc                    s|   t  | z	tjjdd}W n tjy   Y d S  tjy"    w | jr:| j|jkr<t	dt
dd|ji gid S d S )NTr   r   zu%(hostname)s is already configured as the default site. You must unset that before you can save this site as default.r   )r.   clean_fieldsr(   r   r5   r'   MultipleObjectsReturnedr   pkr   rJ   r   )r1   excluder   r2   r*   r+   r_      s(   zSite.clean_fieldsc                     s   t jttd} | du rSg } tjdddddD ]. tt	dd	r6| 
 fd
d jjdddD  q| t j jj j jjj qt jt| dtd | S dd | D } | S )aW  
        Return a list of `SiteRootPath` instances, most specific path
        first - used to translate url_paths into actual URLs with hostnames.

        Each root path is an instance of the `SiteRootPath` named tuple,
        and have the following attributes:

        - ``site_id`` - The ID of the Site record
        - ``root_path`` - The internal URL path of the site's home page (for example '/home/')
        - ``root_url`` - The scheme/domain name of the site (for example 'https://www.example.com/')
        - ``language_code`` - The language code of the site (for example 'en')
        versionNr   root_page__localez-root_page__url_pathz-is_default_siter   WAGTAIL_I18N_ENABLEDFc                    s$   g | ]}t  j|j j|jjqS r*   )r;   idurl_pathr^   localelanguage_code).0r   rH   r*   r+   
<listcomp>   s    z,Site.get_site_root_paths.<locals>.<listcomp>T)	inclusiveri   i  c                 S   s   g | ]}t | qS r*   )r;   )rk   resultr*   r*   r+   rm     s    )r   r5   SITE_ROOT_PATHS_CACHE_KEYSITE_ROOT_PATHS_CACHE_VERSIONr(   r   r%   r$   getattrr   extendr   get_translationsappendr;   rg   rh   r^   ri   rj   set)ro   r*   rl   r+   get_site_root_paths   sH   

	zSite.get_site_root_pathsc                   C   s   t jttd d S )Nrc   )r   deleterp   rq   r*   r*   r*   r+   clear_site_root_paths_cache  s   z Site.clear_site_root_paths_cacherM   )r7   r8   r9   r   	CharFieldrJ   r   r	   r   rQ   
ForeignKeyCASCADEr   BooleanFieldr   r-   r   rL   rN   rS   rU   staticmethodr[   rX   propertyr^   r_   rw   ry   r:   r*   r*   r2   r+   r(   O   s\    




=r(   N)$collectionsr   django.appsr   django.confr   django.core.cacher   django.core.exceptionsr   	django.dbr   django.db.modelsr   r	   r
   r   django.db.models.functionsr   django.http.requestr   django.utils.translationr   rJ   r   r    r!   r"   r,   Managerr-   r;   rp   rq   Modelr(   r*   r*   r*   r+   <module>   s(    -
