
^Q\}7                 @   s  d  d l  m Z d  d l Z d  d l Z d  d l Z d  d l 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 m Z m Z e j e  Z d	 d
   Z Gd d   d e  Z d d   Z d d   Z d d   Z d S)    )absolute_importN)settings)site)AdminSeleniumTestCase   )DragAndDropAction)
xpath_itemis_sequence
is_integeris_strElementRectc             C   s   d j  |  j j |  j j g  S)N-)joinZ_metaZ	app_label
model_name)m r   J/var/www/dbchiro/venv/build/django-nested-admin/nested_admin/tests/base.py<lambda>   s    r   c                   s*  e  Z d  Z d Z d Z d Z e   f d d    Z d   f d d  Z   f d d   Z	 d	 d
   Z
 d d d d  Z d d   Z d d d  Z d d d  Z d d d  Z d d   Z d d d d d  Z d d   Z d d   Z d d    Z d d! d"  Z d d# d$  Z d d% d&  Z   S)'BaseNestedAdminTestCaseznested_admin.tests.urlsNc                sl   t  t |   j   t j |  j }   f d d       |  |  _  f d d     |  j  |  _ d  S)Nc                sM   |  j  g  g } x7 t |  d d   p' g  D] } | d j   |   q( W| S)NZinlinesr   )modelgetattrappend)Zadmindatainline)descend_admin_inlinesr   r   r   $   s    zABaseNestedAdminTestCase.setUpClass.<locals>.descend_admin_inlinesc                s7   t  |  t  r) d d   t   |   D St |   Sd  S)Nc             S   s   g  |  ] } |  q Sr   r   ).0r   r   r   r   
<listcomp>.   s   	 zYBaseNestedAdminTestCase.setUpClass.<locals>.recursive_map_model_names.<locals>.<listcomp>)
isinstancelistmapget_model_name)r   )recursive_map_model_namesr   r   r!   ,   s    zEBaseNestedAdminTestCase.setUpClass.<locals>.recursive_map_model_names)superr   
setUpClass
admin_site	_registry
root_modelmodelsmodel_names)clsZ
root_admin)	__class__)r   r!   r   r#      s    z"BaseNestedAdminTestCase.setUpClassc                s2   | d  k r |  j  } n  t t |   j |  d  S)N)r&   r"   r   
load_admin)selfobj)r*   r   r   r+   4   s    z"BaseNestedAdminTestCase.load_adminc                s'   t  t |   j   |  j j d  d  S)Nz
            if (window.DJNesting) {
                document.body.addEventListener('mousemove', function(event) {
                    DJNesting.lastMouseMove = event;
                });
            }
        )r"   r   initialize_pageseleniumexecute_script)r,   )r*   r   r   r.   9   s    	z'BaseNestedAdminTestCase.initialize_pagec             C   s   d d   |  j  j d  D } t |  d k rT t j d t j | d d  n  t |  j  j d   } | rx d	 n d
 } |  j	 |  j  j
 d |   | r |  j   |  j   n  d  S)Nc             S   s&   g  |  ] } d  | d k r |  q S)Zfaviconmessager   )r   er   r   r   r   E   s   	 z5BaseNestedAdminTestCase.save_form.<locals>.<listcomp>Zbrowserr   zFound browser errors: %sindent   z/return django.jQuery("[name=_continue]").lengthZ	_continueZ_savez//*[@name="%s"])r/   Zget_loglenloggererrorjsondumpsboolr0   clickfind_element_by_xpathZwait_page_loadedr.   )r,   Zbrowser_errorsZhas_continueZ	name_attrr   r   r   	save_formD   s    #	
z!BaseNestedAdminTestCase.save_formFTc                sZ  g  } t  |  j  \ } } t  | p' g   } | r9 t n t   | r t |  r   | d  r y1 t   f d d   | d d  D  r | SWq Yq Xq n2 y' t   f d d   | D  r | SWn Yn Xd } | r6t |  r&t | d  r&t | d  d k r&| j   d } q@| j d  n
 | s@| Sxt	 |  D]\ } }	 t |  d k rzt
 d   n  |	 d k r| st
 d   n  t |  d k rt
 d	   n  | r| j | d d  n | j d  Pn  t |	  r!t |	  r!t
 d
 t |	  j   n  t |	  rat |  d k rRt
 d |   n  d |	 g }	 n  t |	  rMt |	  d k rt
 d | t |	  f   n  t d d   |	 D  st
 d |   n  |	 \ }
 } | |
 \ } } | r| j | | g  q| j |
 | g  qMqMW| d k	 rV| rF| j | | d  qV| j |  n  | S)aZ
  
        To allow for the case where there are multiple inlines defined on a
        given ModelAdmin or InlineModelAdmin, we need indexes to be a list of
        lists (or at any rate an iterable of iterables, but lets call the
        outermost iterable a list, and the elements themselves tuples).
        The elements of the top-level list should be seen as directions for
        how to descend the hierarchy of the inlines (an empty list thus
        referring to the root model, which is not an inline). Descending down
        the hierarchy, each element is a 2-tuple (a, b) where ``a``
        is the index on the ModelAdmin.inlines attribute, and b is a 0-indexed
        number corresponding to the actual item in that inline currently on
        the page.

        Some operations, such as ``add_inline`` and ``get_num_inlines`` (along
        with the helper method ``get_group``) refer not to a specific item
        within an inline, but to the group of inlines as a whole (at least
        at the last step of descending the hierarchy: for instance when
        clicking add on a nested inline; the add button is at the group-level,
        but the parents of that group are all specific inline items). In the
        case of such operations that operate at the group level, the last item
        is a 1-tuple to indicate which group of the available inlines at that
        level is the target.

        In the trivial case, there is only one inline at every level. Most of
        the unit tests are trivial in this sense. It would be very burdensome,
        not to mention difficult to parse, if the operation::

            self.add_inline(indexes=[1, 3, 2])

        had to be written out as::

            self.add_inline(indexes=[(0, 1), (0, 3), (0, 2), (0, ))])

        The first example above is hard enough to follow without adding a
        number of zeros to each element (and at the end!) to accommodate the
        possibility of there being multiple inline classes when we already now
        that there is only one.

        To ease the burden of writing test cases, it is possible to use an
        integer at a given level of the indexes if there is only one possible
        inline at that level. It is similarly alright to omit the terminal
        1-tuple of (0, ) to ``add_inline`` and ``get_num_inlines`` if there
        is only one possible inline group that can be added to.

        The purposes of this function is to normalize the indexes parameter
        so that it can be converted from the shorter notation to a fully
        qualified one of 2-tuples (and the occasional terminal 1-tuple).
        r   c                s.   g  |  ]$ \ } }   |  o' t  |   q Sr   )r
   )r   ab)inline_type_checkr   r   r      s   	 z>BaseNestedAdminTestCase._normalize_indexes.<locals>.<listcomp>Nc                s.   g  |  ]$ \ } }   |  o' t  |   q Sr   )r
   )r   r>   r?   )r@   r   r   r      s   	 r   z'Indexes depth greater than inline depthz Unexpected None found in indexeszdTerminal index to inline class omitted in group-level operation, but parent has more than one inlinez%Unexpected type %s in list of indexeszRindexes[%d] using shorthand integer value, but more than one inline to choose from   z*Expected indexes[%d] to have len 2, got %dc             S   s   g  |  ] } t  |   q Sr   )r
   )r   ir   r   r   r      s   	 z#indexes[%d] must have only integersrC   rC   rC   )r   r(   r   r
   r5   allr	   popr   	enumerate
ValueErrortype__name__)r,   indexesis_groupZnamed_modelsZnorm_indexesZroot_model_nameZinline_model_namesZgroup_indexlevelZlevel_indexesZinline_indexZinline_itemZinline_model_namer   )r@   r   _normalize_indexesS   sz    1)2z*BaseNestedAdminTestCase._normalize_indexesc          	   C   sn   y | j    WnY t |  } | j d k  rH |  j j d | j  n |  j j d |  | j    Yn Xd S)zBA safe click method that ensures the element is scrolled into viewr   z2document.documentElement.scrollTop += arguments[0]zarguments[0].scrollIntoView()N)r;   r   topr/   r0   )r,   elementZrectr   r   r   r;      s    	zBaseNestedAdminTestCase.clickc             C   s,   t  |  d | d | } | j d |  d  S)Nfrom_indexes
to_indexesscreenshot_hack)r   Zmove_to_target)r,   rP   rQ   rR   actionr   r   r   drag_and_drop_item   s    z*BaseNestedAdminTestCase.drag_and_drop_itemc             C   sd   |  j  | d d } | d } |  j d |  } | j d  } d | | f } |  j j d |  S)	NrK   Tr   rJ   idz#%s .djn-dynamic-form-%szreturn $('%s').lengthrC   )rM   	get_groupget_attributer/   r0   )r,   rJ   r   groupZgroup_idselectorr   r   r   get_num_inlines   s    
z'BaseNestedAdminTestCase.get_num_inlinesc             C   s   |  j  | d d } | j   } g  } x5 | D]- \ } } | d t |  | d f g 7} q. W| d | g 7} d d j |  } |  j j |  S)NrK   Tz
/*[%s][%d]r   z/*[@data-inline-model='%s']z/%s/)rM   rE   r   r   r/   r<   )r,   rJ   r   Z
expr_partsZparent_modelZparent_item_indexexprr   r   r   rV      s    %z!BaseNestedAdminTestCase.get_groupc             C   sa   |  j  |  } | j   \ } } | j |  |  j d |  } | j d t |  | d f  S)NrJ   z.//*[%s][%d]r   )rM   rE   r   rV   r<   r   )r,   rJ   r   Z
item_indexrX   r   r   r   get_item   s
    z BaseNestedAdminTestCase.get_itemc             C   s   |  j  | d d } |  j |  } | d	 } d |  j |  j d  | f } |  j |   } |  j |  Wd  QX| | g | d
 <| d  k	 r |  j d | d | n  | d  k	 r |  j d | d | n  d  S)NrK   Tr   z0#%s .djn-add-item a.djn-add-handler.djn-model-%srU   namerJ   slugrC   rC   )rM   rZ   rV   rW   clickable_selectorr;   	set_field)r,   rJ   r^   r_   Z	new_indexr   Zadd_selectorelr   r   r   
add_inline   s    
z"BaseNestedAdminTestCase.add_inlinec          	   C   sh   |  j  |  } | d d } d |  j |  j d  | f } |  j |   } |  j |  Wd  QXd  S)Nr   r   z$#%s .djn-remove-handler.djn-model-%srU   rC   )rM   r]   rW   r`   r;   )r,   rJ   r   Zremove_selectorrb   r   r   r   remove_inline  s    z%BaseNestedAdminTestCase.remove_inlinec          	   C   s   |  j  |  } | d d } |  j |  j d  } d | | f } |  j |   } |  j |  Wd  QX|  j r d | | f } |  j |  n  d  S)Nr   r   rU   z$#%s .djn-delete-handler.djn-model-%sz2#%s.grp-predelete .grp-delete-handler.djn-model-%srC   )rM   r]   rW   r`   r;   has_grappelliwait_until_clickable_selector)r,   rJ   r   item_iddelete_selectorrb   undelete_selectorr   r   r   delete_inline  s    	z%BaseNestedAdminTestCase.delete_inlinec          	   C   s   |  j  |  } | d d } |  j |  j d  } d | | f } |  j |   } |  j |  Wd  QX|  j r d | | f } |  j |  n  d  S)Nr   r   rU   z$#%s .djn-delete-handler.djn-model-%sz8#%s:not(.grp-predelete) .grp-delete-handler.djn-model-%srC   )rM   r]   rW   r`   r;   re   rf   )r,   rJ   r   rg   ri   rb   rh   r   r   r   undelete_inline  s    	z'BaseNestedAdminTestCase.undelete_inlinec             C   sF   |  j  |  } | s d | S|  j d |  j d  } d | | f S)Nz#id_%srJ   rU   z	#id_%s-%s)rM   r]   rW   )r,   attnamerJ   Zfield_prefixr   r   r   get_form_field_selector)  s
    z/BaseNestedAdminTestCase.get_form_field_selectorc             C   s4   |  j  |  } |  j | d | } |  j j |  S)NrJ   )rM   rm   r/   Zfind_element_by_css_selector)r,   rl   rJ   field_selectorr   r   r   	get_field0  s    z!BaseNestedAdminTestCase.get_fieldc          	   C   sW   |  j  |  } |  j | d | } |  j |   } | j   | j |  Wd  QXd  S)NrJ   )rM   rm   r`   clearZ	send_keys)r,   rl   valuerJ   rn   rb   r   r   r   ra   5  s
    
z!BaseNestedAdminTestCase.set_field)rI   
__module____qualname__Zroot_urlconfr&   Znested_modelsclassmethodr#   r+   r.   r=   rM   r;   rT   rZ   rV   r]   rc   rd   rj   rk   rm   ro   ra   r   r   )r*   r   r      s(   |
r   c             C   s    d t  j k r t j |   S|  S)N	grappelli)r   INSTALLED_APPSunittestexpectedFailure)funcr   r   r   expected_failure_if_grappelli=  s    rz   c             C   s    d t  j k r t j |   S|  S)NZsuit)r   rv   rw   rx   )ry   r   r   r   expected_failure_if_suitC  s    r{   c                s%   t  j      f d d    } | S)Nc                 s.   d t  j k r! t j d   n    |  |   S)Nru   zSkipping (grappelli required))r   rv   rw   ZSkipTest)argskwargs)ry   r   r   wrapperJ  s    z&skip_if_not_grappelli.<locals>.wrapper)	functoolswraps)ry   r~   r   )ry   r   skip_if_not_grappelliI  s    !r   )
__future__r   r   r8   loggingrw   Zdjango.confr   Zdjango.contrib.admin.sitesr   r$   Zdjango_admin_testutilsr   Z	drag_dropr   utilsr   r	   r
   r   r   	getLoggerrI   r6   r    r   rz   r{   r   r   r   r   r   <module>   s    ( '