Ñò
Tm£>c           @   s  d  Z  d d d !Z d d d !Z d d d !Z d d k l Z d d	 k Z d d	 k Z d d	 k Z d d	 k	 Z	 d d
 k
 l Z d d k l Z d d k l Z l Z e d e ƒ  ƒ Z e d e ƒ  ƒ Z d d d d d d	 d	 d „ Z d „  Z d d d d d d	 d	 d „ Z d	 S(   s   
$RCSfile: CreationDialog.py,v $

Implements the GUI mini-workflow invoked from the Zope Management
Interface to create an instance of ExternalFile.  This source code has
been factored out of ExternalFile into this separate module for the
reasons listed below:

1. This code is not strictly necessary to use ExternalFile, and in
certain cases (for example, when creating instances programmatically)
one prefers to bypass this code altogether and call the ExternalFile
constructor directly.

2. There are a great many possible error conditions or alternative
scenarios that may occur when creating an instance of ExternalFile TTW
(through the web), resulting in many lines of code.  This code was
threatening to obscure the actual implementation code when it was all
contained in the same module.

3. Zope3 introduces an architecture where GUI handling code and Zope
object implementation code are kept separate and knit together
declaratively through the use of a deployment descriptor.  This allows
alternative GUIs to be plugged in and makes the code much more
general.  For example, it could be used in non-Zope environments.
Creating a separate module for GUI stuff moves us in that direction.

Author: <a href="mailto:cstrong@arielpartners.com">Craeg Strong</a>
Release: 1.2
s	   $Name:  $i   iþÿÿÿs   $Date: 2003/04/21 01:52:57 $s   $Revision: 1.8 $i
   iÿÿÿÿ(   t   StringION(   t   MessageDialog(   t   PageTemplateFile(   t	   copy_filet   create_files   www/confirm_create.pts   www/conflict_create.ptt    c         C   s<  d } | d }	 | p t  d d d d d | ƒ S| pH | } | t t i | d ƒ t i | d ƒ t i | d	 ƒ ƒ d
 } n t i i | | ƒ }
 | p | i ot i i |
 ƒ pÄ d |
 t	 i
 ƒ  f } |  i d |	 d | d | d | d | d | d | d d d d ƒ 	Sy t |
 d ƒ } Wq§t j
 oH } d |
 t	 i
 ƒ  | f } | i d d ƒ t  d d d | d | ƒ SXq8t |  |	 | | | | | d d d | ƒSnŽt i ƒ  } | i ƒ  } | p7 d | i } | i d d ƒ t  d d d | d | ƒ St | ƒ } t | | ƒ t i i |
 ƒ pZ d |
 t	 i
 ƒ  f } |  i d |	 d | d | d | d | d | d | d | d | i ƒ 	Sy t |
 d ƒ } | i ƒ  WnT t j
 oH } d |
 t	 i
 ƒ  | f } | i d d ƒ t  d d d | d | ƒ SX|  i d |	 d | d | d | d | d | d | d | i ƒ Sd S(   sc  
    Workhorse method called from create.pt form.  Actually does the
    work of confirming the user's intended (possibly destructive)
    actions, creating the target file if necessary, and calling
    create_object to actually create the object.  This method may be
    called from create callbacks of derived classes to do the heavy
    lifting.  
    t   manage_addExternalFileFormt   actiont   titlet   ERRORt   messages#   You must specify a target filepath.t   /s   \t   :i   si   File %s does not exist yet on server %s.
            Would you like to create a blank file by that name?
t   file_idt
   file_titlet   file_descriptiont   basedirt   target_filepatht   temp_filenameR   t   upload_filenamet   rbsM   Target file %s exists on server %s
                but is not readable (%s).
s   
s   <br/>t   upload_filet   REQUESTsH   Uploaded file %s is empty.
            Please specify a non-empty file.
s˜   File %s does not exist yet on server %s.
                Would you like to create a file by that name containing the uploaded contents?
                t   absF   Target file %s is not writable
                    on server %s (%s).
N(   R   t   maxt   stringt   rfindt   ost   patht   joint   filenamet   existst   sockett   gethostnamet   manage_confirmCreateObjectFormt   Nonet   opent   IOErrort   replacet   manage_createObjectt   tempfilet   mktempt   readR    R   t   closet   manage_conflictCreateObjectForm(   t   foldert   idR   t   descriptionR   R   R   R   t   tryagain_urlR   t   fully_resolved_target_filepathR
   t   testopent   valueR   t   upload_contentst   data(    (    s1   /srv/Zope/Products/ExternalFile/CreationDialog.pyt   manage_addObjectViaGui2   s    
		c
         C   s©  d }
 t  i i | | ƒ } t  i i | ƒ } d } | o» y t | | ƒ WnT t j
 oH } d | t i ƒ  | f } | i d d ƒ t	 d d d | d	 |
 ƒ SX| o, d
 | | t i ƒ  t i ƒ  | | f } qd | | t i ƒ  | f } n | pl y t d | ƒ Wqht j
 oH } d | t i ƒ  | f } | i d d ƒ t	 d d d | d	 |
 ƒ SXn d | | t i ƒ  f } t |  | | | | | | d d d |	 ƒS(   så   
    Create a new instance of ExternalFile pointing to target filepath
    on server.  If user has uploaded a file (upload_file is not None),
    overwrite the contents of the server file with the uploaded file
    contents.
    R   R   s>   Target file %s is not writable
            on server %s (%s).
s   
s   <br/>R   R	   R
   R   sÐ   Object instance %s successfully created,
            pointing to file %s on %s.
            Contents of %s:%s
            were replaced by contents of uploaded file %s.
            File successfully created.
s½   Object instance %s successfully created,
            pointing to newly-created file %s on %s,
            with contents copied from uploaded file %s.
            File successfully created.
sB   Target file %s is not writable
                on server %s (%s).
so   Object instance %s successfully created,
        pointing to file %s on %s.
        File successfully created.
R   R   N(   R   R   R   R   R   R%   R    R!   R&   R   R#   R'   (   R-   R   R   R   R   R   R   R   R   R   R0   R1   t   target_is_existing_filet
   create_msgR3   R
   (    (    s1   /srv/Zope/Products/ExternalFile/CreationDialog.pyt   manage_confirmCreateObject·   sJ    	c	      
   C   s@  t  | ƒ t  g  ƒ j o | d }	 n$ t  | ƒ t  d ƒ j o
 | }	 n y¨ |  i | | | | | | ƒ | d j o d Sy |  i ƒ  }
 Wn | d }
 n X|	 i d ƒ d j o( d |
 | f }
 | i i |
 d ƒ n | i i |
 d	 ƒ WnG t j
 o; } t | ƒ } | i	 d
 d ƒ t
 d d d | d d ƒ SXd S(   s®   
    Factory method to create an instance of ExternalFile, called
    from a GUI in the Zope Management Interface.  It calls
    addExternalFile to actually do the work.
    i    R   Nt   URL1t   Editiÿÿÿÿs   %s/%ss   /manage_editForms   /manage_mains   
s   <br/>R   t   ErrorR
   R   t   manage_main(   t   typet   addExternalFileR#   t   DestinationURLt   findt   RESPONSEt   redirectt	   Exceptiont   strR&   R   (   R-   R   R.   R   R/   R   R   R   R   t   actt   urlt   eR
   (    (    s1   /srv/Zope/Products/ExternalFile/CreationDialog.pyR'   ù   s0    


(   t   __doc__t
   __cvstag__t   __date__t   __version__t	   cStringIOR    R   R    R   R(   t   GlobalsR   t'   Products.PageTemplates.PageTemplateFileR   t	   FileUtilsR   R   t   globalsR"   R,   R#   R6   R9   R'   (    (    (    s1   /srv/Zope/Products/ExternalFile/CreationDialog.pyt   <module>   s&   0		‚	B