Edgewall Software

Ticket #2141: miltiFileAttach.r6253.patch

File miltiFileAttach.r6253.patch, 7.1 KB (added by Slava Zanko <slavaz>, 12 months ago)

renew patch

  • trac/attachment.py

     
    506506 
    507507    # Internal methods 
    508508 
    509     def _do_save(self, req, attachment): 
     509    def _do_save_one(self, req, attachment, upload, description): 
    510510        req.perm(attachment.resource).require('ATTACHMENT_CREATE') 
    511511 
    512         if 'cancel' in req.args: 
    513             req.redirect(get_resource_url(self.env, attachment.resource.parent, 
    514                                           req.href)) 
    515512 
    516         upload = req.args['attachment'] 
    517513        if not hasattr(upload, 'filename') or not upload.filename: 
    518514            raise TracError(_('No file uploaded')) 
    519515        if hasattr(upload.file, 'fileno'): 
     
    541537            raise TracError(_('No file uploaded')) 
    542538        # Now the filename is known, update the attachment resource 
    543539        # attachment.filename = filename 
    544         attachment.description = req.args.get('description', '') 
     540        attachment.description = description 
    545541        attachment.author = get_reporter_id(req, 'author') 
    546542        attachment.ipnr = req.remote_addr 
    547543 
     
    570566            attachment.filename = None 
    571567        attachment.insert(filename, upload.file, size) 
    572568 
    573         req.redirect(get_resource_url(self.env, attachment.resource(id=None), 
     569    def _do_save_many(self,req, attachment): 
     570        curr_attach = 0 
     571        while curr_attach < int(req.args['multiple_attachments']): 
     572            upload=req.args['attachment-%d' % curr_attach] 
     573            if hasattr(upload, 'filename') and upload.filename: 
     574                attachment.filename = None 
     575                self._do_save_one(req, attachment, upload, req.args.get('description-%d' % curr_attach, '')) 
     576            curr_attach=curr_attach+1 
     577 
     578    def _do_save(self, req, attachment): 
     579        req.perm(attachment.resource).require('ATTACHMENT_CREATE') 
     580 
     581        if 'cancel' in req.args: 
     582            req.redirect(get_resource_url(self.env, attachment.resource.parent, 
     583                                          req.href)) 
     584 
     585        if 'multiple_attachments' in req.args: 
     586            self._do_save_many(req, attachment) 
     587            req.redirect(get_resource_url(self.env, attachment.resource(id=None), 
    574588                                      req.href)) 
     589        else: 
     590            self._do_save_one(req, attachment, req.args['attachment'], req.args.get('description', '')) 
     591            # Redirect the user to list of attachments (must add a trailing '/') 
     592            req.redirect(get_resource_url(self.env, attachment.resource(id=None), 
     593                                      req.href)) 
    575594 
    576595    def _do_delete(self, req, attachment): 
    577596        req.perm(attachment.resource).require('ATTACHMENT_DELETE') 
  • trac/htdocs/js/trac.js

     
    6969function getAncestorByTagName(elem, tagName) { 
    7070  return $(elem).parents(tagName).get(0); 
    7171} 
     72var ATTACHFILE_COUNTER=0; 
     73function manageMultipleAttachFields(_obj){ 
     74    if (_obj.value == '') { 
     75        if($("#multiAttach_tbody").get(0).rows.length == 1) return; 
     76        $("#multiAttach_tbody").get(0).deleteRow($(_obj).attr("attachnum")*1); 
     77        ATTACHFILE_COUNTER=0; 
     78        $("#multiAttach_tbody tr").each(function(index, element){ 
     79            $("input", $("td", element).get(0)) 
     80                .attr("attachnum",ATTACHFILE_COUNTER) 
     81                .get(0).name = "attachment-"+ATTACHFILE_COUNTER; 
     82            $("input", $("td", element).get(1)) 
     83                .get(0).name = "description-"+ATTACHFILE_COUNTER; 
     84            ATTACHFILE_COUNTER++; 
     85        }); 
     86        $("#multiAttach_count").get(0).value = $("#multiAttach_count").val()*1-1; 
     87    } else { 
     88        if ($(_obj).attr("attachnum") != $("#multiAttach_count").val()-1) return; 
     89        var tr = $("#multiAttach_tbody").get(0).insertRow(-1); 
     90        var td = tr.insertCell(-1); 
     91        $('<input type="file" onchange="manageMultipleAttachFields(this)">') 
     92           .attr("attachnum",$("#multiAttach_count").val()) 
     93           .appendTo(td) 
     94           .get(0).name = "attachment-"+$("#multiAttach_count").val(); 
     95 
     96        td = tr.insertCell(-1); 
     97        $('<input type="text">') 
     98            .attr("size",60) 
     99            .appendTo(td) 
     100            .get(0).name = "description-"+$("#multiAttach_count").val(); 
     101        $("#multiAttach_count").get(0).value = $("#multiAttach_count").val()*1+1; 
     102    } 
     103} 
  • 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" attachnum="0" name="attachment-0" onchange="manageMultipleAttachFields(this);"/></td> 
     49            <td><input type="text" 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" />