a
    Dge                     @   sF   d dl mZ d dlmZ d dlmZ G dd dZG dd deZdS )	    )ImproperlyConfigured)models)resolve_model_stringc                   @   s2   e Zd ZdZdd ZdddZdd	 Zd
d ZdS )ObjectTypeRegistryz
    Implements a lookup table for mapping objects to values according to the object type.
    The most specific type according to the object's inheritance chain is selected.
    c                 C   s   i | _ i | _d S Nvalues_by_exact_classvalues_by_classself r   U/var/www/lab.imftr.de/x/nb_venv/lib/python3.9/site-packages/wagtail/utils/registry.py__init__   s    zObjectTypeRegistry.__init__NFc                 C   s   |r|| j |< n
|| j|< d S r   r   )r   clsvalueexact_classr   r   r   register   s    zObjectTypeRegistry.registerc                 C   sZ   z| j | W S  tyT   | D ],}z| j| W    Y S  tyL   Y q"0 q"Y n0 d S r   )r   KeyErrormror	   )r   r   Zancestorr   r   r   get_by_type   s    zObjectTypeRegistry.get_by_typec                 C   s*   |  |j}t|r&t|ts&||}|S r   )r   	__class__callable
isinstancetype)r   objr   r   r   r   get%   s    zObjectTypeRegistry.get)NF)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r      s
   

r   c                       s6   e Zd ZdZ fddZd
 fdd	Zdd	 Z  ZS )ModelFieldRegistrya  
    Handles the recurring pattern where we need to register different values for different
    model field types, and retrieve the one that most closely matches a given model field,
    according to its type (taking inheritance into account), and in the case of foreign keys,
    the type of the related model (again, taking inheritance into account).

    For example, this is used by wagtail.admin.forms.models when constructing model forms:
    we use such a registry to retrieve the appropriate dict of arguments to pass to the
    form field constructor. A lookup for a models.TextField will return a dict specifying a
    text area widget, and a lookup for a foreign key to Image will return a dict specifying
    an image chooser widget.
    c                    s"   t    | j| jtj< i | _d S r   )superr   foreign_key_lookupr	   r   
ForeignKeyvalues_by_fk_related_modelr
   r   r   r   r   <   s    
zModelFieldRegistry.__init__NFc                    s>   |r(|t jkr|| jt|< q:tdnt j|||d d S )NzTThe 'to' argument on ModelFieldRegistry.register is only valid for ForeignKey fields)r   r   )r   r#   r$   r   r   r!   r   )r   Zfield_classtor   r   r%   r   r   r   D   s    
zModelFieldRegistry.registerc                 C   sP   d }|j j}| D ]}|| jv r| j| } q2qt|rLt|tsL||}|S r   )Zremote_fieldmodelr   r$   r   r   r   )r   fieldr   Ztarget_modelr'   r   r   r   r"   O   s    

z%ModelFieldRegistry.foreign_key_lookup)NNF)r   r   r   r   r   r   r"   __classcell__r   r   r%   r   r    .   s   r    N)Zdjango.core.exceptionsr   Z	django.dbr   Zwagtail.coreutilsr   r   r    r   r   r   r   <module>   s   '