ó
?()Qc           @   s  d  d l  m Z d  d l m Z d  d l m Z d  d l m Z m Z m	 Z	 m
 Z
 d  d l m Z m Z d  d l m Z m Z d  d l m Z m Z m Z m Z m Z d g Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z  d „  Z! d S(   iÿÿÿÿ(   t   ImproperlyConfigured(   t   models(   t   FieldDoesNotExist(   t   BaseModelFormt   BaseModelFormSett   fields_for_modelt   _get_foreign_key(   t
   ListFiltert   FieldListFilter(   t   get_fields_from_patht   NotRelationField(   t   flatten_fieldsetst   BaseModelAdmint
   ModelAdmint
   HORIZONTALt   VERTICALt   validatec         C   s˜  t  j ƒ  | j } t |  | ƒ t |  d ƒ r8t |  d |  j ƒ xó t |  j ƒ D]ß \ } } t | ƒ sR t |  | ƒ s1t | | ƒ sÞ y | j	 | ƒ Wq+t  j
 k
 rÚ t d |  j | | |  j | j j f ƒ ‚ q+Xq.t |  | | d | | ƒ } t | t  j ƒ r.t d |  j | | f ƒ ‚ q.q1qR qR Wn  t |  d ƒ r«t |  d |  j ƒ xN t |  j ƒ D]: \ } } | |  j k rjt d |  j | | f ƒ ‚ qjqjWn  t |  d ƒ rt |  d |  j ƒ xFt |  j ƒ D]2\ } } t | ƒ rmt | t  j ƒ rmt | t ƒ s9t d |  j | | j f ƒ ‚ n  t | t ƒ rt d	 |  j | | j f ƒ ‚ qqÝt | t t f ƒ rÂ| \ } } t | t ƒ sÈt d
 |  j | | j f ƒ ‚ qÈn | } y t | | ƒ WqÝt t
 f k
 rt d |  j | | f ƒ ‚ qÝXqÝWn  t |  d ƒ rNt |  j t ƒ rNt d |  j ƒ ‚ n  t |  d ƒ r†t |  j t ƒ r†t d |  j ƒ ‚ n  t |  d ƒ rú|  j rút |  d |  j ƒ xFt |  j ƒ D]2\ } } y | j | ƒ d } Wn? t  j
 k
 r%t d |  j | | | j j  | j f ƒ ‚ n X| |  j k rTt d |  j | | f ƒ ‚ n  | |  j k r…t d | |  j |  j f ƒ ‚ n  |  j rË|  j d |  j k rËt d |  j | |  j d f ƒ ‚ n  | j! sÁt d |  j | | f ƒ ‚ qÁqÁWn  t |  d ƒ rt |  d |  j" ƒ n  |  j# rwt	 |  | | d |  j# ƒ } t | t  j$ t  j% f ƒ swt d |  j ƒ ‚ qwn  |  j& rGt |  d |  j& ƒ x± t |  j& ƒ D] \ } } | d k ræt' |  j& ƒ d k ræt d |  j ƒ ‚ n  | d k røq£n  | j( d ƒ r| d } n  d | k r&q£n  t	 |  | | d  | | ƒ q£Wn  t |  d! ƒ rit) |  | | ƒ n  xB d+ D]: }	 t t* |  |	 ƒ t+ ƒ spt d% |  j |	 f ƒ ‚ qpqpWt |  d& ƒ r”t |  d& |  j, ƒ xÁ t |  j, ƒ D]­ \ } }
 t |
 t- ƒ st d' |  j | f ƒ ‚ n  |
 j. s<t d( |  j | f ƒ ‚ n  t |
 j. t  j/ ƒ smt d) |  j | f ƒ ‚ n  t |
 |
 j. ƒ t0 |
 |  | ƒ qàWn  d* S(,   sÛ   
    Does basic ModelAdmin option validation. Calls custom validation
    classmethod in the end if it is provided in cls. The signature of the
    custom validation classmethod should be: def validate(cls, model).
    t   list_displaysY   %s.list_display[%d], %r is not a callable or an attribute of %r or found in the model %r.s   list_display[%d]sH   '%s.list_display[%d]', '%s' is a ManyToManyField which is not supported.t   list_display_linkssR   '%s.list_display_links[%d]' refers to '%s' which is not defined in 'list_display'.t   list_filtersE   '%s.list_filter[%d]' is '%s' which is not a descendant of ListFilter.sf   '%s.list_filter[%d]' is '%s' which is of type FieldListFilter but is not associated with a field name.sE   '%s.list_filter[%d][1]' is '%s' which is not of type FieldListFilter.sD   '%s.list_filter[%d]' refers to '%s' which does not refer to a Field.t   list_per_pages'   '%s.list_per_page' should be a integer.t   list_max_show_alls,   '%s.list_max_show_all' should be an integer.t   list_editablei    sE   '%s.list_editable[%d]' refers to a field, '%s', not defined on %s.%s.sM   '%s.list_editable[%d]' refers to '%s' which is not defined in 'list_display'.sE   '%s' cannot be in both '%s.list_editable' and '%s.list_display_links's}   '%s.list_editable[%d]' refers to the first field in list_display, '%s', which can't be used unless list_display_links is set.sW   '%s.list_editable[%d]' refers to a field, '%s', which isn't editable through the admin.t   search_fieldst   date_hierarchysI   '%s.date_hierarchy is neither an instance of DateField nor DateTimeField.t   orderingt   ?i   s‚   '%s.ordering' has the random ordering marker '?', but contains other fields as well. Please either remove '?' or the other fields.t   -t   __s   ordering[%d]t   readonly_fieldst   list_select_relatedt   save_ast   save_on_tops   '%s.%s' should be a boolean.t   inliness6   '%s.inlines[%d]' does not inherit from BaseModelAdmin.s4   'model' is a required attribute of '%s.inlines[%d]'.s:   '%s.inlines[%d].model' does not inherit from models.Model.N(   R   R   R    (1   R   t   get_appst   _metat   validate_baset   hasattrt   check_isseqR   t	   enumeratet   callablet	   get_fieldR   R    t   __name__t   object_namet
   fetch_attrt
   isinstancet   ManyToManyFieldR   R   t   Fieldt
   issubclassR   R   t   tuplet   listR	   R
   R   t   intR   R   t   get_field_by_namet	   app_labelt   editableR   R   t	   DateFieldt   DateTimeFieldR   t   lent
   startswitht   check_readonly_fieldst   getattrt   boolR!   R   t   modelt   Modelt   validate_inline(   t   clsR>   t   optst   idxt   fieldt   ft   itemt   list_filter_classt
   field_namet   attrt   inline(    (    s8   ../Django//lib/python/django/contrib/admin/validation.pyR      sÞ    
	,& !"")  	 		!!	c      
   C   s­  |  j  rX t |  |  j |  j j d |  j  ƒ } t | t j ƒ sX t d |  j ƒ ‚ qX n  t	 | |  j d |  j  d t
 ƒ} t |  j t ƒ s¡ t d |  j ƒ ‚ n  t |  d d  ƒ } | d  k	 rå t | t ƒ rå t d |  j ƒ ‚ n  t |  d ƒ rt |  j t ƒ rt d |  j ƒ ‚ n  t |  d	 ƒ r~|  j r~| r~| j |  j k r~t d
 |  j | j | j j | j f ƒ ‚ q~n  t |  d ƒ r©t |  |  j |  j j ƒ n  d  S(   Nt   fk_names4   '%s.fk_name is not an instance of models.ForeignKey.t   can_fails   '%s.extra' should be a integer.t   max_nums4   '%s.max_num' should be an integer or None (default).t   formsets4   '%s.formset' does not inherit from BaseModelFormSet.t   excludesU   %s cannot exclude the field '%s' - this is the foreign key to the parent model %s.%s.R   (   RK   R)   R>   R#   R-   R   t
   ForeignKeyR    R*   R   t   Truet   extraR3   R<   t   NoneR%   R0   RN   R   RO   t   nameR5   R;   (   RA   t   parentt   parent_modelRE   t   fkRM   (    (    s8   ../Django//lib/python/django/contrib/admin/validation.pyR@   ¿   s,    	$!"+c      	   C   sã   xÜ | D]Ô } t  | ƒ t k r+ | f } n  x­ | D]¥ } | |  j k rM q2 n  t |  | | | | ƒ y | j | ƒ } Wn t j k
 r q2 n Xt | t j ƒ r2 | j	 j
 j j r2 t d |  j | | | f ƒ ‚ q2 q2 Wq Wd S(   sJ  
    Validate the fields specification in `flds` from a ModelAdmin subclass
    `cls` for the `model` model. `opts` is `model`'s Meta inner class.
    Use `label` for reporting problems to the user.

    The fields specification can be a ``fields`` option or a ``fields``
    sub-option from a ``fieldsets`` option component.
    sg   '%s.%s' can't include the ManyToManyField field '%s' because '%s' manually specifies a 'through' model.N(   t   typeR1   R   t   check_formfieldR)   R   R   R-   R.   t   relt   throughR#   t   auto_createdR    R*   (   RA   R>   RB   t   fldst   labelt   fieldsRD   RE   (    (    s8   ../Django//lib/python/django/contrib/admin/validation.pyt   validate_fields_specä   s    	%c   	   
   C   s
  | j  } t |  d ƒ r  t |  d |  j ƒ xr t |  j ƒ D]^ \ } } t |  | | d | ƒ } t | t j t j	 f ƒ s; t
 d |  j | | f ƒ ‚ q; q; Wn  |  j r1t |  d |  j ƒ t |  | | |  j d ƒ |  j rô t
 d |  j ƒ ‚ n  t |  j ƒ t t |  j ƒ ƒ k r1t
 d |  j ƒ ‚ q1n  |  j r[t |  d |  j ƒ xÅ t |  j ƒ D]´ \ } } t |  d | | ƒ t | ƒ d k r«t
 d	 |  j | f ƒ ‚ n  t |  d
 | | d ƒ d | d k rït
 d |  j | f ƒ ‚ n  t |  | | | d d d | ƒ q]Wt |  j ƒ } t | ƒ t t | ƒ ƒ k r[t
 d |  j ƒ ‚ q[n  |  j rt |  d |  j ƒ xT |  j D]I } t |  | | d | ƒ y | j | ƒ } Wqt j k
 rÉqqXqWt |  j ƒ t t |  j ƒ ƒ k rt
 d |  j ƒ ‚ qn  t |  d ƒ rCt |  j t ƒ rCt
 d |  j ƒ ‚ n  t |  d ƒ rÎt |  d |  j ƒ xf t |  j ƒ D]R \ } } t |  | | d | ƒ } t | t j	 ƒ sut
 d |  j | f ƒ ‚ ququWn  t |  d ƒ rYt |  d |  j ƒ xf t |  j ƒ D]R \ } } t |  | | d | ƒ } t | t j	 ƒ s t
 d |  j | f ƒ ‚ q q Wn  t |  d ƒ rt |  d |  j ƒ x |  j j ƒ  D]‰ \ } } t |  | | d | ƒ } t | t j ƒ pÇ| j sæt
 d |  j | f ƒ ‚ n  | t t f k r‹t
 d |  j | f ƒ ‚ q‹q‹Wn  t |  d ƒ rt |  d |  j  ƒ xÆ |  j  j ƒ  D]² \ } } t |  | | d | ƒ } t | t j! t j t j	 f ƒ r®t
 d |  j | f ƒ ‚ n  t |  d | | ƒ x: t | ƒ D], \ } } t |  | | d | | f | ƒ qÏWqMWn  d  S(   Nt   raw_id_fieldssL   '%s.raw_id_fields[%d]', '%s' must be either a ForeignKey or ManyToManyField.R_   s.   Both fieldsets and fields are specified in %s.s)   There are duplicate field(s) in %s.fieldst	   fieldsetss   fieldsets[%d]i   s6   '%s.fieldsets[%d]' does not have exactly two elements.s   fieldsets[%d][1]i   sC   'fields' key is required in %s.fieldsets[%d][1] field options dict.s   fieldsets[%d][1]['fields']s,   There are duplicate field(s) in %s.fieldsetsRO   s*   There are duplicate field(s) in %s.excludet   forms,   %s.form does not inherit from BaseModelForm.t   filter_verticals3   '%s.filter_vertical[%d]' must be a ManyToManyField.t   filter_horizontals5   '%s.filter_horizontal[%d]' must be a ManyToManyField.t   radio_fieldssW   '%s.radio_fields['%s']' is neither an instance of ForeignKey nor does have choices set.sG   '%s.radio_fields['%s']' is neither admin.HORIZONTAL nor admin.VERTICAL.t   prepopulated_fieldssl   '%s.prepopulated_fields['%s']' is either a DateTimeField, ForeignKey or ManyToManyField. This isn't allowed.s   prepopulated_fields['%s']s   prepopulated_fields['%s'][%d]("   R#   R%   R&   Ra   R'   R)   R-   R   RP   R.   R    R*   R_   R`   Rb   R9   t   sett   check_isdictR   RO   RY   R   R0   Rc   R   Rd   Re   Rf   t   itemst   choicesR   R   Rg   R8   (	   RA   R>   RB   RC   RD   RE   t   fieldsett   flattened_fieldsetst   val(    (    s8   ../Django//lib/python/django/contrib/admin/validation.pyR$     sœ    	 		$	&	$"c         C   s5   t  | t t f ƒ s1 t d |  j | f ƒ ‚ n  d  S(   Ns    '%s.%s' must be a list or tuple.(   R-   R2   R1   R    R*   (   RA   R^   t   obj(    (    s8   ../Django//lib/python/django/contrib/admin/validation.pyR&   p  s    c         C   s/   t  | t ƒ s+ t d |  j | f ƒ ‚ n  d  S(   Ns   '%s.%s' must be a dictionary.(   R-   t   dictR    R*   (   RA   R^   Ro   (    (    s8   ../Django//lib/python/django/contrib/admin/validation.pyRi   t  s    c         C   sW   y | j  | ƒ SWn? t j k
 rR t d |  j | | | j j | j f ƒ ‚ n Xd  S(   Ns@   '%s.%s' refers to field '%s' that is missing from model '%s.%s'.(   R)   R   R   R    R*   R#   R5   (   RA   R>   RB   R^   RD   (    (    s8   ../Django//lib/python/django/contrib/admin/validation.pyR)   x  s
    c         C   sÐ   t  |  j d d  ƒ rZ y |  j j | WqÌ t k
 rV t d |  j | | f ƒ ‚ qÌ Xnr t |  d ƒ ox |  j t	 j k } | sÌ t
 | ƒ } y | | WqÌ t k
 rÈ t d |  j | | f ƒ ‚ qÌ Xn  d  S(   Nt   base_fieldss;   '%s.%s' refers to field '%s' that is missing from the form.t   get_form(   R<   Rc   RS   Rq   t   KeyErrorR    R*   R%   Rr   R   R   (   RA   R>   RB   R^   RD   t   get_form_is_overriddenR_   (    (    s8   ../Django//lib/python/django/contrib/admin/validation.pyRY     s    !c         C   s|   y | j  | ƒ SWn t j k
 r' n Xy t | | ƒ SWn< t k
 rw t d |  j | | | j j | j f ƒ ‚ n Xd  S(   NsT   '%s.%s' refers to '%s' that is neither a field, method or property of model '%s.%s'.(	   R)   R   R   R<   t   AttributeErrorR    R*   R#   R5   (   RA   R>   RB   R^   RD   (    (    s8   ../Django//lib/python/django/contrib/admin/validation.pyR,     s    c         C   s½   t  |  d |  j ƒ x£ t |  j ƒ D]’ \ } } t | ƒ s# t |  | ƒ sµ t | | ƒ s² y | j | ƒ Wq¯ t j k
 r« t d |  j	 | | |  j	 | j
 j f ƒ ‚ q¯ Xq² qµ q# q# Wd  S(   NR   s\   %s.readonly_fields[%d], %r is not a callable or an attribute of %r or found in the model %r.(   R&   R   R'   R(   R%   R)   R   R   R    R*   R#   R+   (   RA   R>   RB   RC   RD   (    (    s8   ../Django//lib/python/django/contrib/admin/validation.pyR;   ›  s    N("   t   django.core.exceptionsR    t	   django.dbR   t   django.db.models.fieldsR   t   django.forms.modelsR   R   R   R   t   django.contrib.adminR   R   t   django.contrib.admin.utilR	   R
   t   django.contrib.admin.optionsR   R   R   R   R   t   __all__R   R@   R`   R$   R&   Ri   R)   RY   R,   R;   (    (    (    s8   ../Django//lib/python/django/contrib/admin/validation.pyt   <module>   s"   "(		±	%	"	j					