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 g dZG d	d
 d
ejZG dd deZG dd deZdS )    )models)cached_property)gettextInvokeViaAttributeShortcut)Site   )register_setting)BaseGenericSettingBaseSiteSettingr	   c                       sl   e Zd ZdZG dd dZdZedd Zedd Z fd	d
Z	e
dd ZdddZ fddZ  ZS )AbstractSettingz
    The abstract base model for settings. Subclasses must be registered using
    :func:`~wagtail.contrib.settings.registry.register_setting`
    c                   @   s   e Zd ZdZdS )zAbstractSetting.MetaTN__name__
__module____qualname__Zabstract r   r   ^/var/www/lab.imftr.de/x/nb_venv/lib/python3.9/site-packages/wagtail/contrib/settings/models.pyMeta   s   r   Nc                 C   s$   | j  }| jdur |j| j }|S )a  
        Returns a queryset of objects of this type to use as a base.

        You can use the `select_related` attribute on your class to
        specify a list of foreign key field names, which the method
        will attempt to select additional related-object data for
        when the query is executed.

        If your needs are more complex than this, you can override
        this method on your custom class.
        N)objectsallselect_related)clsquerysetr   r   r   base_queryset   s    

zAbstractSetting.base_querysetc                 C   s   d| j j d| j j  S )z
        Returns the name of the attribute that should be used to store
        a reference to the fetched/created object on a request.
        _.)_metaZ	app_labelZ
model_namelower)r   r   r   r   get_cache_attr_name0   s    z#AbstractSetting.get_cache_attr_namec                    s   t  j|i | i | _d S N)super__init___page_url_cache)selfargskwargs	__class__r   r   r!   8   s    zAbstractSetting.__init__c                 C   s
   t | dS )Nget_page_urlr   r#   r   r   r   page_url=   s    zAbstractSetting.page_urlc                 C   sn   || j v r| j | S t| |s2td| jj|t| |}t|dr\|jt| dd}nd}|| j |< |S )a  
        Returns the URL of a page referenced by a foreign key
        (or other attribute) matching the name ``attribute_name``.
        If the field value is null, or links to something other
        than a ``Page`` object, an empty string is returned.
        The result is also cached per-object to facilitate
        fast repeat access.

        Raises an ``AttributeError`` if the object has no such
        field or attribute.
        z!'{}' object has no attribute '{}'specific_requestN )	r"   hasattrAttributeErrorformatr'   r   getattrr+   get_url)r#   Zattribute_namerequestpageurlr   r   r   r(   C   s    





zAbstractSetting.get_page_urlc                    s   t   }|dd  |S )Nr*   r    __getstate__popr#   stater&   r   r   r7   c   s    
zAbstractSetting.__getstate__)N)r   r   r   __doc__r   r   classmethodr   r   r!   r   r*   r(   r7   __classcell__r   r   r&   r   r      s   



 r   c                       s`   e Zd ZejedddejdZG dd dZe	dd Z
 fdd	Ze	d
d Zdd Z  ZS )r   TF)uniqueZdb_indexeditableZ	on_deletec                   @   s   e Zd ZdZdS )zBaseSiteSetting.MetaTNr   r   r   r   r   r   s   s   r   c                 C   sF   |   }t||rt||S t|}| |}||_t||| |S )z
        Get or create an instance of this model for the request,
        and cache the result on the request for faster repeat access.
        )r   r.   r1   r   Zfind_for_requestfor_siter,   setattr)r   r3   	attr_namesiteZsite_settingsr   r   r   for_requestv   s    



zBaseSiteSetting.for_requestc                    s   t   }|dd  |S )Nr,   r6   r9   r&   r   r   r7      s    
zBaseSiteSetting.__getstate__c                 C   s2   |du r|  d|  |  }|j|d\}}|S )zI
        Get or create an instance of this setting for the site.
        Nz %s does not exist for site None.)rC   )ZDoesNotExistr   Zget_or_create)r   rC   r   instancecreatedr   r   r   r@      s
    zBaseSiteSetting.for_sitec                 C   s   t d| jj| jd S )Nz%(site_setting)s for %(site)s)Zsite_settingrC   )r   r   verbose_namerC   r)   r   r   r   __str__   s    zBaseSiteSetting.__str__)r   r   r   r   ZOneToOneFieldr   ZCASCADErC   r   r<   rD   r7   r@   rH   r=   r   r   r&   r   r   j   s   


r   c                   @   s@   e Zd ZdZG dd dZedd ZedddZd	d
 ZdS )r
   ze
    Generic settings are singleton models - only one instance of each model
    can be created.
    c                   @   s   e Zd ZdZdS )zBaseGenericSetting.MetaTNr   r   r   r   r   r      s   r   c                 C   s"   |    }|du r| j S |S )z
        Internal convenience method to get or create the first instance.

        We cannot hardcode `pk=1`, for example, as not all database backends
        use sequential IDs (e.g. Postgres).
        N)r   firstr   create)r   Z	first_objr   r   r   _get_or_create   s    	
z!BaseGenericSetting._get_or_createNc                 C   sN   |du st |tr|  S |  }t||r6t||S |  }t||| |S )a8  
        Get or create an instance of this model. There is only ever one
        instance of models inheriting from `AbstractSetting` so we can
        use `pk=1`.

        If `request_or_site` is present and is a request object, then we cache
        the result on the request for faster repeat access.
        N)
isinstancer   rK   r   r.   r1   rA   )r   Zrequest_or_siterB   objr   r   r   load   s    

zBaseGenericSetting.loadc                 C   s   t | jjS r   )strr   rG   r)   r   r   r   rH      s    zBaseGenericSetting.__str__)N)	r   r   r   r;   r   r<   rK   rN   rH   r   r   r   r   r
      s   
r
   N)Z	django.dbr   Zdjango.utils.functionalr   Zdjango.utils.translationr   r   Zwagtail.coreutilsr   Zwagtail.modelsr   registryr	   __all__ZModelr   r   r
   r   r   r   r   <module>   s   Y4