o
    #i                     @   sp   d dl mZ d dlmZ d dlmZ ddlmZ eeddZ	G dd	 d	eZ
G d
d de
Zdd Zdd ZdS )    )settings)get_object_or_404)
LazyObject   )reverse_hostHOST_SITE_TIMEOUTi  c                       s$   e Zd Z fddZdd Z  ZS )LazySitec                    s&   t    | j|jj||d d S )N)nameargskwargs)super__init____dict__updatehostr	   )selfrequestr
   r   	__class__ ]/var/www/GraceOrthoHospital/venv_grace/lib/python3.10/site-packages/django_hosts/callbacks.pyr      s   
zLazySite.__init__c                 C   s6   t | j| j| jd}ddlm} t||d}|| _d S )Nr
   r   r   Sitedomain__iexact)r   r	   r
   r   django.contrib.sites.modelsr   r   _wrapped)r   r   r   siter   r   r   _setup   s   
zLazySite._setup)__name__
__module____qualname__r   r   __classcell__r   r   r   r   r   
   s    r   c                   @   s   e Zd Zdd ZdS )CachedLazySitec                 C   sv   t | j| j| jd}d| }ddlm} ||d }|d ur#|| _d S ddlm	} t
||d}|||t || _d S )Nr   zhosts:%sr   )cacher   r   )r   r	   r
   r   django.core.cacher%   getr   r   r   r   setr   )r   r   	cache_keyr%   r   r   r   r   r   r      s   
zCachedLazySite._setupN)r    r!   r"   r   r   r   r   r   r$      s    r$   c                 O      t | g|R i || _dS )a  
    A callback function which uses the :mod:`django.contrib.sites` contrib
    app included in Django to match a host to a
    :class:`~django.contrib.sites.models.Site` instance, setting a
    ``request.site`` attribute on success.

    :param request: the request object passed from the middleware
    :param \*args: the parameters as matched by the host patterns
    :param \*\*kwargs: the keyed parameters as matched by the host patterns

    It's important to note that this uses
    :func:`~django_hosts.resolvers.reverse_host` behind the scenes to
    reverse the host with the given arguments and keyed arguments to
    enable a flexible configuration of what will be used to retrieve
    the :class:`~django.contrib.sites.models.Site` instance -- in the end
    the callback will use a ``domain__iexact`` lookup to get it.

    For example, imagine a host conf with a username parameter::

        from django.conf import settings
        from django_hosts import patterns, host

        settings.PARENT_HOST = 'example.com'

        host_patterns = patterns('',
            host(r'www', settings.ROOT_URLCONF, name='www'),
            host(r'(?P<username>\w+)', 'path.to.custom_urls',
                 callback='django_hosts.callbacks.host_site',
                 name='user-sites'),
        )

    When requesting this website with the host ``jezdez.example.com``,
    the callback will act as if you'd do::

        request.site = Site.objects.get(domain__iexact='jezdez.example.com')

    ..since the result of calling :func:`~django_hosts.resolvers.reverse_host`
    with the username ``'jezdez'`` is ``'jezdez.example.com'``.

    Later, in your views, you can nicely refer to the current site
    as ``request.site`` for further site-specific functionality.
    N)r   r   r   r
   r   r   r   r   	host_site+   s   +r,   c                 O   r*   )a  
    A callback function similar to :func:`~django_hosts.callbacks.host_site`
    which caches the resulting :class:`~django.contrib.sites.models.Site`
    instance in the default cache backend for the time specfified as
    :attr:`~django.conf.settings.HOST_SITE_TIMEOUT`.

    :param request: the request object passed from the middleware
    :param \*args: the parameters as matched by the host patterns
    :param \*\*kwargs: the keyed parameters as matched by the host patterns
    N)r$   r   r+   r   r   r   cached_host_siteY   s   r-   N)django.confr   django.shortcutsr   django.utils.functionalr   	resolversr   getattrr   r   r$   r,   r-   r   r   r   r   <module>   s    .