ó
¶äjfc           @   sò   d  d l  m Z d  d l Z d  d l Z d  d l Z d  d l m Z d  d l m Z d  d l	 m
 Z
 m Z m Z m Z d Z d „  Z d „  Z e d	 „ Z d
 d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d S(   iÿÿÿÿ(   t   slugifyN(   t   settings(   t	   AutoField(   t   sint   cost   radianst   acosgƒÀÊ¡…í®@c         C   sw   d d l  m  } m } d d l m } | |  j ƒ  ƒ } t |  ƒ ‚ | ro | | d  Œ  } | | d | d ƒ St Sd  S(   Niÿÿÿÿ(   t   datetimet	   timedelta(   t   parsedate_tzi   t   seconds(   R   R   t   email.utilsR	   t   stript	   Exceptiont   False(   t
   datestringR   R   R	   t
   time_tuplet   dt(    (    s4   /var/www/svtgbi.org/svtgbi/modules/core/functions.pyt   to_datetime   s    c         C   sK   d d l  m } d d l m } y | |  ƒ t SWn | k
 rF t SXd  S(   Niÿÿÿÿ(   t   validate_email(   t   ValidationError(   t   django.core.validatorsR   t   django.core.exceptionsR   t   TrueR   (   t   emailR   R   (    (    s4   /var/www/svtgbi.org/svtgbi/modules/core/functions.pyt   is_valid_email   s    
c         C   ss   | r% t  d t j | f d ƒ } n t  d t j | f d ƒ } x! |  j ƒ  D] } | j | ƒ qN W| j ƒ  | S(   Ns   %s/exam_applications/%ss   wb+s   %s/%s(   t   openR   t   PRIVATE_FILESt
   MEDIA_ROOTt   chunkst   writet   close(   t   ft   filenamet   privatet   destinationt   chunk(    (    s4   /var/www/svtgbi.org/svtgbi/modules/core/functions.pyt   handle_uploaded_file)   s    
t   slugc         C   sx   d } t  |  ƒ } } x[ t rs | rC d j | t | ƒ g ƒ } n  | j j i | | 6  j ƒ  sf | S| d 7} q Wd  S(   Ni    t   -i   (   R    R   t   joint   strt   objectst   filtert   count(   t   valuet   modelt	   slugfieldt   suffixt	   potentialt   base(    (    s4   /var/www/svtgbi.org/svtgbi/modules/core/functions.pyt   slugify_unique6   s    	c         C   sS   xL t  rN d j d „  t |  ƒ Dƒ ƒ } | j j i | | 6  j ƒ  s | Sq Wd  S(   Nt    c         s   s(   |  ] } t  j t j t j ƒ Vq d  S(   N(   t   randomt   choicet   stringt   ascii_uppercaset   digits(   t   .0t   x(    (    s4   /var/www/svtgbi.org/svtgbi/modules/core/functions.pys	   <genexpr>D   s    (   R   R(   t   rangeR*   R+   R,   (   t   numberR.   t   fieldt
   random_str(    (    s4   /var/www/svtgbi.org/svtgbi/modules/core/functions.pyt   random_string_uniqueA   s    	c         C   s#   d j  d „  t |  ƒ Dƒ ƒ } | S(   NR4   c         s   s(   |  ] } t  j t j t j ƒ Vq d  S(   N(   R5   R6   R7   R8   R9   (   R:   R;   (    (    s4   /var/www/svtgbi.org/svtgbi/modules/core/functions.pys	   <genexpr>L   s    (   R(   R<   (   R=   R?   (    (    s4   /var/www/svtgbi.org/svtgbi/modules/core/functions.pyt   generate_random_passwordJ   s    c         C   so   t  g  |  j j D]I } t | t ƒ r | |  j j j ƒ  k r | j t |  | j ƒ f ^ q ƒ } |  j	 |   S(   N(
   t   dictt   _metat   fieldst
   isinstanceR   t   parentst   valuest   namet   getattrt	   __class__(   t   objR    t   initial(    (    s4   /var/www/svtgbi.org/svtgbi/modules/core/functions.pyt   copy_model_instanceQ   s    Lc         C   sj   t  |  ƒ }  t  | ƒ } t  | | ƒ } t |  ƒ t | ƒ t |  ƒ t | ƒ t | ƒ } t | ƒ t S(   s&   all angles in degrees, result in miles(   R   R   R   R   t   EARTH_RADIUS_IN_MILES(   t   lat_at   long_at   lat_bt   long_bt
   delta_longt   cos_x(    (    s4   /var/www/svtgbi.org/svtgbi/modules/core/functions.pyt   calc_dist_fixedX   s    !c         C   s¹   t  j } t  j } t  j } t  j } |  j | ƒ rW t j j | |  j	 | d ƒ ƒ } n3 |  j | ƒ rŠ t j j | |  j	 | d ƒ ƒ } n  t j j
 | ƒ sµ t d | | f ƒ ‚ n  | S(   NR4   s"   media URI must start with %s or %s(   R   t
   STATIC_URLt   STATIC_ROOTt	   MEDIA_URLR   t
   startswitht   ost   pathR(   t   replacet   isfileR   (   t   urit   relt   sUrlt   sRoott   mUrlt   mRootR[   (    (    s4   /var/www/svtgbi.org/svtgbi/modules/core/functions.pyt   link_callbacke   s    				$$c         C   sQ   g  } x7 t  |  ƒ | k r? |  |  } | j | ƒ |  | }  q	 W| j |  ƒ | S(   N(   t   lent   append(   t   arrt   sizet   arrst   pice(    (    s4   /var/www/svtgbi.org/svtgbi/modules/core/functions.pyt   splitx   s    
c         C   sI   d |  k o d k n r  d Si d d 6d d 6d d	 6j  |  d
 d ƒ S(   Ni   i   t   tht   sti   t   ndi   t   rdi   i
   (   t   get(   t   d(    (    s4   /var/www/svtgbi.org/svtgbi/modules/core/functions.pyR0   ‚   s    c         C   s/   | j  |  ƒ j d t | j ƒ t | j ƒ ƒ S(   Ns   {S}(   t   strftimeR\   R)   t   dayR0   (   t   formatt   t(    (    s4   /var/www/svtgbi.org/svtgbi/modules/core/functions.pyt   custom_strftime†   s    (   t   django.template.defaultfiltersR    R5   R7   RZ   t   django.confR   t   django.db.modelsR   t   mathR   R   R   R   RN   R   R   R   R%   R3   R@   RA   RM   RU   Rd   Rk   R0   Rv   (    (    (    s4   /var/www/svtgbi.org/svtgbi/modules/core/functions.pyt   <module>   s&   "		
							
	