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	m
Z
 G dd deZdd	d
Zdd Zdd Zdd ZG dd deZdd Zdd ZdS )    )urlsplit)settings)	force_str)resolve_model_string)PageSitec                   @   s   e Zd ZdS )BadRequestErrorN__name__
__module____qualname__ r   r   S/var/www/lab.imftr.de/x/nb_venv/lib/python3.9/site-packages/wagtail/api/v2/utils.pyr   
   s   r   Nc                 C   sP   t tdd }|d u r,| r,t| }|r,|j}|rLtt|}|jd |j S d S )NZWAGTAILAPI_BASE_URLz://)	getattrr   r   Zfind_for_requestZroot_urlr   r   schemenetloc)requestbase_urlsiteZbase_url_parsedr   r   r   get_base_url   s    
r   c                 C   s"   | dr|S t| pd}|| S )N)zhttp://zhttps:// )
startswithr   )r   pathr   r   r   r   get_full_url   s    
r   c                 C   s   |  ||}|rt||S d S N)Zget_object_detail_urlpathr   )Zrouterr   modelpkZurl_pathr   r   r   get_object_detail_url$   s    r   c                 C   s@   g }|  dD ](}t|}t|ts,td|| qt|S )N,zModel is not a page)splitr   
issubclassr   
ValueErrorappendtuple)stringZpage_modelsZ
sub_stringZ
page_modelr   r   r   page_models_from_string+   s    
r%   c                   @   s   e Zd ZdS )FieldsParameterParseErrorNr	   r   r   r   r   r&   9   s   r&   c                    s:    fddfdddfdd	 \}}|S )	a  
    Parses the ?fields= GET parameter. As this parameter is supposed to be used
    by developers, the syntax is quite tight (eg, not allowing any whitespace).
    Having a strict syntax allows us to extend the it at a later date with less
    chance of breaking anyone's code.

    This function takes a string and returns a list of tuples representing each
    top-level field. Each tuple contains three items:
     - The name of the field (string)
     - Whether the field has been negated (boolean)
     - A list of nested fields if there are any, None otherwise

    Some examples of how this function works:

    >>> parse_fields_parameter("foo")
    [
        ('foo', False, None),
    ]

    >>> parse_fields_parameter("foo,bar")
    [
        ('foo', False, None),
        ('bar', False, None),
    ]

    >>> parse_fields_parameter("-foo")
    [
        ('foo', True, None),
    ]

    >>> parse_fields_parameter("foo(bar,baz)")
    [
        ('foo', False, [
            ('bar', False, None),
            ('baz', False, None),
        ]),
    ]

    It raises a FieldsParameterParseError (subclass of ValueError) if it
    encounters a syntax error
    c                    s   t  t |  S r   )len)Zcurrent_str)
fields_strr   r   get_positionh   s    z,parse_fields_parameter.<locals>.get_positionc                    sB  d}d}d}| r8| d }|dv rh|s:t d| | f |dv r^|dkr^t d| | f ||| fS |d	kr|st d| | f d}n|dv r|r|d
krt d| | f ||7 }nf| s|dkr|d
krt d| | f ||7 }n0| rt d |  nt d| | f d}| dd  } q||| fS )NTFr   r   )()r   z#unexpected char '%s' at position %d*_r*   -r-   r.   z$unexpected whitespace at position %d   )r&   isalnumisspace)r(   
first_charnegatedidentchar)r)   r   r   parse_field_identifierk   sn    










z6parse_fields_parameter.<locals>.parse_field_identifierFc                    sf  d }d}g }| rPd }| \}}} |dv rL|s<t d| |rLt d| | r| d dkr|rpt d |  | dd  dd	\}} |r|}n,|d
kr|rt d|dkr|s|st d||||f | r| d dkr|st d |  || dd  fS | rJ| d dkrJ| dd  } | r:| d dkrJt d |  d}q|r^t d|| fS )NTr,   z"'%s' must be in the first positionz'%s' cannot be negatedr   r*   z"unexpected char '(' at position %dr0   )expect_close_bracketr.   z*negated fields with '_' doesn't make senser-   z-additional fields with '*' doesn't make senser+   z"unexpected char ')' at position %dr   z"unexpected char ',' at position %dFz;unexpected end of input (did you miss out a close bracket?))r&   r"   )r(   r8   Zfirst_identZis_firstfieldsZ
sub_fieldsr5   r4   )r)   r7   parse_fieldsr   r   r:      sd    



z,parse_fields_parameter.<locals>.parse_fields)Fr   )r(   r9   r.   r   )r(   r)   r7   r:   r   parse_fields_parameter=   s
    +CMr;   c                 C   s(   | dv rdS | dv rdS t d|  dS )z
    Parses strings into booleans using the following mapping (case-sensitive):

    'true'   => True
    'false'  => False
    '1'      => True
    '0'      => False
    )true1T)false0Fz$expected 'true' or 'false', got '%s'N)r!   )valuer   r   r   parse_boolean   s
    	rA   )N)urllib.parser   Zdjango.confr   Zdjango.utils.encodingr   Zwagtail.coreutilsr   Zwagtail.modelsr   r   	Exceptionr   r   r   r   r%   r!   r&   r;   rA   r   r   r   r   <module>   s   
 D