Edgewall Software

Ticket #6361: miltiFileAttach.patch

File miltiFileAttach.patch, 8.0 KB (added by Slava Zanko <slavazanko@…>, 5 years ago)

for revision 6164

  • trac/attachment.py

     
    504504                     parent=get_resource_name(self.env, resource.parent)) 
    505505 
    506506    # Internal methods 
    507  
    508     def _do_save(self, req, attachment): 
     507    def _do_save_one(self, req, attachment, upload, description): 
    509508        req.perm(attachment.resource).require('ATTACHMENT_CREATE') 
    510509 
    511         if 'cancel' in req.args: 
    512             req.redirect(get_resource_url(self.env, attachment.resource.parent, 
    513                                           req.href)) 
    514510 
    515         upload = req.args['attachment'] 
    516511        if not hasattr(upload, 'filename') or not upload.filename: 
    517512            raise TracError(_('No file uploaded')) 
    518513        if hasattr(upload.file, 'fileno'): 
     
    540535            raise TracError(_('No file uploaded')) 
    541536        # Now the filename is known, update the attachment resource 
    542537        # attachment.filename = filename 
    543         attachment.description = req.args.get('description', '') 
     538        attachment.description = description 
    544539        attachment.author = get_reporter_id(req, 'author') 
    545540        attachment.ipnr = req.remote_addr 
    546541 
     
    569564            attachment.filename = None 
    570565        attachment.insert(filename, upload.file, size) 
    571566 
    572         req.redirect(get_resource_url(self.env, attachment.resource(id=None), 
     567    def _do_save_many(self,req, attachment): 
     568        curr_attach = 0 
     569        while curr_attach < int(req.args['multiple_attachments']): 
     570            upload=req.args['attachment-%d' % curr_attach] 
     571            if hasattr(upload, 'filename') and upload.filename: 
     572                attachment.filename = None 
     573                self._do_save_one(req, attachment, upload, req.args.get('description-%d' % curr_attach, '')) 
     574            curr_attach=curr_attach+1 
     575 
     576    def _do_save(self, req, attachment): 
     577        req.perm(attachment.resource).require('ATTACHMENT_CREATE') 
     578 
     579        if 'cancel' in req.args: 
     580            req.redirect(get_resource_url(self.env, attachment.resource.parent, 
     581                                          req.href)) 
     582 
     583        if 'multiple_attachments' in req.args: 
     584            self._do_save_many(req, attachment) 
     585            req.redirect(get_resource_url(self.env, attachment.resource(id=None), 
    573586                                      req.href)) 
     587#           req.redirect(context.parent.resource_href()) 
     588        else: 
     589            self._do_save_one(req, attachment, req.args['attachment'], req.args.get('description', '')) 
     590            # Redirect the user to list of attachments (must add a trailing '/') 
     591            req.redirect(get_resource_url(self.env, attachment.resource(id=None), 
     592                                      req.href)) 
    574593 
     594 
     595 
     596#----------------------------------------------------------------------------------------------- 
     597 
    575598    def _do_delete(self, req, attachment): 
    576599        req.perm(attachment.resource).require('ATTACHMENT_DELETE') 
    577600 
  • trac/htdocs/js/trac.js

     
    6969function getAncestorByTagName(elem, tagName) { 
    7070  return $(elem).parents(tagName).get(0); 
    7171} 
     72var ATTACHFILE_COUNTER=0; 
     73 
     74function manageMultipleAttachFields(_obj){ 
     75    var count = document.getElementById("multiAttach_count"); 
     76    if (_obj.value == '') { 
     77        if($("#multiAttach_tbody").get(0).rows.length == 1) return; 
     78        $("#multiAttach_tbody").get(0).deleteRow(_obj.getAttribute("attachNum")*1); 
     79        ATTACHFILE_COUNTER=0; 
     80        $("#multiAttach_tbody tr").each(function(index, element){ 
     81            $("input", $("td", element).get(0)) 
     82                .attr("attachnum",ATTACHFILE_COUNTER) 
     83                .attr("id","multiAttach_attachment"+ATTACHFILE_COUNTER) 
     84                .get(0).name = "attachment-"+ATTACHFILE_COUNTER; 
     85 
     86            $("input", $("td", element).get(0)) 
     87                .attr("id","multiAttach_description"+ATTACHFILE_COUNTER) 
     88                .get(0).name = "description-"+ATTACHFILE_COUNTER; 
     89 
     90            ATTACHFILE_COUNTER++; 
     91        }); 
     92        count.value = count.value*1-1; 
     93    } else { 
     94        if ($(_obj).attr("attachnum") != count.value-1) return; 
     95        var tr = $("#multiAttach_tbody").get(0).insertRow(-1); 
     96        var td = tr.insertCell(-1); 
     97        td.innerHTML='<input type="file" id="multiAttach_attachment-'+count.value+'" attachnum="'+count.value+'" name="attachment-'+count.value+'" onchange="manageMultipleAttachFields(this);"/>'; 
     98        var td = tr.insertCell(-1); 
     99        td.innerHTML='<input type="text" id="multiAttach_description-'+count.value+'" name="description-'+count.value+'" size="60" />'+"\n"; 
     100        count.value = count.value*1+1; 
     101    } 
     102} 
     103 No newline at end of file 
  • trac/__init__.py

     
    1212# history and logs, available at http://trac.edgewall.org/log/. 
    1313 
    1414__version__ = __import__('pkg_resources').get_distribution('Trac').version 
     15 
  • trac/templates/attachment.html

     
    1313  <body py:with="parent = attachments and attachments.parent or 
    1414                          attachment.resource.parent"> 
    1515 
    16     <div id="ctxtnav" class="nav"> 
    17       <h2>Attachment Navigation</h2> 
    18       <ul> 
    19         <li class="last"> 
    20           <a href="${chrome.links.up[0].href}">${_("Back to %(parent)s", parent=name_of(parent))}</a> 
    21         </li> 
    22       </ul> 
    23     </div> 
    24  
    25     <div py:choose="mode" id="content" class="attachment"> 
    26       <py:when test="'new'"> 
    27         <h1>Add Attachment to <a href="${url_of(parent)}">${name_of(parent)}</a></h1> 
    28         <form id="attachment" method="post" enctype="multipart/form-data" action=""> 
     16    <py:def function="attach_oneFile()"> 
    2917          <div class="field"> 
    3018            <label>File:<br /><input type="file" name="attachment" /></label> 
    3119          </div> 
     
    4937            </div> 
    5038            <br /> 
    5139          </fieldset> 
     40     
     41    </py:def> 
     42    <py:def function="attach_multiFile()"> 
     43         <table><thead> 
     44          <tr><th align="left">File</th><th align="left">Description</th></tr> 
     45         </thead> 
     46        <tbody id="multiAttach_tbody"> 
     47        <tr> 
     48            <td><input type="file" id="multiAttach_attachment-0" attachnum="0" name="attachment-0" onchange="manageMultipleAttachFields(this);"/></td> 
     49            <td><input type="text" id="multiAttach_description-0" name="description-0" size="60" /></td> 
     50        </tr> 
     51        </tbody></table> 
     52        <input type="hidden" id="multiAttach_count" name="multiple_attachments" value="1" /> 
     53 
     54          <fieldset> 
     55            <legend>Attachment Info</legend> 
     56            <py:if test="authname == 'anonymous'"> 
     57              <div class="field"> 
     58                <label>Your email or username:<br /> 
     59                  <input type="text" name="author" size="30" value="$author" /> 
     60                </label> 
     61              </div> 
     62              </py:if> 
     63            <div class="options"> 
     64              <label><input type="checkbox" name="replace" /> 
     65                Replace existing attachments of the same name</label> 
     66            </div> 
     67            <br /> 
     68          </fieldset> 
     69     
     70    </py:def> 
     71 
     72    <div id="ctxtnav" class="nav"> 
     73      <h2>Attachment Navigation</h2> 
     74      <ul> 
     75        <li class="last"> 
     76          <a href="${chrome.links.up[0].href}">${_("Back to %(parent)s", parent=name_of(parent))}</a> 
     77        </li> 
     78      </ul> 
     79    </div> 
     80 
     81    <div py:choose="mode" id="content" class="attachment"> 
     82      <py:when test="'new'"> 
     83        <h1>Add Attachment to <a href="${url_of(parent)}">${name_of(parent)}</a></h1> 
     84        <form id="attachment" method="post" enctype="multipart/form-data" action=""> 
     85         
     86        ${attach_multiFile()} 
    5287          <div class="buttons"> 
    5388            <input type="hidden" name="action" value="new" /> 
    5489            <input type="hidden" name="realm" value="$parent.realm" /> 
     
    5792            <input type="submit" name="cancel" value="Cancel" /> 
    5893          </div> 
    5994        </form> 
     95 
    6096      </py:when> 
    6197 
    6298      <py:when test="'delete'">