mò
þÀbIc           @   s4  d  Z  d Z  d d d !Z d k Z d k Td k Td k l Z l Z d k l Z d	 k l	 Z	 l
 Z
 d k Z d k Z d k Z d
 k l Z l Z y d k l Z Wn d k l Z n Xd Z d Z d Z d Z e d e ƒ  ƒ Z d d d d d d d d d d d e d „ Z d e f d „  ƒ  YZ e e ƒ d S(   s   ExtImage product module.s  ExtImage product module.
    The ExtImage-Product works like the Zope Image-product, but stores the 
    uploaded image externally in a repository-direcory. It creates a preview
    of the image (requires PIL).

$Id: ExtImage.py 5740 2006-01-22 11:38:09Z roug $s   $Release: 1.1.3 $i
   iþÿÿÿN(   t   *(   s   InitializeClasss   DTMLFile(   s   DateTime(   s   IntTypes
   StringType(   s   joins   isfile(   s   StringIOi    i   i   i   t   extImageAddt    c         C   sV  | oz t | d ƒ oj | i } | p | } | t t i | d ƒ t i | d ƒ t i | d ƒ ƒ d } t i | t	 ƒ } n |  i ƒ  }  t | | | | ƒ } |  i | | ƒ |  i | ƒ i | | d | | |	 |
 ƒ | t j p | t j o, |  i | ƒ i | | d | | |	 |
 ƒ n | d	 j	 o$ d | } |  i |  | d | ƒSn d	 S(
   s    Add a ExtImage to a folder t   filenamet   /s   \t   :i   i    s)   The ExtImage %s was successfully created!t   manage_tabs_messageN(   t   idt   hasattrt   fileR   t   titlet   maxt   stringt   rfindt	   translatet   TRANSMAPt   selft   thist   ExtImaget   descrt   permission_checkt   tempExtImaget
   _setObjectt   _getObt   manage_file_uploadt   content_typet   create_prevt   maxxt   maxyt   ratiot   UPLOAD_NORESIZEt   UPLOAD_RESIZEt   previewt   REQUESTt   Nonet   messaget   manage_main(   R   R   R
   R   R	   R    R   R   R   R   R   R   R!   R#   R   (    (    t=   /home2/kennisonb/webapps/zope/Zope/Products/Photo/ExtImage.pyt   manage_addExtImageF   s&     	
R   c           B   sÿ  t  Z e ƒ  Z e i d d ƒ e i d d ƒ e i d d ƒ e i d d ƒ e i d d ƒ e i d	 d
 ƒ e i d	 d ƒ e i d	 d ƒ e i d d ƒ e i d d ƒ e i d d ƒ e i d d ƒ d Z h  d d <d d <d d <Z d d d d „ Z d „  Z e i d d ƒ d d d  d  d  d d d d d  d „
 Z
 e i d d  ƒ d! „  Z e i d d" ƒ d# „  Z e i d d$ ƒ d% „  Z e i d d& ƒ d' „  Z d( „  Z d) „  Z d* „  Z d+ „  Z d, „  Z d- „  Z d. „  Z e d/ e ƒ  ƒ Z e i d d0 ƒ d  d1 „ Z e i d d2 ƒ d d d d  d3 „ Z e d4 e ƒ  ƒ Z e i d d5 ƒ d d d e d d d d  d6 „ Z e i d d7 ƒ d d  d8 „ Z d9 „  Z d: „  Z  d; „  Z! d< „  Z" d= „  Z# d> „  Z$ d  d? „ Z% d@ „  Z& dA „  Z' dB „  Z( RS(C   Ns   View management screenst   manage_tabsR$   t   manage_uploadForms   Change permissionst   manage_accesss   Change ExtFile/ExtImaget   manage_editExtFiles
   FTP accesst   manage_FTPstatt   manage_FTPgett   manage_FTPlistt   Viewt
   index_htmlt	   icon_htmlt   icon_gift   linkR   t   defaulti   t   mini
   R   iç  R   i    c         C   s>   t  i d ƒ |  | | | | ƒ g  |  _ d |  _ d |  _	 d S(   s%   initialize a new instance of ExtImaget   __init__R   i    N(
   R   t   inheritedAttributeR   R   R
   R   R   t   prev_filenamet   prev_content_typet   has_preview(   R   R   R
   R   R   (    (    R%   R5   z   s     
		c         C   s
   |  i ƒ  S(   N(   R   t   tag(   R   (    (    R%   t   __str__‡   s    R:   t   0c         K   s  |
 d j o t |  d ƒ o |  i  }
 n |  i |
 ƒ p
 d } n | p |  i ƒ  o, d |  i ƒ  } |  i	 |  i
 ƒ \ } } nL | o  d |  i ƒ  } d \ } } n% |  i ƒ  } |  i	 |  i ƒ \ } } | p | } | p | } | p | } | p | } | o' | d j o t t | ƒ | ƒ } n | o' | d j o t t | ƒ | ƒ } n | d j o |  i ƒ  } n d | |	 | f } | o d | | f } n | o d | | f } n x6 | i ƒ  D]( } | i | ƒ } d	 | | | f } qÓWd
 | } | S(   s=   Generate an HTML IMG tag for this image, with customization.
            Arguments to self.tag() can be any valid attributes of an IMG tag.
            'src' will always be an absolute pathname, to prevent redundant
            downloading of images. Defaults are applied intelligently for
            'height', 'width', and 'alt'. If specified, the 'scale', 'xscale',
            and 'yscale' keyword arguments will be used to automatically adjust
            the output height and width values of the image tag.
            Adopted and adapted from OFS/image.py
        R!   i   s   %s?preview=1s	   %s?icon=1i    s"   <img src="%s" border="%s" alt="%s"s   %s height="%s"s   %s width="%s"s
   %s %s="%s"s   %s>N(   i    i    (!   R!   R"   R   R   t   _access_permittedR    t   is_webviewablet   absolute_urlt   urlt   _getImageSizeR7   t	   img_widtht
   img_heightt   iconR   t   heightt   widtht   xscalet   scalet   xdeltat   yscalet   ydeltat   strt   intt   altt   title_or_idt   bordert   strgt   argst   keyst   keyt   gett   value(   R   R    RD   RE   RF   RN   RH   RG   RJ   RP   R!   RR   RB   RC   RI   RT   RK   R@   RV   RQ   (    (    R%   R:   ‹   sD    
  
    
R    c         C   s   d |  i ƒ  d ‚ d S(   s    return a preview of the image t   Redirects
   ?preview=1N(   R   R?   (   R   (    (    R%   R    ¹   s     t   preview_htmlc         C   s   |  i d d ƒ S(   sA    The preview-image embedded in html with a link to the real file R    i   N(   R   R:   (   R   (    (    R%   RX   ¾   s     t	   is_brokenc         C   sv   |  i oY |  i |  i j oF |  i |  i ƒ } t | ƒ p# |  i ƒ  t | ƒ p d Sq_ qc n t i	 d ƒ |  ƒ S(   s@    Check if external file exists and return true (1) or false (0) i   RY   N(
   R   R9   R   R7   t   _get_filenamet   fnt   isfilet   _undoR   R6   (   R   R[   (    (    R%   RY   Ã   s     
R>   c         C   sC   |  i ƒ  } | d j p | d j p | d j o d Sn d Sd S(   s:    return 1 for GIF,JPEG and PNG images, otherwise return 0 t   JPEGt   GIFt   PNGi   i    N(   R   t   format(   R   Ra   (    (    R%   R>   Î   s
     'c         C   s[   |  i |  i ƒ } t | ƒ p |  i ƒ  n t | ƒ o t i | ƒ d } n d } | S(   s&    Returns the size of the preview file i   i    N(	   R   RZ   R7   R[   R\   R]   t   ost   statt   size(   R   R[   Rd   (    (    R%   t   prev_rawsizeÖ   s       c         C   s   |  i |  i ƒ  ƒ S(   s=    Returns a formatted stringified version of the preview size N(   R   t   _bytetostringRe   (   R   (    (    R%   t	   prev_sizeÞ   s     c         C   s   |  i |  i ƒ d S(   s    pixel width of the image i    N(   R   RA   R   (   R   (    (    R%   RF   â   s     c         C   s   |  i |  i ƒ d S(   s    pixel height of the image i   N(   R   RA   R   (   R   (    (    R%   RE   æ   s     c         C   s   |  i |  i ƒ d S(   s    pixel width of the preview i    N(   R   RA   R7   (   R   (    (    R%   t
   prev_widthê   s     c         C   s   |  i |  i ƒ d S(   s    pixel height of the preview i   N(   R   RA   R7   (   R   (    (    R%   t   prev_heightî   s     c         C   sE   y3 d k  l } | i |  i |  i ƒ ƒ } | i SWn d Sn Xd S(   s!    Get the Fileformat of the image (   s   Imaget   unknownN(   t   PILt   Imaget   openR   RZ   R   t   imRa   (   R   Rn   Rl   (    (    R%   Ra   ò   s     t   extImageEditt   manage_del_prevc         C   s˜   |  i oW |  i |  i j oD |  i |  i ƒ } y t i | | d ƒ Wqa t j
 o qa Xn d |  _	 d |  _ | d j	 o |  i |  | ƒ Sn d S(   s    Delete the Preview Image s   .undoR   i    N(   R   R9   R   R7   RZ   t   prev_fnRb   t   renamet   OSErrorR8   R!   R"   R$   (   R   R!   Rq   (    (    R%   Rp     s       			t   manage_create_prevc         C   sç   |  i | | ƒ \ } } | d j o9 | d j o, |  i ƒ  } |  i |  i | | | | ƒ n | d j	 ox |  i o |  i |  | ƒ Sqã | d j o- | d j o  d } |  i |  | d | ƒSqã d } |  i |  | d | ƒSn d S(   s    Create a preview Image i    R<   s%   Attention: You must enter a value > 0R   s8   Warning: An error occurred while generating the preview.N(   s%   Attention: You must enter a value > 0(   R   t   _formatDimensionsR   R   t   _get_ufnR7   t   _createPreviewR   R   R!   R"   R9   R$   R#   (   R   R   R   R   R!   R7   R#   (    (    R%   Rt     s     
t   extImageUploadR   c	         C   s¢  | oâ t | ƒ t d ƒ j	 oÉ |  i ƒ  }	 |  i | |  i |	 ƒ ƒ |  i | | ƒ \ } } | t j o7 | d j o* | d j o |  i |	 |	 | | | ƒ qq|	 |  _ |  i | | i d ƒ |  i | p |  i ƒ |  _ |  i ƒ  n‰ t i d ƒ |  | | ƒ | t j ob |  i | | ƒ \ } } | d j o9 | d j o, |  i ƒ  }	 |  i |  i |	 | | | ƒ qqn | d j	 o  d }
 |  i |  | d |
 ƒSn d S(   s"    Upload file from local directory R   i    id   R   s#   The file was uploaded successfully!R   N(   t
   is_previewt   typeR	   R   Rv   R7   t   _copyRZ   Ru   R   R   R   R   Rw   R   t   _get_content_typet   readR   R   R8   t   _initPreviewR   R6   t   GENERATER   R!   R"   R#   R$   (   R   R	   R   Ry   R   R   R   R   R!   R7   R#   (    (    R%   R   $  s0      '		t   manage_http_uploadc   
      C   s@  | o¤ t i | d ƒ } |  i ƒ  } d } y t i | ƒ } Wn | | ‚ n X|  i
 | |  i | ƒ ƒ | |  _ |  i t t i d ƒ |  i |  i ƒ |  _ |  i ƒ  ne t i d ƒ |  | ƒ |  i oD |  i |  i ƒ \ } } |  i ƒ  } |  i |  i | | | d ƒ n | d j	 o  d }	 |  i |  | d |	 ƒSn d S(	   s    Upload file from http-server s   /:s   Can't open: id   R€   i    s#   The file was uploaded successfully!R   N(   Ry   t   urllibt   quoteR@   R   Rv   R7   t   cant_read_exct   urlopent   fp_inR{   RZ   R|   t   fp_tempR}   R   R8   R~   R   R6   R9   RA   RF   RE   Rw   R   R!   R"   R#   R$   (
   R   R@   Ry   R!   R7   R…   RE   RF   Rƒ   R#   (    (    R%   R€   D  s0       		
c         C   sm   t  i d ƒ |  | | ƒ } |  i oD |  i |  i ƒ \ } } |  i
 ƒ  } |  i |  i | | | d ƒ n | S(   s    Handle HTTP PUT requests t   PUTi    N(   R   R6   R   R!   t   RESPONSER9   RA   R7   RF   RE   Rv   Rw   R   (   R   R!   Rˆ   R7   RE   RF   (    (    R%   R‡   ]  s     
c         C   sL   |  i |  i ƒ \ } } | d j p | d j o d |  _ n
 d |  _ d  S(   Ni    i   (   R   RA   R7   Rh   Ri   R9   (   R   Ri   Rh   (    (    R%   R~   k  s    c         C   s‘   ys d k  l } y | i |  i | ƒ ƒ } Wn) |  i ƒ  | i |  i | ƒ ƒ } n X| i d } | i d } Wn d } } n X| | f S(   N(   s   Imagei    i   (   Rk   Rl   Rm   R   RZ   R   Rn   R]   Rd   RF   RE   (   R   R   RE   RF   Rn   Rl   (    (    R%   RA   r  s    
c         C   s  yì d k  l } | i |  i | ƒ ƒ } | i d j o# | i d j o | i d ƒ } n | d j o | i
 | | f ƒ n< | i d	 | | f ƒ | i ƒ  | i | | f | i ƒ } | i |  i | ƒ d d d ƒd |  _ | |  _ Wn n X|  i ƒ  d	 S(
   s    generate a preview using PIL (   s   Imaget   RGBt   CMYKi   R^   t   qualityid   s
   image/jpegN(   Rk   Rl   Rm   R   RZ   t   from_filenameRn   t   modet   convertR   t	   thumbnailR   R   t   draftR"   t   loadt   resizet   BICUBICt   savet   to_filenameR8   R7   R~   (   R   RŒ   R•   R   R   R   Rn   Rl   (    (    R%   Rw   €  s"       
	c         C   s>  t  | ƒ t j o. y t i | ƒ } WqA |  i d } qA Xn t  | ƒ t j o. y t i | ƒ } Wq‚ |  i d } q‚ Xn | d j o¥ | d j o˜ | |  i d j  o |  i d } n& | |  i d j o |  i d } n | |  i d j  o |  i d } q4| |  i d j o |  i d } q4n | | f S(   s+    make sure, the dimensions are valid int's R3   i    R4   R   N(   Rz   R   t
   StringTypeR   t   atoiR   t   _image_sizeR   (   R   R   R   (    (    R%   Ru   “  s*             c         C   sj   |  i oM |  i |  i ƒ } t | ƒ o) t | d ƒ o t i | d | ƒ qW n t i	 d ƒ |  ƒ S(   s+    restore filename after undo or copy-paste s   .undoR]   N(
   R   R9   RZ   R7   R[   R\   Rb   Rr   R   R6   (   R   R[   (    (    R%   R]   ¢  s     
c         C   sÏ   | d j	 o t i d ƒ |  | ƒ Sn d } |  i  oH |  i |  i  ƒ d } t | ƒ o  t | d ƒ o |  i  } q n | p t i d ƒ |  |  i	 ƒ } n | |  i	 j o | d d | d <n | S(   sp    If no unique filename has been generated, generate one
            otherwise, return the existing one.
        Rv   R   s   .previews   .undoiÿÿÿÿN(
   R   R"   R   R6   R   t   new_fnRZ   t   test_fnR\   R7   (   R   R   R™   Rš   (    (    R%   Rv   ª  s     
 c         C   sJ  y |  i d ƒ Wn nX|  i ƒ  } | } | d d | d <|  i on |  i |  i j o[ |  i |  i ƒ } t
 | ƒ o& |  i | |  i | ƒ ƒ | |  _ qg  |  _ d |  _ ng |  i oS |  i |  i ƒ } t
 | ƒ p t
 | d ƒ o | |  _ qg  |  _ d |  _ n
 g  |  _ t i d ƒ |  | ƒ St i d ƒ |  | ƒ S(   sÔ    When a copy of the object is created (zope copy-paste-operation),
            this function is called by CopySupport.py. A copy of the external 
            file is created and self.filename is changed.
        i   iÿÿÿÿs   .previewi    s   .undot   manage_afterCloneN(   R   R?   t   _get_new_ufnR™   t   new_prev_fnR9   R   R7   RZ   t   old_prev_fnR\   R{   t   old_fnR   R6   t   item(   R   R    RŸ   Rž   R™   R   (    (    R%   R›   À  s0     	
		c         C   s   t  i d ƒ |  | | ƒ S(   sÔ    When a copy of the object is created (zope copy-paste-operation),
            this function is called by CopySupport.py. A copy of the external 
            file is created and self.filename is changed.
        t   manage_afterAddN(   R   R6   R   R    t	   container(   R   R    R¢   (    (    R%   R¡   ä  s     c         C   sz   |  i oW |  i |  i j oD |  i |  i ƒ } y t i | | d ƒ Wqa t j
 o qa Xn t	 i
 d ƒ |  | | ƒ S(   sa   This method is called, when the object is deleted. To support 
            undo-functionality and because this happens too, when the object 
            is moved (cut-paste) or renamed, the external file is not deleted. 
            It are just renamed to filename.undo and remains in the 
            repository, until it is deleted manually.
        s   .undot   manage_beforeDeleteN(   R   R9   R   R7   RZ   Rq   Rb   Rr   Rs   R   R6   R    R¢   (   R   R    R¢   Rq   (    (    R%   R£   ì  s     	()   t   __name__t
   __module__t   ClassSecurityInfot   securityt   declareProtectedt	   meta_typeR˜   R5   R;   R"   R:   R    RX   RY   R>   Re   Rg   RF   RE   Rh   Ri   Ra   t   DTMLFilet   globalsR$   Rp   Rt   R(   t
   NO_PREVIEWR   R€   R‡   R~   RA   Rw   Ru   R]   Rv   R›   R¡   R£   (    (    (    R%   R   _   sl   	!	'-			
								!							$	(   t   __doc__t   __version__t   Globalst   __main__t   ExtFilet   InitializeClassRª   t   DateTimet   typest   IntTypeR–   R   Rb   R   t   os.patht   joinR\   t	   cStringIOt   StringIOR¬   R   R   R   R«   t   manage_addExtImageFormR"   R&   R   (   Rµ   R³   Rº   R   R\   R–   R   R   R&   R­   R   R²   Rª   R   R®   R·   R¬   R¹   R   R¯   Rb   (    (    R%   t   ?   s.   )	  -ÿ  