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 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                   @      e Zd ZdZdS )zAbstractSetting.MetaTN__name__
__module____qualname__abstract r   r   _/var/www/html/ndineBlogger/venv/lib/python3.10/site-packages/wagtail/contrib/settings/models.pyMeta       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.
        _.)_meta	app_label
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| |st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   getattrr0   get_url)r(   attribute_namerequestpageurlr   r   r   r-   C   s   





zAbstractSetting.get_page_urlc                       t   }|dd  |S )Nr/   r%   __getstate__popr(   stater+   r   r   r>   c      
zAbstractSetting.__getstate__r$   )r   r   r   __doc__r   r   classmethodr   r#   r&   r   r/   r-   r>   __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)uniquedb_indexeditable	on_deletec                   @   r   )zBaseSiteSetting.MetaTNr   r   r   r   r   r   s   r   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#   r3   r6   r   find_for_requestfor_siter1   setattr)r   r9   	attr_namesitesite_settingsr   r   r   for_requestv   s   



zBaseSiteSetting.for_requestc                    r<   )Nr1   r=   r@   r+   r   r   r>      rB   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.)rN   )DoesNotExistr   get_or_create)r   rN   r   instancecreatedr   r   r   rK      s
   zBaseSiteSetting.for_sitec                 C   s   t d| jj| jd S )Nz%(site_setting)s for %(site)s)site_settingrN   )r   r   verbose_namerN   r.   r   r   r   __str__   s   zBaseSiteSetting.__str__)r   r   r   r   OneToOneFieldr   CASCADErN   r   rD   rP   r>   rK   rW   rE   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                   @   r   )zBaseGenericSetting.MetaTNr   r   r   r   r   r      r   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   	first_objr   r   r   _get_or_create   s   	
z!BaseGenericSetting._get_or_createNc                 C   sN   |du s	t |tr|  S |  }t||rt||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   r]   r#   r3   r6   rL   )r   request_or_siterM   objr   r   r   load   s   

zBaseGenericSetting.loadc                 C   s   t | jjS r$   )strr   rV   r.   r   r   r   rW      s   zBaseGenericSetting.__str__r$   )	r   r   r   rC   r   rD   r]   ra   rW   r   r   r   r   r
      s    
r
   N)	django.dbr   django.utils.functionalr   django.utils.translationr   r   wagtail.coreutilsr   wagtail.modelsr   registryr	   __all__Modelr   r   r
   r   r   r   r   <module>   s    Y4