ó
O'—^c           @   sÉ   d  Z  d d l m Z m Z d d l m Z m Z e d d d d d d	 d
 g ƒ Z e d d d d d d d g ƒ Z d „  Z	 d „  Z
 d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d S(   sý   
Helper function for returning the field information that is associated
with a model class. This includes returning all the forward and reverse
relationships and their associated metadata.

Usage: `get_field_info(model)` returns a `FieldInfo` instance.
iÿÿÿÿ(   t   OrderedDictt
   namedtuple(   t   get_related_modelt   get_remote_fieldt   FieldResultt   pkt   fieldst   forward_relationst   reverse_relationst   fields_and_pkt	   relationst   RelationInfot   model_fieldt   related_modelt   to_manyt   to_fieldt   has_through_modelt   reversec         C   sv   |  j  j j  } t | ƒ } t | ƒ } t | ƒ } t | ƒ } t | | ƒ } t | | ƒ } t | | | | | | ƒ S(   sÏ   
    Given a model class, returns a `FieldInfo` instance, which is a
    `namedtuple`, containing metadata about the various field types on the model
    including information about their relationships.
    (	   t   _metat   concrete_modelt   _get_pkt   _get_fieldst   _get_forward_relationshipst   _get_reverse_relationshipst   _merge_fields_and_pkt   _merge_relationshipst	   FieldInfo(   t   modelt   optsR   R   R   R   R	   t   relationships(    (    sA   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/utils/model_meta.pyt   get_field_info   s    c         C   sM   |  j  } t | ƒ } x1 | rH | j rH t | ƒ j j  } t | ƒ } q W| S(   N(   R   R   t   parent_linkR   R   (   R   R   t   rel(    (    sA   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/utils/model_meta.pyR   2   s    	c         C   sW   t  ƒ  } xG g  |  j D]" } | j r t | ƒ r | ^ q D] } | | | j <q< W| S(   N(   R    R   t	   serializeR   t   name(   R   R   t   field(    (    sA   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/utils/model_meta.pyR   >   s    	9c         C   s   t  |  d d  ƒ o |  j d S(   Nt	   to_fieldsi    (   t   getattrt   NoneR$   (   R#   (    (    sA   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/utils/model_meta.pyt   _get_to_fieldF   s    c         C   s  t  ƒ  } xy g  |  j D]! } | j r t | ƒ r | ^ q D]F } t d | d t | ƒ d t d t | ƒ d t d t ƒ | | j <q; Wxw g  |  j	 D] } | j r’ | ^ q’ D]P } t d | d t | ƒ d t
 d d d t | ƒ j j j d t ƒ | | j <q« W| S(   sD   
    Returns an `OrderedDict` of field names to `RelationInfo`.
    R   R   R   R   R   R   N(   R    R   R!   R   R   R   t   FalseR'   R"   t   many_to_manyt   TrueR&   t   throughR   t   auto_created(   R   R   R#   (    (    sA   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/utils/model_meta.pyR   J   s$    	8,c         C   sl  t  ƒ  } g  |  j D] } | j j s | ^ q } xu | D]m } | j ƒ  } t | d | j ƒ } t d d d | d t	 | j ƒ j
 d t | j ƒ d t d t ƒ | | <q8 Wg  |  j D] } | j j r³ | ^ q³ } x” | D]Œ } | j ƒ  } t | d | j ƒ } t d d d | d t d d d t t	 | j ƒ d d ƒ d k	 oTt	 | j ƒ j j j d t ƒ | | <qØ W| S(	   sD   
    Returns an `OrderedDict` of field names to `RelationInfo`.
    R   R   R   R   R   R   R+   N(   R    t   related_objectsR#   R)   t   get_accessor_nameR%   R   R   R&   R   t   multipleR'   R(   R*   R+   R   R,   (   R   R   t   rt   all_related_objectst   relationt   accessor_namet   relatedt    all_related_many_to_many_objects(    (    sA   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/utils/model_meta.pyR   j   s2    	((!c         C   s1   t  ƒ  } |  | d <|  | |  j <| j | ƒ | S(   NR   (   R    R"   t   update(   R   R   R	   (    (    sA   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/utils/model_meta.pyR   •   s
    	
c         C   s&   t  t |  j ƒ  ƒ t | j ƒ  ƒ ƒ S(   N(   R    t   listt   items(   R   R   (    (    sA   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/utils/model_meta.pyR   ž   s    c         C   s+   t  |  d ƒ o* t  |  j d ƒ o* |  j j S(   s_   
    Given a model class, returns a boolean True if it is abstract and False if it is not.
    R   t   abstract(   t   hasattrR   R9   (   R   (    (    sA   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/utils/model_meta.pyt   is_abstract_model¥   s    N(   t   __doc__t   collectionsR    R   t   rest_framework.compatR   R   R   R   R   R   R   R'   R   R   R   R   R;   (    (    (    sA   /tmp/pip-unpacked-wheel-62FVgP/rest_framework/utils/model_meta.pyt   <module>   s2   					 	+			