a
    5Xhv                     @   s  d dl Z d dlmZ d dlmZ d dlmZ d dlmZm	Z	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 d dlmZmZmZ d dlmZ d dlmZ d dlmZ dd Zdd Z dd Z!dd Z"G dd dZ#G dd de#Z$G dd de#Z%dd Z&dd  Z'd!d" Z(dS )#    N)chain)apps)settings)NotRelationFieldflattenget_fields_from_path)checks)FieldDoesNotExist)models)
LOOKUP_SEP)
Combinable)BaseModelFormBaseModelFormSet_get_foreign_key)engines)DjangoTemplates)import_stringc                 C   s&   zt | |W S  ty    Y dS 0 dS )zX
    issubclass() variant that doesn't raise an exception if cls isn't a
    class.
    FN)
issubclass	TypeError)clsZ	classinfo r   `/var/www/viveiro_mudafortebrasil/venv/lib/python3.9/site-packages/django/contrib/admin/checks.py_issubclass   s    r   c              	   C   sJ   t | }|D ]8}zt |}W n ty2   Y qY n0 t||r dS qdS )z|
    Return whether or not a dotted class path (or a subclass of that class) is
    found in a list of candidate paths.
    TF)r   ImportErrorr   )Z
class_pathZcandidate_pathsr   pathZcandidate_clsr   r   r   _contains_subclass   s    

r   c                 K   s.   ddl m} g }|D ]}|||  q|S )Nr   	all_sites)django.contrib.admin.sitesr   extendcheck)Zapp_configskwargsr   errorssiter   r   r   check_admin_app.   s
    r$   c            	      K   s  ddl m} tdsg S g }d}|D ].\}}t|s&|tjd| d| d q&t D ]}t	|t
r^|j} q|q^d}|s|tjd	d
d nd|jvrtdtjr|tjddd d|jvr|tjddd tdd |D }|rd|jvr|tjddd tdtjs8|tjddd tdtjsZ|tjddd tdtjs~|tjddd d! |S )"zF
    Check that the admin's dependencies are correctly installed.
    r   r   zdjango.contrib.admin))zdjango.contrib.contenttypesi  )zdjango.contrib.authi  )zdjango.contrib.messagesi  zE'%s' must be in INSTALLED_APPS in order to use the admin application.z	admin.E%d)idNzA 'django.template.backends.django.DjangoTemplates' instance must be configured in TEMPLATES in order to use the admin application.z
admin.E403z+django.contrib.auth.context_processors.authz)django.contrib.auth.backends.ModelBackendz'django.contrib.auth.context_processors.auth' must be enabled in DjangoTemplates (TEMPLATES) if using the default auth backend in order to use the admin application.z
admin.E402z3django.contrib.messages.context_processors.messagesz'django.contrib.messages.context_processors.messages' must be enabled in DjangoTemplates (TEMPLATES) in order to use the admin application.z
admin.E404c                 s   s   | ]}|j V  qd S N)Zenable_nav_sidebar).0r#   r   r   r   	<genexpr>z       z%check_dependencies.<locals>.<genexpr>z*django.template.context_processors.requestz'django.template.context_processors.request' must be enabled in DjangoTemplates (TEMPLATES) in order to use the admin navigation sidebar.z
admin.W411z7django.contrib.auth.middleware.AuthenticationMiddlewarezv'django.contrib.auth.middleware.AuthenticationMiddleware' must be in MIDDLEWARE in order to use the admin application.z
admin.E408z4django.contrib.messages.middleware.MessageMiddlewarezs'django.contrib.messages.middleware.MessageMiddleware' must be in MIDDLEWARE in order to use the admin application.z
admin.E409z4django.contrib.sessions.middleware.SessionMiddlewarezs'django.contrib.sessions.middleware.SessionMiddleware' must be in MIDDLEWARE in order to use the admin application.zInsert 'django.contrib.sessions.middleware.SessionMiddleware' before 'django.contrib.auth.middleware.AuthenticationMiddleware'.z
admin.E410)hintr%   )r   r   r   is_installedappendr   Errorr   all
isinstancer   engineZcontext_processorsr   r   ZAUTHENTICATION_BACKENDSanyWarningZ
MIDDLEWARE)	r!   r   r"   Zapp_dependenciesZapp_nameZ
error_coder0   Zdjango_templates_instanceZsidebar_enabledr   r   r   check_dependencies7   s    



		r3   c                   @   s   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7S )8BaseModelAdminChecksc                 K   s   g |  || || || || || || || || || 	|| 
|| || |S r&   )_check_autocomplete_fields_check_raw_id_fields_check_fields_check_fieldsets_check_exclude_check_form_check_filter_vertical_check_filter_horizontal_check_radio_fields_check_prepopulated_fields_check_view_on_site_url_check_ordering_check_readonly_fieldsselfZ	admin_objr!   r   r   r   r       s6    	
zBaseModelAdminChecks.checkc                    sH   t  jttfs tdd ddS tt fddt jD S dS )zV
        Check that `autocomplete_fields` is a list or tuple of model fields.
        a list or tupleautocomplete_fieldsz
admin.E036optionobjr%   c                    s"   g | ]\}}  |d | qS )zautocomplete_fields[%d])_check_autocomplete_fields_itemr'   index
field_namerH   rC   r   r   
<listcomp>   s   
zCBaseModelAdminChecks._check_autocomplete_fields.<locals>.<listcomp>N)r/   rE   listtuplemust_ber   from_iterable	enumeraterC   rH   r   rM   r   r5      s    z/BaseModelAdminChecks._check_autocomplete_fieldsc              	   C   s   z|j j|}W n" ty4   t|||dd Y S 0 |jsXt|tjsXt	d||ddS |j
j|jj }|du rtjd|jj jt|jf |jdd	gS |jstjd
|jjt|jf |jdd	gS g S dS )z
        Check that an item in `autocomplete_fields` is a ForeignKey or a
        ManyToManyField and that the item has a related ModelAdmin with
        search_fields defined.
        z
admin.E037fieldrG   rH   r%   %a foreign key or a many-to-many fieldz
admin.E038rF   NzXAn admin for model "%s" has to be registered to be referenced by %s.autocomplete_fields.z
admin.E039rH   r%   zR%s must define "search_fields", because it's referenced by %s.autocomplete_fields.z
admin.E040)model_meta	get_fieldr	   refer_to_missing_fieldmany_to_manyr/   r
   
ForeignKeyrQ   
admin_site	_registrygetremote_fieldr   r-   __name__type	__class__search_fields)rC   rH   rL   labelrV   Zrelated_adminr   r   r   rI      sJ    z4BaseModelAdminChecks._check_autocomplete_fields_itemc                    sH   t  jttfs tdd ddS tt fddt jD S dS )zZCheck that `raw_id_fields` only contains field names that are listed
        on the model.rD   raw_id_fieldsz
admin.E001rF   c                 3   s$   | ]\}}  |d | V  qdS )zraw_id_fields[%d]N)_check_raw_id_fields_itemrJ   rM   r   r   r(     s   
z<BaseModelAdminChecks._check_raw_id_fields.<locals>.<genexpr>N)r/   rh   rO   rP   rQ   r   rR   rS   rT   r   rM   r   r6   
  s    z)BaseModelAdminChecks._check_raw_id_fieldsc              	   C   sz   z|j j|}W n" ty4   t|||dd Y S 0 |j|krPt|||ddS |jsrt|tj	srt
d||ddS g S dS )zCheck an item of `raw_id_fields`, i.e. check that field named
        `field_name` exists in model `model` and is a ForeignKey or a
        ManyToManyField.z
admin.E002rU   rW   z
admin.E003rF   N)rY   rZ   r[   r	   r\   namer]   r/   r
   r^   rQ   rC   rH   rL   rg   rV   r   r   r   ri     s*    
z.BaseModelAdminChecks._check_raw_id_fields_itemc                    s    j du rg S t j ttfs.tdd ddS  jrHtjd jddgS t	 j }t
|t
t|krztjd	 jd
dgS tt fdd j D S )zCheck that `fields` only refer to existing fields, doesn't contain
        duplicates. Check if at most one of `fields` and `fieldsets` is defined.
        NrD   fieldsz
admin.E004rF   z,Both 'fieldsets' and 'fields' are specified.z
admin.E005rX   z2The value of 'fields' contains duplicate field(s).z
admin.E006c                 3   s   | ]}  |d V  qdS )rl   N_check_field_spec)r'   rL   rM   r   r   r(   V  s   z5BaseModelAdminChecks._check_fields.<locals>.<genexpr>)rl   r/   rO   rP   rQ   	fieldsetsr   r-   re   r   lensetr   rR   )rC   rH   rl   r   rM   r   r7   :  s0    

z"BaseModelAdminChecks._check_fieldsc                    s\    j du rg S t j ttfs.tdd ddS g tt fddt j D S dS )zRCheck that fieldsets is properly formatted and doesn't contain
        duplicates.NrD   ro   z
admin.E007rF   c                 3   s&   | ]\}}  |d | V  qdS )zfieldsets[%d]N)_check_fieldsets_item)r'   rK   fieldsetrH   seen_fieldsrC   r   r   r(   i  s   z8BaseModelAdminChecks._check_fieldsets.<locals>.<genexpr>)ro   r/   rO   rP   rQ   r   rR   rS   rT   r   rt   r   r8   \  s    
z%BaseModelAdminChecks._check_fieldsetsc                    s  t |ttfstd ddS t|dkr:td ddS t |d ts\tdd	  d
dS d|d vrtjd  jddgS t |d d ttfstdd  ddS |	t
|d d  t|tt|krtjd  jddgS tt fdd|d d D S )z|Check an item of `fieldsets`, i.e. check that this is a pair of a
        set name and a dictionary containing "fields" key.rD   z
admin.E008rF      zof length 2z
admin.E009   a dictionary%s[1]z
admin.E010rl   z3The value of '%s[1]' must contain the key 'fields'.z
admin.E011rX   z%s[1]['fields']z(There are duplicate field(s) in '%s[1]'.z
admin.E012c                 3   s    | ]} |d   V  qdS )z%s[1]["fields"]Nrm   )r'   Zfieldset_fieldsrg   rH   rC   r   r   r(     s   z=BaseModelAdminChecks._check_fieldsets_item.<locals>.<genexpr>)r/   rO   rP   rQ   rp   dictr   r-   re   r   r   rq   r   rR   )rC   rH   rs   rg   ru   r   rz   r   rr   q  sF    
z*BaseModelAdminChecks._check_fieldsets_itemc                    s@   t |tr.tt fddt|D S | S dS )z`fields` should be an item of `fields` or an item of
        fieldset[1]['fields'] for any `fieldset` in `fieldsets`. It should be a
        field name or a tuple of field names.c                 3   s(   | ] \}} |d  |f V  qdS )z%s[%d]N)_check_field_spec_itemrJ   rz   r   r   r(     s   z9BaseModelAdminChecks._check_field_spec.<locals>.<genexpr>N)r/   rP   rO   r   rR   rS   r|   )rC   rH   rl   rg   r   rz   r   rn     s    
	z&BaseModelAdminChecks._check_field_specc                 C   st   ||j v rg S z|jj|}W n ty6   g  Y S 0 t|tjrl|jj	jj
sltjd||f |jddgS g S d S )NzvThe value of '%s' cannot include the ManyToManyField '%s', because that field manually specifies a relationship model.z
admin.E013rX   )readonly_fieldsrY   rZ   r[   r	   r/   r
   ManyToManyFieldrb   ZthroughZauto_createdr   r-   re   rk   r   r   r   r|     s&    




z+BaseModelAdminChecks._check_field_spec_itemc                 C   sb   |j du rg S t|j ttfs.tdd|ddS t|j tt|j krZtjd|j	ddgS g S dS )	z4Check that exclude is a sequence without duplicates.NrD   excludez
admin.E014rF   z3The value of 'exclude' contains duplicate field(s).z
admin.E015rX   )
r   r/   rO   rP   rQ   rp   rq   r   r-   re   rT   r   r   r   r9     s    
z#BaseModelAdminChecks._check_excludec                 C   s$   t |jtstdd|ddS g S dS )z)Check that form subclasses BaseModelForm.r   formz
admin.E016parentrG   rH   r%   N)r   r   r   must_inherit_fromrT   r   r   r   r:     s
    z BaseModelAdminChecks._check_formc                    sH   t  jttfs tdd ddS tt fddt jD S dS )z8Check that filter_vertical is a sequence of field names.rD   filter_verticalz
admin.E017rF   c                 3   s$   | ]\}}  |d | V  qdS )zfilter_vertical[%d]N_check_filter_itemrJ   rM   r   r   r(     s   
z>BaseModelAdminChecks._check_filter_vertical.<locals>.<genexpr>N)r/   r   rO   rP   rQ   r   rR   rS   rT   r   rM   r   r;     s    z+BaseModelAdminChecks._check_filter_verticalc                    sH   t  jttfs tdd ddS tt fddt jD S dS )z:Check that filter_horizontal is a sequence of field names.rD   filter_horizontalz
admin.E018rF   c                 3   s$   | ]\}}  |d | V  qdS )zfilter_horizontal[%d]Nr   rJ   rM   r   r   r(     s   
z@BaseModelAdminChecks._check_filter_horizontal.<locals>.<genexpr>N)r/   r   rO   rP   rQ   r   rR   rS   rT   r   rM   r   r<     s    z-BaseModelAdminChecks._check_filter_horizontalc              	   C   sT   z|j j|}W n" ty4   t|||dd Y S 0 |jsLtd||ddS g S dS )zCheck one item of `filter_vertical` or `filter_horizontal`, i.e.
        check that given field exists and is a ManyToManyField.z
admin.E019rU   za many-to-many fieldz
admin.E020rF   N)rY   rZ   r[   r	   r\   r]   rQ   rk   r   r   r   r   	  s    z'BaseModelAdminChecks._check_filter_itemc                    sD   t  jtstdd ddS tt fdd j D S dS )z*Check that `radio_fields` is a dictionary.rx   radio_fieldsz
admin.E021rF   c                 3   s2   | ]*\}}  |d  |d|  V  qdS )r   zradio_fields["%s"]N)_check_radio_fields_key_check_radio_fields_valuer'   rL   valrM   r   r   r(   #  s   
z;BaseModelAdminChecks._check_radio_fields.<locals>.<genexpr>N)r/   r   r{   rQ   rO   r   rR   itemsrT   r   rM   r   r=     s    z(BaseModelAdminChecks._check_radio_fieldsc              	   C   sl   z|j j|}W n" ty4   t|||dd Y S 0 t|tjsd|jsdt	j
d||f |jddgS g S dS )zCheck that a key of `radio_fields` dictionary is name of existing
        field and that the field is a ForeignKey or has `choices` defined.z
admin.E022rU   zsThe value of '%s' refers to '%s', which is not an instance of ForeignKey, and does not have a 'choices' definition.z
admin.E023rX   N)rY   rZ   r[   r	   r\   r/   r
   r^   choicesr   r-   re   rk   r   r   r   r   ,  s     
z,BaseModelAdminChecks._check_radio_fields_keyc                 C   s<   ddl m}m} |||fvr4tjd| |jddgS g S dS )z3Check type of a value of `radio_fields` dictionary.r   )
HORIZONTALVERTICALzDThe value of '%s' must be either admin.HORIZONTAL or admin.VERTICAL.z
admin.E024rX   N)django.contrib.admin.optionsr   r   r   r-   re   )rC   rH   r   rg   r   r   r   r   r   r   D  s    	z.BaseModelAdminChecks._check_radio_fields_valuec                 C   s2   t |js*t|jts*tjd|jddgS g S d S )NzBThe value of 'view_on_site' must be a callable or a boolean value.z
admin.E025rX   )callableZview_on_siter/   boolr   r-   re   rT   r   r   r   r?   U  s    	z,BaseModelAdminChecks._check_view_on_site_urlc                    sD   t  jtstdd ddS tt fdd j D S dS )zXCheck that `prepopulated_fields` is a dictionary containing allowed
        field types.rx   prepopulated_fieldsz
admin.E026rF   c                 3   s2   | ]*\}}  |d  |d|  V  qdS )r   zprepopulated_fields["%s"]N)_check_prepopulated_fields_key _check_prepopulated_fields_valuer   rM   r   r   r(   k  s   
zBBaseModelAdminChecks._check_prepopulated_fields.<locals>.<genexpr>N)r/   r   r{   rQ   rO   r   rR   r   rT   r   rM   r   r>   b  s    z/BaseModelAdminChecks._check_prepopulated_fieldsc              	   C   sp   z|j j|}W n" ty4   t|||dd Y S 0 t|tjtjtj	frht
jd||f |jddgS g S dS )zCheck a key of `prepopulated_fields` dictionary, i.e. check that it
        is a name of existing field and the field is one of the allowed types.
        z
admin.E027rU   zyThe value of '%s' refers to '%s', which must not be a DateTimeField, a ForeignKey, a OneToOneField, or a ManyToManyField.z
admin.E028rX   N)rY   rZ   r[   r	   r\   r/   r
   DateTimeFieldr^   r~   r   r-   re   rk   r   r   r   r   v  s$    
z3BaseModelAdminChecks._check_prepopulated_fields_keyc                    sF   t |ttfstd ddS tt fddt|D S dS )zdCheck a value of `prepopulated_fields` dictionary, i.e. it's an
        iterable of existing fields.rD   z
admin.E029rF   c                 3   s(   | ] \}} |d  |f V  qdS )z%s[%r]N)%_check_prepopulated_fields_value_item)r'   rK   Zsubfield_namerz   r   r   r(     s   zHBaseModelAdminChecks._check_prepopulated_fields_value.<locals>.<genexpr>N)r/   rO   rP   rQ   r   rR   rS   )rC   rH   r   rg   r   rz   r   r     s    z5BaseModelAdminChecks._check_prepopulated_fields_valuec              	   C   s>   z|j j| W n" ty4   t|||dd Y S 0 g S dS )zYFor `prepopulated_fields` equal to {"slug": ("title",)},
        `field_name` is "title".z
admin.E030rU   N)rY   rZ   r[   r	   r\   rC   rH   rL   rg   r   r   r   r     s    z:BaseModelAdminChecks._check_prepopulated_fields_value_itemc                    sV    j du rg S t j ttfs.tdd ddS tt fddt j D S dS )z;Check that ordering refers to existing fields or is random.NrD   orderingz
admin.E031rF   c                 3   s$   | ]\}}  |d | V  qdS )zordering[%d]N)_check_ordering_itemrJ   rM   r   r   r(     s   z7BaseModelAdminChecks._check_ordering.<locals>.<genexpr>)r   r/   rO   rP   rQ   r   rR   rS   rT   r   rM   r   r@     s    
z$BaseModelAdminChecks._check_orderingc              	   C   s   t |ttjfr@t |tjs$| }t |jtjr<|jj}ng S |dkrlt|j	dkrlt
jdd|jddgS |dkrxg S t|v rg S |dr|dd }|d	krg S z|jj| W n" ty   t|||d
d Y S 0 g S dS )z0Check that `ordering` refers to existing fields.?rw   z^The value of 'ordering' has the random ordering marker '?', but contains other fields as well.z2Either remove the "?", or remove the other fields.z
admin.E032)r*   rH   r%   -Npkz
admin.E033rU   )r/   r   r
   ZOrderByZascZ
expressionFrj   rp   r   r   r-   re   r   
startswithrY   rZ   r[   r	   r\   r   r   r   r   r     s:    
	
z)BaseModelAdminChecks._check_ordering_itemc                    sV    j dkrg S t j ttfs.tdd ddS tt fddt j D S dS )	z?Check that readonly_fields refers to proper attribute or field.r   rD   r}   z
admin.E034rF   c                 3   s$   | ]\}}  |d | V  qdS )zreadonly_fields[%d]N)_check_readonly_fields_itemrJ   rM   r   r   r(     s   
z>BaseModelAdminChecks._check_readonly_fields.<locals>.<genexpr>N)r}   r/   rO   rP   rQ   r   rR   rS   rT   r   rM   r   rA     s    
z+BaseModelAdminChecks._check_readonly_fieldsc                 C   s   t |rg S t||rg S t|j|r*g S z|jj| W n: tyv   tjd||jj	|jjj
f |jddg Y S 0 g S d S )NzSThe value of '%s' is not a callable, an attribute of '%s', or an attribute of '%s'.z
admin.E035rX   )r   hasattrrY   rZ   r[   r	   r   r-   re   rc   rg   r   r   r   r   r     s*    

z0BaseModelAdminChecks._check_readonly_fields_itemN)rc   
__module____qualname__r    r5   rI   r6   ri   r7   r8   rr   rn   r|   r9   r:   r;   r<   r   r=   r   r   r?   r>   r   r   r   r@   r   rA   r   r   r   r   r   r4      s6   1",	'r4   c                       s   e Zd Z fddZdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Z  ZS ))ModelAdminChecksc                    s   g t  || || || || || || || || 	|| 
|| || || || || |S r&   )superr    _check_save_as_check_save_on_top_check_inlines_check_list_display_check_list_display_links_check_list_filter_check_list_select_related_check_list_per_page_check_list_max_show_all_check_list_editable_check_search_fields_check_date_hierarchy _check_action_permission_methods_check_actions_uniquenessrB   re   r   r   r      s>    
	
zModelAdminChecks.checkc                 C   s$   t |jtstdd|ddS g S dS )zCheck save_as is a boolean.	a booleansave_asz
admin.E101rF   N)r/   r   r   rQ   rT   r   r   r   r   *  s    zModelAdminChecks._check_save_asc                 C   s$   t |jtstdd|ddS g S dS )zCheck save_on_top is a boolean.r   save_on_topz
admin.E102rF   N)r/   r   r   rQ   rT   r   r   r   r   2  s    z#ModelAdminChecks._check_save_on_topc                    sH   t  jttfs tdd ddS tt fddt jD S dS )z%Check all inline model admin classes.rD   inlinesz
admin.E103rF   c                 3   s$   | ]\}}  |d | V  qdS )zinlines[%d]N)_check_inlines_itemr'   rK   itemrM   r   r   r(   C  s   z2ModelAdminChecks._check_inlines.<locals>.<genexpr>N)r/   r   rO   rP   rQ   r   rR   rS   rT   r   rM   r   r   :  s    zModelAdminChecks._check_inlinesc                 C   s   z|j d |j }W n* ty>   tjd| |jddg Y S 0 ddlm} t||sntjd| |jddgS |j	stjd| |jddgS t|j	t
jstd	d
| |ddS ||j	|j S dS )zCheck one inline model admin..z*'%s' must inherit from 'InlineModelAdmin'.z
admin.E104rX   r   )InlineModelAdminz#'%s' must have a 'model' attribute.z
admin.E105za Modelz%s.modelz
admin.E106rF   N)r   rc   AttributeErrorr   r-   re   r   r   r   rY   r
   ZModelrQ   r_   r    )rC   rH   inlinerg   Zinline_labelr   r   r   r   r   I  s:    

z$ModelAdminChecks._check_inlines_itemc                    sH   t  jttfs tdd ddS tt fddt jD S dS )zBCheck that list_display only contains fields or usable attributes.rD   list_displayz
admin.E107rF   c                 3   s$   | ]\}}  |d | V  qdS )zlist_display[%d]N)_check_list_display_itemr   rM   r   r   r(   x  s   z7ModelAdminChecks._check_list_display.<locals>.<genexpr>N)r/   r   rO   rP   rQ   r   rR   rS   rT   r   rM   r   r   o  s    z$ModelAdminChecks._check_list_displayc                 C   s   t |rg S t||rg S z|jj|}W nd ty   zt|j|}W n@ ty   tj	d|||j
j|jjjf |j
ddg Y  Y S 0 Y n0 t|tjrtj	d| |j
ddgS g S )NzsThe value of '%s' refers to '%s', which is not a callable, an attribute of '%s', or an attribute or method on '%s'.z
admin.E108rX   z0The value of '%s' must not be a ManyToManyField.z
admin.E109)r   r   rY   rZ   r[   r	   getattrr   r   r-   re   rc   rg   r/   r
   r~   )rC   rH   r   rg   rV   r   r   r   r   ~  s<    
	z)ModelAdminChecks._check_list_display_itemc                    sp   ddl m}  jdu rg S t jttfs:tdd ddS  jj|ju rltt	
 fdd	t jD S g S )
zACheck that list_display_links is a unique subset of list_display.r   )
ModelAdminNza list, a tuple, or Nonelist_display_linksz
admin.E110rF   c                 3   s$   | ]\}}  |d | V  qdS )zlist_display_links[%d]N)_check_list_display_links_itemrJ   rM   r   r   r(     s   
z=ModelAdminChecks._check_list_display_links.<locals>.<genexpr>)r   r   r   r/   rO   rP   rQ   Zget_list_display__func__r   rR   rS   )rC   rH   r   r   rM   r   r     s"    
z*ModelAdminChecks._check_list_display_linksc                 C   s.   ||j vr&tjd||f |jddgS g S d S )NzIThe value of '%s' refers to '%s', which is not defined in 'list_display'.z
admin.E111rX   )r   r   r-   re   r   r   r   r   r     s    
	z/ModelAdminChecks._check_list_display_links_itemc                    sH   t  jttfs tdd ddS tt fddt jD S d S )NrD   list_filterz
admin.E112rF   c                 3   s$   | ]\}}  |d | V  qdS )zlist_filter[%d]N)_check_list_filter_itemr   rM   r   r   r(     s   z6ModelAdminChecks._check_list_filter.<locals>.<genexpr>)r/   r   rO   rP   rQ   r   rR   rS   rT   r   rM   r   r     s    z#ModelAdminChecks._check_list_filterc              	   C   s   ddl m}m} t|rft|tjsft||s>td||ddS t	||r`t
jd| |jddgS g S nt|ttfr|\}}t||std	d
| |ddS g S nL|}zt|j| W n2 ttfy   t
jd||f |jddg Y S 0 g S dS )aG  
        Check one item of `list_filter`, i.e. check if it is one of three options:
        1. 'field' -- a basic field filter, possibly w/ relationships (e.g.
           'field__rel')
        2. ('field', SomeFieldListFilter) - a field-based list filter class
        3. SomeListFilter - a non-field list filter class
        r   )FieldListFilter
ListFilterr   z
admin.E113r   z:The value of '%s' must not inherit from 'FieldListFilter'.z
admin.E114rX   r   ry   z
admin.E115zBThe value of '%s' refers to '%s', which does not refer to a Field.z
admin.E116N)Zdjango.contrib.adminr   r   r   r/   r
   ZFieldr   r   r   r   r-   re   rP   rO   r   rY   r   r	   )rC   rH   r   rg   r   r   rV   Zlist_filter_classr   r   r   r     sN    

	

	z(ModelAdminChecks._check_list_filter_itemc                 C   s*   t |jtttfs"tdd|ddS g S dS )z?Check that list_select_related is a boolean, a list or a tuple.za boolean, tuple or listlist_select_relatedz
admin.E117rF   N)r/   r   r   rO   rP   rQ   rT   r   r   r   r     s    z+ModelAdminChecks._check_list_select_relatedc                 C   s$   t |jtstdd|ddS g S dS )z'Check that list_per_page is an integer.
an integerlist_per_pagez
admin.E118rF   N)r/   r   intrQ   rT   r   r   r   r     s
    z%ModelAdminChecks._check_list_per_pagec                 C   s$   t |jtstdd|ddS g S dS )z+Check that list_max_show_all is an integer.r   list_max_show_allz
admin.E119rF   N)r/   r   r   rQ   rT   r   r   r   r   '  s
    z)ModelAdminChecks._check_list_max_show_allc                    sH   t  jttfs tdd ddS tt fddt jD S dS )zjCheck that list_editable is a sequence of editable fields from
        list_display without first element.rD   list_editablez
admin.E120rF   c                 3   s$   | ]\}}  |d | V  qdS )zlist_editable[%d]N)_check_list_editable_itemr   rM   r   r   r(   ;  s   
z8ModelAdminChecks._check_list_editable.<locals>.<genexpr>N)r/   r   rO   rP   rQ   r   rR   rS   rT   r   rM   r   r   1  s    z%ModelAdminChecks._check_list_editablec              	   C   s   z|j j|}W n" ty4   t|||dd Y S 0 ||jvr\tjd||f |jddgS |j	r||j	v rtjd| |jddgS |jd |kr|j	s|j	d urtjd	||jd f |jd
dgS |j
r|jrtjd||f |jddgS g S d S )Nz
admin.E121rU   zKThe value of '%s' refers to '%s', which is not contained in 'list_display'.z
admin.E122rX   zMThe value of '%s' cannot be in both 'list_editable' and 'list_display_links'.z
admin.E123r   z~The value of '%s' refers to the first field in 'list_display' ('%s'), which cannot be used unless 'list_display_links' is set.z
admin.E124zJThe value of '%s' refers to '%s', which is not editable through the admin.z
admin.E125)rY   rZ   r[   r	   r\   r   r   r-   re   r   editableZprimary_keyrk   r   r   r   r   C  s`    
		z*ModelAdminChecks._check_list_editable_itemc                 C   s(   t |jttfs tdd|ddS g S dS )z"Check search_fields is a sequence.rD   rf   z
admin.E126rF   N)r/   rf   rO   rP   rQ   rT   r   r   r   r   y  s
    z%ModelAdminChecks._check_search_fieldsc              	   C   s   |j du rg S zt|j|j d }W n0 ttfyT   tjd|j  |jddg Y S 0 t|t	j
t	jfsxtdd|dd	S g S dS )
z?Check that date_hierarchy refers to DateField or DateTimeField.NzNThe value of 'date_hierarchy' refers to '%s', which does not refer to a Field.z
admin.E127rX   za DateField or DateTimeFielddate_hierarchyz
admin.E128rF   )r   r   rY   r   r	   r   r-   re   r/   r
   Z	DateFieldr   rQ   )rC   rH   rV   r   r   r   r     s*    

	z&ModelAdminChecks._check_date_hierarchyc           	   	   C   sr   |  }g }|D ]\\}}}t|ds&q|jD ]>}d| }t||s,|tjd|jj||jf |jdd q,q|S )z
        Actions with an allowed_permission attribute require the ModelAdmin to
        implement a has_<perm>_permission() method for each permission.
        allowed_permissionszhas_%s_permissionz/%s must define a %s() method for the %s action.z
admin.E129rX   )_get_base_actionsr   r   r,   r   r-   re   rc   )	rC   rH   actionsr"   funcrj   _Z
permissionmethod_namer   r   r   r     s*    


z1ModelAdminChecks._check_action_permission_methodsc                 C   s^   g }t dd | D }| D ]4\}}|dkr$|tjd|jj|f |jdd q$|S )z.Check that every action has a unique __name__.c                 s   s   | ]\}}}|V  qd S r&   r   )r'   r   rj   r   r   r   r(     r)   z=ModelAdminChecks._check_actions_uniqueness.<locals>.<genexpr>rw   zS__name__ attributes of actions defined in %s must be unique. Name %r is not unique.z
admin.E130rX   )	collectionsCounterr   r   r,   r   r-   re   rc   )rC   rH   r"   namesrj   countr   r   r   r     s     z*ModelAdminChecks._check_actions_uniqueness)rc   r   r   r    r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__r   r   r   r   r     s(   &$;

6
r   c                       sP   e Zd Z fddZ fddZdd Zdd Zd	d
 Zdd Zdd Z	  Z
S )InlineModelAdminChecksc                    sV   |j }g t || ||| ||| || || || |S r&   )	parent_modelr   r    _check_relation_check_exclude_of_parent_model_check_extra_check_max_num_check_min_num_check_formset)rC   Z
inline_objr!   r   r   r   r   r      s     


zInlineModelAdminChecks.checkc                    sz   t  |}|rg S | ||r$g S |jd u r2g S t||j|jd}|j|jv rrtj	d|j|j
jf |jddgS g S d S )Nfk_namezVCannot exclude the field '%s', because it is the foreign key to the parent model '%s'.z
admin.E201rX   )r   r9   r   r   r   rY   r   rj   r   r-   rZ   rg   re   )rC   rH   r   r"   Zfkr   r   r   r     s(    
z5InlineModelAdminChecks._check_exclude_of_parent_modelc              
   C   s^   zt ||j|jd W n> tyT } z&tj|jd |jddgW  Y d }~S d }~0 0 g S d S )Nr   r   z
admin.E202rX   )r   rY   r   
ValueErrorr   r-   argsre   )rC   rH   r   er   r   r   r     s
    0z&InlineModelAdminChecks._check_relationc                 C   s$   t |jtstdd|ddS g S dS )zCheck that extra is an integer.r   extraz
admin.E203rF   N)r/   r   r   rQ   rT   r   r   r   r     s    z#InlineModelAdminChecks._check_extrac                 C   s2   |j du rg S t|j ts*tdd|ddS g S dS )z!Check that max_num is an integer.Nr   max_numz
admin.E204rF   )r   r/   r   rQ   rT   r   r   r   r     s
    
z%InlineModelAdminChecks._check_max_numc                 C   s2   |j du rg S t|j ts*tdd|ddS g S dS )z!Check that min_num is an integer.Nr   min_numz
admin.E205rF   )r   r/   r   rQ   rT   r   r   r   r     s
    
z%InlineModelAdminChecks._check_min_numc                 C   s$   t |jtstdd|ddS g S dS )z0Check formset is a subclass of BaseModelFormSet.r   formsetz
admin.E206r   N)r   r   r   r   rT   r   r   r   r     s
    z%InlineModelAdminChecks._check_formset)rc   r   r   r    r   r   r   r   r   r   r   r   r   r   r   r     s   

r   c                 C   s   t jd|| f |j|dgS )NzThe value of '%s' must be %s.rX   r   r-   re   )rd   rG   rH   r%   r   r   r   rQ   *  s    
rQ   c                 C   s   t jd|| f |j|dgS )Nz)The value of '%s' must inherit from '%s'.rX   r   r   r   r   r   r   4  s    
r   c                 C   s$   t jd|| |jjjf |j|dgS )Nz?The value of '%s' refers to '%s', which is not a field of '%s'.rX   )r   r-   rY   rZ   rg   re   rU   r   r   r   r\   >  s    r\   ))r   	itertoolsr   Zdjango.appsr   Zdjango.confr   Zdjango.contrib.admin.utilsr   r   r   Zdjango.corer   Zdjango.core.exceptionsr	   Z	django.dbr
   Zdjango.db.models.constantsr   Zdjango.db.models.expressionsr   Zdjango.forms.modelsr   r   r   Zdjango.templater   Zdjango.template.backends.djangor   Zdjango.utils.module_loadingr   r   r   r$   r3   r4   r   r   rQ   r   r\   r   r   r   r   <module>   s<   	y    j   <[

