ó
O'—^c           @   s  d  Z  d d l Z d d l m Z y* d d l m Z m Z d d l m Z Wn7 e	 k
 r… d d l
 m Z m Z d d l m Z n Xd d l m Z d d l m Z d d	 l m Z d
 e f d „  ƒ  YZ d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d S(   s¢   
Google Cloud Messaging
Previously known as C2DM
Documentation is available on the Android Developer website:
https://developer.android.com/google/gcm/index.html
iÿÿÿÿNi   (   t	   GCMDevice(   t   Requestt   urlopen(   t	   urlencode(   t   ImproperlyConfigured(   t   NotificationError(   t   PUSH_NOTIFICATIONS_SETTINGSt   GCMErrorc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s8   /tmp/pip-unpacked-wheel-MTFMX_/push_notifications/gcm.pyR      s   c         c   s7   x0 t  d t |  ƒ | ƒ D] } |  | | | !Vq Wd S(   sA   
	Yield successive chunks from list  l with a minimum size  n
	i    N(   t   ranget   len(   t   lt   nt   i(    (    s8   /tmp/pip-unpacked-wheel-MTFMX_/push_notifications/gcm.pyt   _chunks   s    c         C   s~   t  j d ƒ } | s$ t d ƒ ‚ n  i | d 6d | d 6t t |  ƒ ƒ d 6} t t  d |  | ƒ } t | ƒ j ƒ  j d ƒ S(	   Nt   GCM_API_KEYsX   You need to set PUSH_NOTIFICATIONS_SETTINGS["GCM_API_KEY"] to send messages through GCM.s   Content-Types   key=%st   Authorizations   Content-Lengtht   GCM_POST_URLs   utf-8(	   t   SETTINGSt   getR   t   strR   R   R   t   readt   decode(   t   datat   content_typet   keyt   headerst   request(    (    s8   /tmp/pip-unpacked-wheel-MTFMX_/push_notifications/gcm.pyt	   _gcm_send%   s    c   
      K   s  i |  d 6} x1 | j  ƒ  D]# \ } } | j d ƒ | d | <q WxE | j  ƒ  D]7 \ } } | rN t | t ƒ rx d } n  | | | <qN qN Wt t | j  ƒ  ƒ ƒ j d ƒ } t | d ƒ } | j d ƒ r&| j d ƒ } t	 | ƒ d k r}| d j d ƒ r}| d j d ƒ d	 } t
 | |  ƒ q}nW | j d
 ƒ r}| d k rnt j j d | d ƒ }	 |	 j d d ƒ | St | ƒ ‚ n  | S(   sá   
	Sends a GCM notification to a single registration_id.
	This will send the notification as form data.
	If sending multiple notifications, it is more efficient to use
	gcm_send_bulk_message() with a list of registration_ids
	t   registration_ids   utf-8s   data.%si   s/   application/x-www-form-urlencoded;charset=UTF-8t   ids   
t   =iÿÿÿÿs   Error=s   Error=NotRegistereds   Error=InvalidRegistrationt   activei    (   s   Error=NotRegistereds   Error=InvalidRegistration(   t   itemst   encodet
   isinstancet   boolR   t   sortedR   t
   startswitht   splitR   t   _gcm_handle_canonical_idR    t   objectst   filtert   updateR   (
   R   R   t   kwargst   valuest   kt   vt   resultt   linest   new_idt   device(    (    s8   /tmp/pip-unpacked-wheel-MTFMX_/push_notifications/gcm.pyt   _gcm_send_plain4   s,    	!%c         K   sµ  i |  d 6} | d k	 r& | | d <n  x- | j ƒ  D] \ } } | r3 | | | <q3 q3 Wt j | d d d t ƒj d ƒ } t j t | d ƒ ƒ } | d	 s¦ | d
 r±g  g  } } t }	 x‹ t	 | d ƒ D]y \ }
 } | j
 d ƒ } | r| d k r| j |  |
 ƒ qt }	 n  | j
 d ƒ } | rÊ | j |  |
 | f ƒ qÊ qÊ W| rut j j d | ƒ } | j d d ƒ n  x! | D] \ } } t | | ƒ q|W|	 r±t | ƒ ‚ q±n  | S(   s–   
	Sends a GCM notification to one or more registration_ids. The registration_ids
	needs to be a list.
	This will send the notification as json data.
	t   registration_idsR   t
   separatorst   ,t   :t	   sort_keyss   utf-8s   application/jsont   failuret   canonical_idst   resultst   errort   NotRegisteredt   InvalidRegistrationR   t   registration_id__inR!   i    N(   R8   R9   (   R?   R@   (   t   NoneR"   t   jsont   dumpst   TrueR#   t   loadsR   t   Falset	   enumerateR   t   appendR    R*   R+   R,   R)   R   (   R6   R   R-   R.   R/   R0   t   responset   ids_to_removet   old_new_idst   throw_errort   indexR1   R>   R3   t   removedt   old_id(    (    s8   /tmp/pip-unpacked-wheel-MTFMX_/push_notifications/gcm.pyt   _gcm_send_jsonc   s8    $	c         C   sf   t  j j d |  d t ƒ j ƒ  rC t  j j d | ƒ j d t ƒ n t  j j d | ƒ j d |  ƒ d S(   sC   
	Handle situation when GCM server response contains canonical ID
	R   R!   N(   R    R*   R+   RE   t   existsR,   RG   (   t   canonical_idt
   current_id(    (    s8   /tmp/pip-unpacked-wheel-MTFMX_/push_notifications/gcm.pyR)   —   s    !"c         K   s   t  |  | |  S(   sG  
	Sends a GCM notification to a single registration_id.

	If sending multiple notifications, it is more efficient to use
	gcm_send_bulk_message() with a list of registration_ids

	A reference of extra keyword arguments sent to the server is available here:
	https://developers.google.com/cloud-messaging/server-ref#downstream
	(   R5   (   R   R   R-   (    (    s8   /tmp/pip-unpacked-wheel-MTFMX_/push_notifications/gcm.pyt   gcm_send_message¡   s    c         K   sn   t  j d ƒ } t |  ƒ | k r^ g  } x0 t |  | ƒ D] } | j t | | |  ƒ q7 W| St |  | |  S(   s*  
	Sends a GCM notification to one or more registration_ids. The registration_ids
	needs to be a list.
	This will send the notification as json data.

	A reference of extra keyword arguments sent to the server is available here:
	https://developers.google.com/cloud-messaging/server-ref#downstream
	t   GCM_MAX_RECIPIENTS(   R   R   R   R   RI   RQ   (   R6   R   R-   t   max_recipientst   rett   chunk(    (    s8   /tmp/pip-unpacked-wheel-MTFMX_/push_notifications/gcm.pyt   gcm_send_bulk_message¯   s    (   t   __doc__RC   t   modelsR    t   urllib.requestR   R   t   urllib.parseR   t   ImportErrort   urllib2t   urllibt   django.core.exceptionsR   t    R   t   settingsR   R   R   R   R   R5   RQ   R)   RU   RZ   (    (    (    s8   /tmp/pip-unpacked-wheel-MTFMX_/push_notifications/gcm.pyt   <module>   s&   			/	4	
	