
    Sf                     P    d dl mZ d dlmZ d dlmZ  G d dej                  ZdS )    )settings)models)FieldDoesNotExistc                   J     e Zd ZdZd
 fd	Zd Zd fd	ZddZd Zd	 Z	 xZ
S )HostSiteManagera  
    A model manager to limit objects to those associated with a site.

    :param field_name: the name of the related field pointing at the
                       :class:`~django.contrib.sites.models.Site` model,
                       or a series of relations using the
                       ``field1__field2__field3`` notation. Falls back
                       to looking for 'site' and 'sites' fields.
    :param select_related: a boolean specifying whether to use
                           :meth:`~django.db.models.QuerySet.select_related`
                           when querying the database

    Define a manager instance in your model class with one
    of the following notations::

        on_site = HostSiteManager()  # automatically looks for site and sites
        on_site = HostSiteManager("author__site")
        on_site = HostSiteManager("author__blog__site")
        on_site = HostSiteManager("author__blog__site",
                                  select_related=False)

    Then query against it with one of the manager methods::

        def home_page(request):
            posts = BlogPost.on_site.by_request(request).all()
            return render(request, 'home_page.html', {'posts': posts})

    NTc                     t          t          |                                            || _        || _        d| _        d| _        d S )N   F)superr   __init___field_name_select_related_depth_is_validated)self
field_nameselect_related	__class__s      G/var/www/html/env/lib/python3.11/site-packages/django_hosts/managers.pyr   zHostSiteManager.__init__$   sD    ot$$--///%-"    c                 x   d }| j         u| j                             dd          }t          |          }|| j        k    r|| _        |d         }	 | j        j                            |          }nX# t          $ r Y nLw xY wdD ]E}	 | j        j                            |          }|x| _         }d| _         n# t          $ r d }Y Bw xY w|r9t          |t          j        t          j        f          st          d|z            n1t          | j        j        d|d| j        j        j        d	          d| _        d S )
N__r	   r   )sitesitesTz+%s must be a ForeignKey or ManyToManyField.z couldn't find a field named z in .)r   splitlenr   model_meta	get_fieldr   r   
isinstancer   
ForeignKeyManyToManyField	TypeError
ValueErrorr   __name__object_name)r   field
name_parts	rel_depthr   potential_names         r   _validate_field_namez$HostSiteManager._validate_field_name+   s   ')//a88JJI4;&&'#AJ
(22:>>$    #4   J,66~FFE 5CBD$z)-D&E ) & & &!%JJJ&  	=ef&7&,&<&> ? ? A !35?!@ A A AA
 "n555zzz"j.:::< = = = "s$   A/ /
A<;A<B55CCc                     |t           j        }| j        s|                                  t	          t
          |                                           } |j        di d| j        z  |iS )Nz%s__id__exact )	r   SITE_IDr   r+   r
   r   get_querysetfilterr   )r   site_idqsr   s      r   r/   zHostSiteManager.get_querysetO   sm    ?&G! 	(%%'''?D))6688ryIIOd.>>HIIIr   c                 ,    |                      |          S )z
        Returns a queryset matching the given site id. If not given
        this falls back to the ``SITE_ID`` setting.

        :param site_id: the ID of the site
        :rtype: :class:`~django.db.models.query.QuerySet`
        )r/   )r   r1   s     r   by_idzHostSiteManager.by_idW   s       )))r   c                     t          |d          r|j        |                                 S |                     |j                  S )z
        Returns a queryset matching the given request's site
        attribute.

        :param request: the current request
        :type request: :class:`~django.http.HttpRequest`
        :rtype: :class:`~django.db.models.query.QuerySet`
        r   )hasattrr   noneby_site)r   requests     r   
by_requestzHostSiteManager.by_requesta   s?     w'' 	7<+?99;;||GL)))r   c                 6    |                      |j                  S )z
        Returns a queryset matching the given site.

        :param site: a site instance
        :type site: :class:`~django.contrib.sites.models.Site`
        :rtype: :class:`~django.db.models.query.QuerySet`
        )r4   id)r   r   s     r   r8   zHostSiteManager.by_siten   s     zz$'"""r   )NT)N)r%   
__module____qualname____doc__r   r+   r/   r4   r:   r8   __classcell__)r   s   @r   r   r      s         :# # # # # #"" "" ""HJ J J J J J* * * ** * *# # # # # # #r   r   N)django.confr   	django.dbr   django.core.exceptionsr   Managerr   r-   r   r   <module>rE      s{                      4 4 4 4 4 4p# p# p# p# p#fn p# p# p# p# p#r   