a
    Dg%                     @   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}|rt|dks|d jttfv r|d S |d jt	kr|t|dk S | dS )zCReturn the wagtailcore.Site object for the given hostname and port.zwagtailcore.Site)hostnameportthenT)r   is_default_siter   )r   r   )defaultZoutput_field)match)r   r   r   	root_pager   r   r   N)r   Z	get_modellistobjectsZannotater   r   MATCH_HOSTNAME_PORTMATCH_HOSTNAME_DEFAULTMATCH_DEFAULTMATCH_HOSTNAMEr	   filterr
   order_byselect_relatedlenr   DoesNotExist)r   r   Sitesites r'   S/var/www/lab.imftr.de/x/nb_venv/lib/python3.9/site-packages/wagtail/models/sites.pyget_site_for_hostname   s:    

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)r.   r   r   r'   r'   r(   get_by_natural_keyD   s    zSiteManager.get_by_natural_key)__name__
__module____qualname__r,   r3   __classcell__r'   r'   r/   r(   r*   @   s   r*   SiteRootPathz(site_id root_path root_url language_codeZ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_lengthZ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;   Zblankr=   ZPagez	root pageZsites_rooted_here)r:   Zrelated_nameZ	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.Metar1   siter&   N)r4   r5   r6   Zunique_together_r:   Zverbose_name_pluralr'   r'   r'   r(   Metaq   s   r@   c                 C   s   | j | jfS Nr1   r-   r'   r'   r(   natural_keyv   s    zSite.natural_keyc                 C   sX   d td}| jr(| j| jr"|nd S | j| jdkr:dnd| j  | jrN|nd S d S )Nz [{}]r    r<   z:%d)formatr?   	site_namer   r   r   )r.   Zdefault_suffixr'   r'   r(   __str__y   s    zSite.__str__c                 C   s   | j  | _ d S rA   )r   lowerr-   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setattrrI   )requestr>   r'   r'   r(   find_for_request   s    

zSite.find_for_requestc                 C   sD   t |  d }|  }d }zt||}W n tjy>   Y n0 |S )Nr   )r   Z_get_raw_hostZget_portr)   r%   r$   )rM   r   r   r>   r'   r'   r(   rK      s    zSite._find_for_requestc                 C   s<   | j dkrd| j S | j dkr(d| j S d| j| j f S d S )Nr<   z	http://%si  z
https://%szhttp://%s:%d)r   r   r-   r'   r'   r(   root_url   s
    



zSite.root_urlNc                    sz   t  | ztjjdd}W n( tjy2   Y nD tjyF    Y n00 | jrv| j|jkrvt	dt
dd|ji gi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   r2   r$   ZMultipleObjectsReturnedr   pkr   r?   r   )r.   excluder   r/   r'   r(   rP      s$    zSite.clean_fieldsc                     s   t jttd} | du rg } tjdddddD ]\ tt	dd	rl| 
 fd
d jjdddD  q2| t j jj j jjj q2t jt| dtd n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   Zroot_page__localez-root_page__url_pathz-is_default_siter   ZWAGTAIL_I18N_ENABLEDFc                    s$   g | ]}t  j|j j|jjqS r'   )r8   idurl_pathrO   localelanguage_code).0r   r>   r'   r(   
<listcomp>   s   z,Site.get_site_root_paths.<locals>.<listcomp>T)Z	inclusiverW   i  c                 S   s   g | ]}t | qS r'   )r8   )rY   resultr'   r'   r(   r[         )r   r2   SITE_ROOT_PATHS_CACHE_KEYSITE_ROOT_PATHS_CACHE_VERSIONr%   r   r"   r!   getattrr   extendr   Zget_translationsappendr8   rU   rV   rO   rW   rX   set)r\   r'   rZ   r(   get_site_root_paths   sH    
	
zSite.get_site_root_pathsc                   C   s   t jttd d S )NrS   )r   deleter^   r_   r'   r'   r'   r(   clear_site_root_paths_cache  s    z Site.clear_site_root_paths_cache)N)r4   r5   r6   r   Z	CharFieldr?   r   r	   r   rE   Z
ForeignKeyZCASCADEr   ZBooleanFieldr   r*   r   r@   rB   rF   rH   staticmethodrN   rK   propertyrO   rP   rd   rf   r7   r'   r'   r/   r(   r%   O   sZ   




=r%   N)$collectionsr   Zdjango.appsr   Zdjango.confr   Zdjango.core.cacher   Zdjango.core.exceptionsr   Z	django.dbr   Zdjango.db.modelsr   r	   r
   r   Zdjango.db.models.functionsr   Zdjango.http.requestr   Zdjango.utils.translationr   r?   r   r   r   r   r)   Managerr*   r8   r^   r_   ZModelr%   r'   r'   r'   r(   <module>   s&   -
