ó
O'—^c           @   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	 d d l
 m Z m Z d e f d „  ƒ  YZ d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ d S(   iÿÿÿÿ(   t   OrderedDict(   t   ImproperlyConfigured(   t   Polygon(   t   ModelSerializert   ListSerializert   LIST_SERIALIZER_KWARGSi   (   t   GeometryFieldt   GeometrySerializerMethodFieldt   GeoModelSerializerc           B   s   e  Z d  Z RS(   sF   
    Deprecated, will be removed in django-rest-framework-gis 1.0
    (   t   __name__t
   __module__t   __doc__(    (    (    s@   /tmp/pip-unpacked-wheel-aI1tzL/rest_framework_gis/serializers.pyR      s   t   GeoFeatureModelListSerializerc           B   s    e  Z e d  „  ƒ Z d „  Z RS(   c         C   s   t  t |  ƒ j S(   N(   t   superR   t   data(   t   self(    (    s@   /tmp/pip-unpacked-wheel-aI1tzL/rest_framework_gis/serializers.pyR      s    c         C   s(   t  d d t t |  ƒ j | ƒ f f ƒ S(   sQ   
        Add GeoJSON compatible formatting to a serialized queryset list
        t   typet   FeatureCollectiont   features(   R   R   (   R    R   R   t   to_representation(   R   R   (    (    s@   /tmp/pip-unpacked-wheel-aI1tzL/rest_framework_gis/serializers.pyR      s    (   R	   R
   t   propertyR   R   (    (    (    s@   /tmp/pip-unpacked-wheel-aI1tzL/rest_framework_gis/serializers.pyR      s   t   GeoFeatureModelSerializerc           B   sJ   e  Z d  Z e d „  ƒ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(   ss   
    A subclass of ModelSerializer
    that outputs geojson-ready data as
    features and feature collections
    c   	      O   s‘   |  | | Ž  } i | d 6} | j  t g  | j ƒ  D]$ \ } } | t k r2 | | f ^ q2 ƒ ƒ t |  d d  ƒ } t | d t ƒ } | | | Ž  S(   Nt   childt   Metat   list_serializer_class(   t   updatet   dictt   itemsR   t   getattrt   NoneR   (	   t   clst   argst   kwargst   child_serializert   list_kwargst   keyt   valuet   metaR   (    (    s@   /tmp/pip-unpacked-wheel-aI1tzL/rest_framework_gis/serializers.pyt	   many_init&   s    	"c            s\  t  t |  ƒ j | | Ž  t |  d ƒ ‰  d  } |  j j j j j	 } t
 ˆ  d ƒ sb | ˆ  j k rk | } n  t ˆ  d | ƒ ˆ  _ t
 ˆ  d ƒ sš ˆ  j r© t d ƒ ‚ n  ‡  f d †  } ‡  f d †  } | ˆ  j d ƒ | ˆ  j ƒ t ˆ  d d  ƒ ˆ  _ ˆ  j r"| ˆ  j d ƒ | ˆ  j ƒ n  t ˆ  d	 t ƒ ˆ  _ ˆ  j rXˆ  j rXt d
 ƒ ‚ n  d  S(   NR   t   fieldst   id_fieldt	   geo_fields   You must define a 'geo_field'.c            s:   t  ˆ  d ƒ r6 |  ˆ  j k r6 t d j | ƒ ƒ ‚ n  d S(   s#   make sure the field is not excludedt   excludes   You cannot exclude your '{0}'.N(   t   hasattrR*   R   t   format(   t
   field_namet
   field_role(   R%   (    s@   /tmp/pip-unpacked-wheel-aI1tzL/rest_framework_gis/serializers.pyt   check_excludes?   s    c            sa   t  ˆ  d ƒ r] |  ˆ  j k r] t ˆ  j ƒ t k r? |  f } n	 |  g } ˆ  j | 7_ q] n  d S(   s-   Make sure the field is included in the fieldsR'   N(   R+   R'   R   t   tuple(   R-   t   additional_fields(   R%   (    s@   /tmp/pip-unpacked-wheel-aI1tzL/rest_framework_gis/serializers.pyt   add_to_fieldsD   s    	t   bbox_geo_fieldt	   auto_bboxsQ   You must eiher define a 'bbox_geo_field' or 'auto_bbox', but you can not set both(   R   R   t   __init__R   R   R   t   modelt   _metat   pkt   nameR+   R'   R(   R)   R   R3   t   FalseR4   (   R   R   R    t   default_id_fieldt   primary_keyR/   R2   (    (   R%   s@   /tmp/pip-unpacked-wheel-aI1tzL/rest_framework_gis/serializers.pyR5   2   s(    	
	c         C   sW  t  ƒ  } t |  j j ƒ  ƒ } |  j j ro |  j |  j j } | j | ƒ } | j | ƒ | d <| j | ƒ n  d | d <|  j |  j j	 } | j | ƒ } | j | ƒ | d <| j | ƒ |  j j
 rÝ | rÝ | j | d <n` |  j j r=|  j |  j j } | j | ƒ } t | d ƒ r#| j n d | d <| j | ƒ n  |  j | | ƒ | d <| S(	   s2   
        Serialize objects -> primitives.
        t   idt   FeatureR   t   geometryt   bboxt   extentt
   propertiesN(   R    t   listR'   t   valuesR   R(   t   get_attributeR   t   removeR)   R4   RA   R3   R+   R   t   get_properties(   R   t   instancet   featureR'   t   fieldR$   t	   geo_value(    (    s@   /tmp/pip-unpacked-wheel-aI1tzL/rest_framework_gis/serializers.pyR   [   s*    	
"c         C   sm   t  ƒ  } x] | D]U } | j r% q n  | j | ƒ } d } | d k	 rX | j | ƒ } n  | | | j <q W| S(   sð  
        Get the feature metadata which will be used for the GeoJSON
        "properties" key.

        By default it returns all serializer fields excluding those used for
        the ID, the geometry and the bounding box.

        :param instance: The current Django model instance
        :param fields: The list of fields to process (fields already processed have been removed)
        :return: OrderedDict containing the properties of the current feature
        :rtype: OrderedDict
        N(   R    t
   write_onlyRE   R   R   R-   (   R   RH   R'   RB   RJ   R$   t   representation(    (    s@   /tmp/pip-unpacked-wheel-aI1tzL/rest_framework_gis/serializers.pyRG   ˆ   s    		c         C   s4   d | k r |  j  | ƒ } n  t t |  ƒ j | ƒ S(   sS   
        Override the parent method to first remove the GeoJSON formatting
        RB   (   t   unformat_geojsonR   R   t   to_internal_value(   R   R   (    (    s@   /tmp/pip-unpacked-wheel-aI1tzL/rest_framework_gis/serializers.pyRO   ¢   s    c         C   si   | d } d | k r- | d | |  j  j <n  |  j  j re d | k re t j | d ƒ | |  j  j <n  | S(   s#  
        This function should return a dictionary containing keys which maps
        to serializer fields.

        Remember that GeoJSON contains a key "properties" which contains the
        feature metadata. This should be flattened to make sure this
        metadata is stored in the right serializer fields.

        :param feature: The dictionary containing the feature data directly
                        from the GeoJSON data.
        :return: A new dictionary which maps the GeoJSON values to
                 serializer fields
        RB   R?   R@   (   R   R)   R3   R   t	   from_bbox(   R   RI   t   attrs(    (    s@   /tmp/pip-unpacked-wheel-aI1tzL/rest_framework_gis/serializers.pyRN   ª   s    
 (
   R	   R
   R   t   classmethodR&   R5   R   RG   RO   RN   (    (    (    s@   /tmp/pip-unpacked-wheel-aI1tzL/rest_framework_gis/serializers.pyR       s   	)	-		N(   t   collectionsR    t   django.core.exceptionsR   t   django.contrib.gis.geosR   t   rest_framework.serializersR   R   R   R'   R   R   R   R   R   (    (    (    s@   /tmp/pip-unpacked-wheel-aI1tzL/rest_framework_gis/serializers.pyt   <module>   s   