Changeset 4314

Show
Ignore:
Timestamp:
09/22/08 04:48:38 (2 months ago)
Author:
Blackhex
Message:

ScreenshotsPlugin:

  • Replacement of screenshot image is now possible.
  • Request for missing screenshot with SCREENSHOTS_ADMIN permission returns add screenshot form.
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • screenshotsplugin/0.11/tracscreenshots/api.py

    r4304 r4314  
    244244        self._edit_item(context, 'screenshot', id, tmp_screenshot) 
    245245 
    246  
    247246    # Delete item functions 
    248247 
  • screenshotsplugin/0.11/tracscreenshots/core.py

    r4307 r4314  
    179179                screenshot = api.get_screenshot(context, screenshot_id) 
    180180 
    181                 if screenshot: 
    182                     # Set missing dimensions. 
    183                     width = width or screenshot['width'] 
    184                     height = height or screenshot['height'] 
    185  
    186                     if format == 'html': 
    187                         # Format screenshot for presentation. 
    188                         screenshot['author'] = format_to_oneliner(self.env, context, 
    189                           screenshot['author']) 
    190                         screenshot['name'] = format_to_oneliner(self.env, context, 
    191                           screenshot['name']) 
    192                         screenshot['description'] = format_to_oneliner(self.env, 
    193                           context, screenshot['description']) 
    194                         screenshot['time'] = pretty_timedelta(to_datetime( 
    195                           screenshot['time'], utc)) 
    196  
    197                         # For HTML preview format return template. 
    198                         self.data['screenshot'] = screenshot 
    199                         return ('screenshot', None) 
     181                # Check if requested screenshot exists. 
     182                if not screenshot: 
     183                    if context.req.perm.has_permission('SCREENSHOTS_ADMIN'): 
     184                        context.req.redirect(context.req.href.screenshots( 
     185                          action = 'add')) 
    200186                    else: 
    201                         # Prepare screenshot filename. 
    202                         name, ext = os.path.splitext(screenshot['file']) 
    203                         format = (format == 'raw') and ext or '.' + format 
    204                         path = os.path.join(self.path, to_unicode( 
    205                           screenshot['id'])) 
    206                         filename = os.path.join(path, '%s-%sx%s%s' % (name, 
    207                           width, height, format)) 
    208                         orig_name = os.path.join(path, '%s-%sx%s%s' % (name, 
    209                           screenshot['width'], screenshot['height'], ext)) 
    210                         self.log.debug('filemame: %s' % (filename,)) 
    211  
    212                         # Create requested file from original if not exists. 
    213                         if not os.path.exists(filename): 
    214                             self._create_image(orig_name, path, name, format, 
    215                               width, height) 
    216  
    217                         # Send file to request. 
    218                         context.req.send_header('Content-Disposition', 
    219                           'attachment;filename=%s' % (os.path.basename( 
    220                           filename))) 
    221                         context.req.send_header('Content-Description', 
    222                           screenshot['description']) 
    223                         context.req.send_file(filename, mimetypes.guess_type(filename) 
    224                           [0]) 
     187                        raise TracError('Screenshot not found.') 
     188 
     189                # Set missing dimensions. 
     190                width = width or screenshot['width'] 
     191                height = height or screenshot['height'] 
     192 
     193                if format == 'html': 
     194                    # Format screenshot for presentation. 
     195                    screenshot['author'] = format_to_oneliner(self.env, context, 
     196                      screenshot['author']) 
     197                    screenshot['name'] = format_to_oneliner(self.env, context, 
     198                      screenshot['name']) 
     199                    screenshot['description'] = format_to_oneliner(self.env, 
     200                      context, screenshot['description']) 
     201                    screenshot['time'] = pretty_timedelta(to_datetime( 
     202                      screenshot['time'], utc)) 
     203 
     204                    # For HTML preview format return template. 
     205                    self.data['screenshot'] = screenshot 
     206                    return ('screenshot', None) 
    225207                else: 
    226                     raise TracError('Screenshot not found.') 
     208                    # Prepare screenshot filename. 
     209                    name, ext = os.path.splitext(screenshot['file']) 
     210                    format = (format == 'raw') and ext or '.' + format 
     211                    path = os.path.join(self.path, to_unicode( 
     212                      screenshot['id'])) 
     213                    filename = os.path.join(path, '%s-%sx%s%s' % (name, 
     214                      width, height, format)) 
     215                    orig_name = os.path.join(path, '%s-%sx%s%s' % (name, 
     216                      screenshot['width'], screenshot['height'], ext)) 
     217 
     218                    self.log.debug('filemame: %s' % (filename,)) 
     219 
     220                    # Create requested file from original if not exists. 
     221                    if not os.path.exists(filename): 
     222                        self._create_image(orig_name, path, name, format, 
     223                          width, height) 
     224 
     225                    # Send file to request. 
     226                    context.req.send_header('Content-Disposition', 
     227                      'attachment;filename=%s' % (os.path.basename( 
     228                      filename))) 
     229                    context.req.send_header('Content-Description', 
     230                      screenshot['description']) 
     231                    context.req.send_file(filename, mimetypes.guess_type(filename) 
     232                      [0]) 
    227233 
    228234            elif action == 'add': 
     
    247253                name, ext = os.path.splitext(filename) 
    248254                filename = name + ext.lower() 
    249  
    250                 # Check correct file type. 
    251                 reg = re.compile(r'^(.*)[.](.*)$') 
    252                 result = reg.match(filename) 
    253                 if result: 
    254                     if not result.group(2).lower() in self.ext: 
    255                         raise TracError('Unsupported uploaded file type.') 
    256                 else: 
    257                     raise TracError('Unsupported uploaded file type.') 
    258255 
    259256                # Create image object. 
     
    300297 
    301298                # Prepare file paths 
     299                name, ext = os.path.splitext(screenshot['file']) 
    302300                path = os.path.join(self.path, unicode(screenshot['id'])) 
    303                 filepath = os.path.join(path, '%s-%ix%i.%s' % (result.group(1)
    304                   screenshot['width'], screenshot['height'], result.group(2))) 
     301                filepath = os.path.join(path, '%s-%ix%i%s' % (name
     302                  screenshot['width'], screenshot['height'], ext)) 
    305303                path = os.path.normpath(path) 
    306304                filepath = os.path.normpath(filepath) 
     305 
    307306                self.log.debug('path: %s' % (path,)) 
    308307                self.log.debug('filename: %s' % (filepath,)) 
     
    317316                except Exception, error: 
    318317                    api.delete_screenshot(context, screenshot['id']) 
    319                     try: 
    320                        self.log.debug(error) 
    321                     except: 
    322                        pass 
    323318                    try: 
    324319                        os.remove(filename) 
     
    366361                      'Screenshot not found.') 
    367362 
     363                # Get image file from request. 
     364                image = context.req.args['image'] 
     365                if hasattr(image, 'filename') and image.filename: 
     366                    in_file, filename = self._get_file_from_req(context.req) 
     367                    name, ext = os.path.splitext(filename) 
     368                    filename = name + ext.lower() 
     369                else: 
     370                    filename = None 
     371 
    368372                # Construct screenshot dictionary from form values. 
    369373                screenshot = {'name' :  context.req.args.get('name'), 
     
    374378                              'components' : context.req.args.get( 
    375379                                'components') or [], 
    376                               'versions' : context.req.args.get('versions') or []} 
     380                              'versions' : context.req.args.get('versions') or \ 
     381                                []} 
     382 
     383                # Update dimensions and filename if image file is updated. 
     384                if filename: 
     385                    image = Image.open(in_file) 
     386                    screenshot['file'] = filename 
     387                    screenshot['width'] = image.size[0] 
     388                    screenshot['height'] = image.size[1] 
    377389 
    378390                # Convert components and versions to list if only one item is 
     
    383395                     screenshot['versions'] = [screenshot['versions']] 
    384396 
     397                self.log.debug(screenshot) 
     398 
    385399                # Edit screenshot. 
    386400                api.edit_screenshot(context, screenshot_id, screenshot) 
    387401 
     402                # Prepare file paths. 
     403                if filename: 
     404                    name, ext = os.path.splitext(screenshot['file']) 
     405                    path = os.path.join(self.path, unicode(screenshot_id)) 
     406                    filepath = os.path.join(path, '%s-%ix%i%s' % (name, 
     407                      screenshot['width'], screenshot['height'], ext)) 
     408                    path = os.path.normpath(path) 
     409                    filepath = os.path.normpath(filepath) 
     410 
     411                    self.log.debug('path: %s' % (path,)) 
     412                    self.log.debug('filepath: %s' % (filepath,)) 
     413 
     414                    # Delete present images. 
     415                    try: 
     416                        for file in os.listdir(path): 
     417                            file = os.path.join(path, file) 
     418                            file = os.path.normpath(file) 
     419                            os.remove(file) 
     420                    except Exception, error: 
     421                        raise TracError('Error deleting screenshot. Original' \ 
     422                          ' message was: %s' % (error,)) 
     423 
     424                    # Store uploaded image. 
     425                    try: 
     426                        out_file = open(filepath, 'wb+')  
     427                        in_file.seek(0) 
     428                        shutil.copyfileobj(in_file, out_file) 
     429                        out_file.close() 
     430                    except Exception, error: 
     431                        try: 
     432                            os.remove(filename) 
     433                        except: 
     434                            pass 
     435                        raise TracError('Error storing file. Is directory' \ 
     436                          ' specified in path config option in [screenshots]' \ 
     437                          ' section of trac.ini existing? Original message was: %s' \ 
     438                          % (error,)) 
    388439 
    389440                # Notify change listeners. 
     
    392443                      old_screenshot) 
    393444 
    394                 # Clear id to prevent display of edit and delete button. 
     445                # Clear ID to prevent display of edit and delete button. 
    395446                context.req.args['id'] = None 
    396447 
     
    408459                    raise TracError('Deleted screenshot not found.', 
    409460                      'Screenshot not found.') 
     461 
     462                # Delete screenshot. 
     463                api.delete_screenshot(context, screenshot['id']) 
     464 
     465                # Delete screenshot files. Don't append any other files there :-). 
     466                path = os.path.join(self.path, to_unicode(screenshot['id'])) 
     467                path = os.path.normpath(path) 
     468 
     469                self.log.debug('path: %s' % (path,)) 
     470 
    410471                try: 
    411                     # Delete screenshot. 
    412                     api.delete_screenshot(context, screenshot['id']) 
    413  
    414                     # Delete screenshot files. Don't append any other files there :-). 
    415                     path = os.path.join(self.path, to_unicode(screenshot['id'])) 
    416                     path = os.path.normpath(path) 
    417                     self.log.debug('path: %s' % (path,)) 
    418472                    for file in os.listdir(path): 
    419473                        file = os.path.join(path, file) 
     
    527581            raise TracError('Can\'t upload empty file.') 
    528582        filename = os.path.basename(image.filename).decode('utf-8') 
    529         self.log.debug(filename) 
     583 
     584        # Check correct file type. 
     585        reg = re.compile(r'^(.*)[.](.*)$') 
     586        result = reg.match(filename) 
     587        if result: 
     588            if not result.group(2).lower() in self.ext: 
     589                raise TracError('Unsupported uploaded file type.') 
     590        else: 
     591            raise TracError('Unsupported uploaded file type.') 
     592 
    530593        return image.file, filename 
    531594 
  • screenshotsplugin/0.11/tracscreenshots/templates/screenshot-add.html

    r3271 r4314  
    2525            <input type="text" id="description" name="description" value="${(req.args.action == 'edit') and screenshots.screenshot.description or ''}"/><br/> 
    2626          </div> 
    27           <div py:if="req.args.action == 'add'" class="field"> 
     27          <div class="field"> 
    2828            <label for="image">Image File:</label><br/> 
    2929            <input type="file" id="image" name="image" value=""/><br/>