Edgewall Software

Ticket #2141: multiFileAttach.r9435.patch

File multiFileAttach.r9435.patch, 7.5 KB (added by o.shirochenkov@…, 23 months ago)

This patch is broken

  • trac/attachment.py

     
    594594 
    595595    # Internal methods 
    596596 
    597     def _do_save(self, req, attachment): 
     597    def _do_save_one(self, req, attachment, upload, description): 
    598598        req.perm(attachment.resource).require('ATTACHMENT_CREATE') 
    599599 
    600         if 'cancel' in req.args: 
    601             req.redirect(get_resource_url(self.env, attachment.resource.parent, 
    602                                           req.href)) 
    603  
    604         upload = req.args['attachment'] 
    605600        if not hasattr(upload, 'filename') or not upload.filename: 
    606601            raise TracError(_('No file uploaded')) 
    607602        if hasattr(upload.file, 'fileno'): 
     
    629624            raise TracError(_('No file uploaded')) 
    630625        # Now the filename is known, update the attachment resource 
    631626        # attachment.filename = filename 
    632         attachment.description = req.args.get('description', '') 
     627        attachment.description = description 
    633628        attachment.author = get_reporter_id(req, 'author') 
    634629        attachment.ipnr = req.remote_addr 
    635630 
     
    667662            attachment.filename = None 
    668663        attachment.insert(filename, upload.file, size) 
    669664 
     665    def _do_save_many(self,req, attachment): 
     666        curr_attach = 0 
     667    while curr_attach < int(req.args['multiple_attachments']): 
     668        upload=req.args['attachment-%d' % curr_attach] 
     669        if hasattr(upload, 'filename') and upload.filename: 
     670            attachment.filename = None 
     671        self._do_save_one(req, attachment, upload, req.args.get('description-%d' % curr_attach, '')) 
     672        curr_attach=curr_attach+1 
     673 
     674    def _do_save(self, req, attachment): 
     675        req.perm(attachment.resource).require('ATTACHMENT_CREATE') 
     676 
     677        if 'cancel' in req.args: 
     678            req.redirect(get_resource_url(self.env, attachment.resource.parent, 
     679                                          req.href)) 
     680 
     681    if 'multiple_attachments' in req.args: 
     682        self._do_save_many(req, attachment) 
     683        req.redirect(get_resource_url(self.env, attachment.resource(id=None), 
     684                                      req.href)) 
     685    else: 
     686        self._do_save_one(req, attachment, req.args['attachment'], req.args.get('description', '')) 
     687        # Redirect the user to list of attachments (must add a trailing '/') 
    670688        req.redirect(get_resource_url(self.env, attachment.resource(id=None), 
    671689                                      req.href)) 
    672690 
  • trac/htdocs/js/trac.js

     
    105105  } 
    106106 
    107107})(jQuery); 
     108 
     109var ATTACHFILE_COUNTER=0; 
     110function manageMultipleAttachFields(_obj){ 
     111    if (_obj.value == '') { 
     112        if($("#multiAttach_tbody").get(0).rows.length == 1) return; 
     113      $("#multiAttach_tbody").get(0).deleteRow($(_obj).attr("attachnum")*1); 
     114      ATTACHFILE_COUNTER=0; 
     115      $("#multiAttach_tbody tr").each(function(index, element){ 
     116          $("input", $("td", element).get(0)) 
     117              .attr("attachnum",ATTACHFILE_COUNTER) 
     118              .get(0).name = "attachment-"+ATTACHFILE_COUNTER; 
     119          $("input", $("td", element).get(1)) 
     120              .get(0).name = "description-"+ATTACHFILE_COUNTER; 
     121          ATTACHFILE_COUNTER++; 
     122      }); 
     123      $("#multiAttach_count").get(0).value = $("#multiAttach_count").val()*1-1; 
     124    } else { 
     125      if ($(_obj).attr("attachnum") != $("#multiAttach_count").val()-1) return; 
     126      var tr = $("#multiAttach_tbody").get(0).insertRow(-1); 
     127      var td = tr.insertCell(-1); 
     128      $('<input type="file" onchange="manageMultipleAttachFields(this)">') 
     129         .attr("attachnum",$("#multiAttach_count").val()) 
     130         .appendTo(td) 
     131         .get(0).name = "attachment-"+$("#multiAttach_count").val(); 
     132 
     133      td = tr.insertCell(-1); 
     134      $('<input type="text">') 
     135          .attr("size",60) 
     136          .appendTo(td) 
     137          .get(0).name = "description-"+$("#multiAttach_count").val(); 
     138      $("#multiAttach_count").get(0).value = $("#multiAttach_count").val()*1+1; 
     139    } 
     140} 
  • trac/templates/attachment.html

     
    2424  </head> 
    2525 
    2626  <body> 
    27     <div py:choose="mode" id="content" class="attachment"> 
    28       <py:when test="'new'"> 
    29         <h1>Add Attachment to <a href="${url_of(parent)}">${name_of(parent)}</a></h1> 
    30         <form id="attachment" method="post" enctype="multipart/form-data" action=""> 
     27    <py:def function="attach_oneFile()"> 
     28      <div class="field"> 
     29        <label>File:<br /><input type="file" name="attachment" /></label> 
     30      </div> 
     31      <fieldset> 
     32        <legend>Attachment Info</legend> 
     33        <py:if test="authname == 'anonymous'"> 
    3134          <div class="field"> 
    32             <label>File<py:if test="max_size >= 0"> (size limit 
    33                   ${pretty_size(max_size)})</py:if>:<br /> 
    34               <input type="file" name="attachment" /></label> 
     35            <label>Your email or username:<br /> 
     36              <input type="text" name="author" size="30" value="$author" /> 
     37            </label> 
    3538          </div> 
     39          </py:if> 
     40        <div class="field"> 
     41          <label>Description of the file (optional):<br /> 
     42            <input type="text" name="description" size="60" /></label> 
     43        </div> 
     44        <br /> 
     45        <div class="options"> 
     46          <label><input type="checkbox" name="replace" /> 
     47            Replace existing attachment of the same name</label> 
     48        </div> 
     49        <br /> 
     50      </fieldset> 
     51    </py:def> 
     52    <py:def function="attach_multiFile()"> 
     53     <table><thead> 
     54          <tr><th align="left">File</th><th align="left">Description</th></tr> 
     55     </thead> 
     56    <tbody id="multiAttach_tbody"> 
     57        <tr> 
     58            <td><input type="file" attachnum="0" name="attachment-0" onchange="manageMultipleAttachFields(this);"/></td> 
     59            <td><input type="text" name="description-0" size="60" /></td> 
     60        </tr> 
     61        </tbody></table> 
     62        <input type="hidden" id="multiAttach_count" name="multiple_attachments" value="1" /> 
     63 
    3664          <fieldset> 
    3765            <legend>Attachment Info</legend> 
    3866            <py:if test="authname == 'anonymous'"> 
     
    4270                </label> 
    4371              </div> 
    4472              </py:if> 
    45             <div class="field"> 
    46               <label>Description of the file (optional):<br /> 
    47                 <input type="text" name="description" size="60" /></label> 
    48             </div> 
    49             <br /> 
    5073            <py:if test="authname and authname != 'anonymous'"> 
    5174              <div class="options"> 
    5275                <label><input type="checkbox" name="replace" /> 
    53                   Replace existing attachment of the same name</label> 
     76                  Replace existing attachments of the same name</label> 
    5477              </div> 
    5578              <br /> 
    5679            </py:if> 
    5780          </fieldset> 
     81    </py:def> 
     82 
     83    <div py:choose="mode" id="content" class="attachment"> 
     84      <py:when test="'new'"> 
     85        <h1>Add Attachment to <a href="${url_of(parent)}">${name_of(parent)}</a></h1> 
     86        <form id="attachment" method="post" enctype="multipart/form-data" action=""> 
     87         
     88        ${attach_multiFile()} 
    5889          <div class="buttons"> 
    5990            <input type="hidden" name="action" value="new" /> 
    6091            <input type="hidden" name="realm" value="$parent.realm" />