ó
¸9—^c           @  sÃ   d  d l  m Z m 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 m Z d  d l m Z d  d l m Z d	 d
 d d d d g Z d „  Z d e f d „  ƒ  YZ d S(   iÿÿÿÿ(   t   unicode_literalst   print_functionN(   t   settings(   t   get_user_model(   t   Group(   t   BaseCommandt   CommandError(   t
   csv_writer(   t   signalcommandu   addressu   emailsu   googleu   outlooku   linkedinu   vcardc          K  s}   |  j  d ƒ } |  j  d ƒ } d j d „  | | g Dƒ ƒ } | rG | S|  j  d ƒ } | r` | S|  j  d ƒ } | ry | Sd S(   u   Return full name or username.u
   first_nameu	   last_nameu    c         s  s   |  ] } | r | Vq d  S(   N(    (   t   .0t   n(    (    sU   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/export_emails.pys	   <genexpr>   s    u   nameu   usernameu    (   t   gett   join(   t   kwargst
   first_namet	   last_namet   namet   username(    (    sU   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/export_emails.pyt	   full_name   s    t   Commandc           B  s†   e  Z d  Z d Z d Z e Z d Z d „  Z d „  Z	 d „  Z
 e d „  ƒ Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z RS(   u=   Export user email address list in one of a number of formats.u   [output file]u   filename to save tou   utf-8c         O  s)   t  t |  ƒ j | | Ž  t ƒ  |  _ d  S(   N(   t   superR   t   __init__R   t	   UserModel(   t   selft   argsR   (    (    sU   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/export_emails.pyR   4   s    c         C  s~   t  t |  ƒ j | ƒ | j d d d d d d d d  d d	 ƒf | j d
 d d d d d d t d d d d j t ƒ ƒd  S(   Nu   --groupu   -gt   actionu   storet   destu   groupt   defaultt   helpu8   Limit to users which are part of the supplied group nameu   --formatu   -fu   formati    u    output format. May be one of %s.u   , (   R   R   t   add_argumentst   add_argumentt   Nonet   FORMATSR   (   R   t   parser(    (    sU   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/export_emails.pyR   8   s    
c         K  s   t  t d t ƒ |   S(   Nu   EXPORT_EMAILS_FULL_NAME_FUNC(   t   getattrR   R   (   R   R   (    (    sU   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/export_emails.pyR   C   s    c   	      O  sR  t  | ƒ d k r! t d ƒ ‚ n  | d } | rª t j j d | ƒ j ƒ  d k rª d j d „  t j j d ƒ Dƒ ƒ } | r d | d	 } n  t d
 | d | ƒ ‚ n  t ƒ  } t	 t
 d d d d d g ƒ } t	 t
 d d d d d g ƒ } | j j ƒ  j | Œ  } | r(| j d | ƒ j ƒ  } n  | j | Œ  } t	 |  | d ƒ | ƒ d  S(   Ni   u   extra arguments suppliedu   groupR   u   ', 'c         s  s   |  ] } | d  Vq d S(   u   nameN(    (   R	   t   g(    (    sU   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/export_emails.pys	   <genexpr>L   s    u   nameu   'u   '.u   Unknown group 'u   '. Valid group names are: u   EXPORT_EMAILS_ORDER_BYu	   last_nameu
   first_nameu   usernameu   emailu   EXPORT_EMAILS_FIELDSt   groups__nameu   format(   t   lenR   R   t   objectst   filtert   countR   t   valuesR   R"   R   t   allt   order_byt   distinct(	   R   R   t   optionst   groupt   namesR   R+   t   fieldst   qs(    (    sU   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/export_emails.pyt   handleF   s     
(%	c           s=   ˆ  j  j d j ‡  f d †  | Dƒ ƒ ƒ ˆ  j  j d ƒ d S(   uc   
        Single entry per line in the format of:
            "full name" <my@address.com>;
        u   
c         3  s4   |  ]* } d  ˆ  j  |   | j d d ƒ f Vq d S(   u
   "%s" <%s>;u   emailu    N(   R   R   (   R	   t   ent(   R   (    sU   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/export_emails.pys	   <genexpr>`   s    N(   t   stdoutt   writeR   (   R   R1   (    (   R   sU   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/export_emails.pyt   address[   s    )c         C  s7   |  j  j d j d „  | Dƒ ƒ ƒ |  j  j d ƒ d S(   u\   
        Single entry with email only in the format of:
            my@address.com,
        u   ,
c         s  s(   |  ] } | j  d  ƒ r | d  Vq d S(   u   emailN(   R   (   R	   R3   (    (    sU   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/export_emails.pys	   <genexpr>h   s    u   
N(   R4   R5   R   (   R   R1   (    (    sU   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/export_emails.pyt   emailsc   s    #c         C  s_   t  t j ƒ } | j d d g ƒ x6 | D]. } | j |  j |   | j d d ƒ g ƒ q) Wd S(   u3   CSV format suitable for importing into google GMailu   Nameu   Emailu   emailu    N(   t   writert   sysR4   t   writerowR   R   (   R   R1   t   csvfR3   (    (    sU   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/export_emails.pyt   googlek   s    c         C  st   t  t j ƒ } | j d d d g ƒ xH | D]@ } | j | j d d ƒ | j d d ƒ | j d d ƒ g ƒ q, Wd S(	   uˆ   
        CSV format suitable for importing into linkedin Groups.
        perfect for pre-approving members of a linkedin group.
        u
   First Nameu	   Last Nameu   Emailu
   first_nameu    u	   last_nameu   emailN(   R8   R9   R4   R:   R   (   R   R1   R;   R3   (    (    sU   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/export_emails.pyt   linkedinr   s    c         C  s¶   t  t j ƒ } d d d d d d d d d	 d
 d d d d d d d d d d g } | j | ƒ d g t | ƒ d } x: | D]2 } | j |  j |   | j d d ƒ g | ƒ q| Wd S(   u.   CSV format suitable for importing into outlooku   Nameu   E-mail Addressu   Notesu   E-mail 2 Addressu   E-mail 3 Addressu   Mobile Phoneu   Pageru   Companyu	   Job Titleu
   Home Phoneu   Home Phone 2u   Home Faxu   Home Addressu   Business Phoneu   Business Phone 2u   Business Faxu   Business Addressu   Other Phoneu	   Other Faxu   Other Addressu    i   u   emailN(   R8   R9   R4   R:   R%   R   R   (   R   R1   R;   t   columnst   emptyR3   (    (    sU   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/export_emails.pyt   outlook|   s    c         C  sG  y d d l  } Wn4 t k
 rF t |  j j d ƒ ƒ t j d ƒ n Xt j } xð | D]è } | j ƒ  } |  j	 |   | j
 d ƒ _ | j d ƒ rÎ | j d ƒ rÎ | j j | d | d ƒ | j
 d ƒ _ n' | j j |  j	 |   ƒ | j
 d ƒ _ | j d	 ƒ r,| j
 d	 ƒ } | d	 | _ d
 | _ n  | j | j ƒ  ƒ qW Wd S(   u   VCARD format.iÿÿÿÿNu6   Please install vobject to use the vcard export format.i   u   fnu	   last_nameu
   first_nameu   nu   emailu   INTERNET(   t   vobjectt   ImportErrort   printt   stylet   ERRORR9   t   exitR4   t   vCardR   t   addt   valueR   t   vcardt   Namet
   type_paramR5   t	   serialize(   R   R1   RA   t   outR3   t   cardt	   emailpart(    (    sU   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/export_emails.pyRJ   ˆ   s"    	,'(   t   __name__t
   __module__R   R   t   labelt   Truet   can_import_settingst   encodingR   R   R   R   R2   R6   R7   R<   R=   R@   RJ   (    (    (    sU   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/export_emails.pyR   ,   s   							
	(   t
   __future__R    R   R9   t   django.confR   t   django.contrib.authR   t   django.contrib.auth.modelsR   t   django.core.management.baseR   R   t   django_extensions.compatR   R8   t"   django_extensions.management.utilsR   R    R   R   (    (    (    sU   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/commands/export_emails.pyt   <module>   s   		