o
    Ke                     @   s   d dl m Z  d dlmZ d dlZd dlZd dlZd dlmZmZ d dl	m
Z
 d dlmZ dd Zd	d
 ZG dd dZdddZdd Zdd Zdd Zdd Zdd ZdddZdS )    )datetime)timezoneN)check_outputCalledProcessError)User)settingsc               
   C   sH  zpt jdd ddlm}  dd l}tjjddD ]P}| jjd|d}|	 dkr-t
d	|	 dkr?td
t|j d  q|	 dkritj|_|jj|jdd}|j|jd tdt|j d |j d  qtd W dS  |jjy } ztt| W Y d }~dS d }~w t
y } ztt| W Y d }~dS d }~ww )N   z/var/www/mysite/ideatree/)UserProfiler   T)	is_active )stripecustomeriduserz?Database returned more than one profile for one canonical User.zSkipping User id zH that already has a stripe customer id in his corresponding UserProfile.z!IdeaTree Premium account customer)emaildescription)r   zSaved: for User: z stripe customer id: z" in his corresponding UserProfile.SuccessF)syspathinsertmodelsr	   striper   objectsfiltercount	Exceptionprintstridr   STRIPE_SECRET_KEYapi_keyCustomercreater   updateerrorInvalidRequestError)r	   r   actUsernonStripeUserProfilestripe_customer_objecterr r(   +/var/www/ideatree/ideatree/utils/myutils.pyinit_stripe_customer_objects   s<    r*   c                 C   s$   t | |kr| d |d  d S | S )N   z..)len)stringtoLengthr(   r(   r)   truncateStr-   s   $r/   c                   @   s   e Zd Zdd ZdS )Structc                 K   s   | j | d S N)__dict__r!   )selfentriesr(   r(   r)   __init__2   s   zStruct.__init__N)__name__
__module____qualname__r5   r(   r(   r(   r)   r0   1   s    r0   Fc              
   C   s   z'|st j}ttdgdd}ttdgdd}t| | t| || W d S  ty; } zt| t	|d }~w t
yN } zt| t	|d }~w t	yk } zt| tt|ddW  Y d }~S d }~ww )Nzid -u wsgiuserT)shellzid -u www-datai  )status)statS_IRWXUintr   oschmodchownr   writeErrr   OSErrorHttpResponser   )filepathmaskuserIdgroupIdr'   r(   r(   r)   doWSGIPermissions6   s(   rH   c                    s    g } fdd  | ||}|S )Nc                    sr   t | tr'|  D ]\}}t |ttfr ||| q	||kr$|| q	|S t | tr7| D ]} ||| q.|S r1   )
isinstancedictitemslistappend)objarrkeykvitemextractr(   r)   rU   T   s   


zextract_values.<locals>.extractr(   )rN   rP   rO   resultsr(   rT   r)   extract_valuesP   s   rW   c              	   C   s6   z|   } W n ttfy   Y nw t| tjd d S )N)file)decodeUnicodeDecodeErrorAttributeErrorr   r   stderr)liner(   r(   r)   rA   g   s   rA   c                 C   s   |  dd dd ddS )z
  Add slashes before quotes. Useful for escaping strings in CSV, for
  example. Less useful for escaping JavaScript; use the ``escapejs``
  filter instead.
  \z\\"z\"'z\')replace)valuer(   r(   r)   
addslashesq   s   rc   c                 C   s   | sdS t | S )Nr   )r   )sr(   r(   r)   tostr{   s   re   c                 C   s    |  |r| d t|  S | S r1   )endswithr,   )	thestringendingr(   r(   r)   rStrChop   s   
ri   c                 C   s^   t tjt dddtjd }t|jd d d |j|   d |jd  |}|s-t|S |S )Ni  r   )tzinfo   <   i  g     @@)	r   nowr   utcrounddayssecondsmicrosecondsr=   )offsetSecondsdecimalPointsdateTimeNowmsr(   r(   r)   	millisNow   s
   0rw   )F)r   r   )r   django.utilsr   r   r;   r>   
subprocessr   r   django.contrib.auth.modelsr   django.confr   r*   r/   r0   rH   rW   rA   rc   re   ri   rw   r(   r(   r(   r)   <module>   s$   


