Changeset 3114

Show
Ignore:
Timestamp:
01/20/08 18:40:11 (1 year ago)
Author:
Blackhex
Message:
  • Port to 0.11 finished.
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • downloadsplugin/0.11/tracdownloads/api.py

    r3113 r3114  
    55 
    66from trac.core import * 
    7 from trac.config import Option 
     7from trac.config import Option, BoolOption      
    88from trac.web.chrome import add_stylesheet, add_script 
    99from trac.wiki.formatter import format_to_html, format_to_oneliner 
     
    5050      ' '.join(all_fields), 'List of downloads table fields that should' 
    5151      ' be visible to users on Downloads section.') 
     52    unique_filename = BoolOption('downloads', 'unique_filename', False, 
     53      'If enabled checks if uploaded file has unique name.') 
    5254 
    5355    # Get list functions. 
     
    121123          "WHERE time BETWEEN %s AND %s" 
    122124        self.log.debug(sql % (start, stop)) 
    123         context.cursor.execute(sql, (start, stop)) 
     125        context.cursor.execute(sql, (to_timestamp(start), to_timestamp(stop))) 
    124126        downloads = [] 
    125127        for row in context.cursor: 
     
    210212            return row 
    211213 
     214    def get_download_by_file(self, context, file): 
     215        columns = ('id', 'file', 'description', 'size', 'time', 'count', 
     216          'author', 'tags', 'component', 'version', 'architecture', 'platform', 
     217          'type') 
     218        sql = "SELECT id, file, description, size, time, count, author, tags," \ 
     219          " component, version, architecture, platform, type FROM download" \ 
     220          " WHERE file = %s" 
     221        self.log.debug(sql % (file,)) 
     222        context.cursor.execute(sql, (file,)) 
     223        for row in context.cursor: 
     224            row = dict(zip(columns, row)) 
     225            row['count'] = row['count'] or 0 
     226            return row 
     227 
    212228    def get_architecture(self, context, id): 
    213229        columns = ('id', 'name', 'description') 
     
    277293 
    278294    def edit_download(self, context, id, download): 
    279         self._edit_item(contex, 'download', id, download) 
     295        self._edit_item(context, 'download', id, download) 
    280296 
    281297    def edit_architecture(self, context, id, architecture): 
     
    422438                # Get form values. 
    423439                download_id = context.req.args.get('id') or 0 
     440                download_file = context.req.args.get('file') 
    424441 
    425442                # Get download. 
    426                 download = self.get_download(context, download_id) 
     443                if download_id: 
     444                    download = self.get_download(context, download_id) 
     445                else: 
     446                    download = self.get_download_by_file(context, download_file) 
    427447 
    428448                if download: 
     
    518538                            'platform' : context.req.args.get('platform'), 
    519539                            'type' : context.req.args.get('type')} 
     540 
     541                # Check for file name uniqueness. 
     542                if self.unique_filename: 
     543                    if self.get_download_by_file(context, filename): 
     544                        raise TracError('File with same name is already' \ 
     545                          ' uploaded and unique file names are enabled.') 
    520546 
    521547                # Add new download. 
     
    770796        if not hasattr(file, 'filename') or not file.filename: 
    771797            raise TracError('No file uploaded.') 
     798 
     799        # Get file size. 
    772800        if hasattr(file.file, 'fileno'): 
    773801            size = os.fstat(file.file.fileno())[6] 
  • downloadsplugin/0.11/tracdownloads/core.py

    r2999 r3114  
    6060            req.args['id'] = match.group(1) 
    6161            return True 
     62        match = re.match(r'''^/downloads/([^/]+)$''', req.path_info) 
     63        if match: 
     64            req.args['action'] = 'get-file' 
     65            req.args['file'] = match.group(1) 
     66            return True 
    6267        return False 
    6368 
  • downloadsplugin/0.11/tracdownloads/tags.py

    r2999 r3114  
    44 
    55from trac.core import * 
     6from trac.mimeview import * 
    67from trac.util.html import html 
    78 
     
    105106 
    106107    def _resolve_ids(self, download): 
    107  
    108         # Get database access. 
     108        # Create context. 
     109        context = Context('downloads-core') 
    109110        db = self.env.get_db_cnx() 
    110         cursor = db.cursor() 
     111        context.cursor = db.cursor() 
    111112 
    112113        # Resolve architecture platform and type names. 
    113114        api = self.env[DownloadsApi] 
    114         architecture = api.get_architecture(cursor, download['architecture']) 
    115         platform = api.get_platform(cursor, download['platform']) 
    116         type = api.get_type(cursor, download['type']) 
     115        architecture = api.get_architecture(context, download['architecture']) 
     116        platform = api.get_platform(context, download['platform']) 
     117        type = api.get_type(context, download['type']) 
    117118        self.log.debug(architecture) 
    118119        download['architecture'] = architecture['name'] 
  • downloadsplugin/0.11/tracdownloads/timeline.py

    r2999 r3114  
    33import time 
    44 
     5from genshi.builder import tag 
     6 
    57from trac.core import * 
     8from trac.mimeview import * 
    69from trac.wiki import wiki_to_html, wiki_to_oneliner 
    7 from trac.util import Markup 
     10from trac.util.text import pretty_size 
    811 
    9 from trac.Timeline import ITimelineEventProvider 
     12from trac.timeline import ITimelineEventProvider 
    1013 
    1114from tracdownloads.api import * 
     
    2023    # ITimelineEventProvider 
    2124 
     25    def get_timeline_filters(self, req): 
     26        if 'DOWNLOADS_VIEW' in req.perm: 
     27            yield ('downloads', 'Downloads changes') 
     28 
    2229    def get_timeline_events(self, req, start, stop, filters): 
    2330        self.log.debug("start: %s, stop: %s, filters: %s" % (start, stop, 
    2431          filters)) 
    25         if 'downloads' in filters: 
    26             # Create database context 
     32        if ('downloads' in filters) and ('DOWNLOADS_VIEW' in req.perm): 
     33            # Create context. 
     34            context = Context.from_request(req)('downloads-timeline') 
    2735            db = self.env.get_db_cnx() 
    28             cursor = db.cursor() 
     36            context.cursor = db.cursor() 
    2937 
    3038            # Get API component. 
    3139            api = self.env[DownloadsApi] 
    3240 
    33             format = req.args.get('format') 
    34             self.log.debug("format: %s" % (format)) 
     41            self.log.debug(api.get_new_downloads(context, start, stop)) 
    3542 
    3643            # Get message events 
    37             for download in api.get_new_downloads(req, cursor, start, stop): 
    38                 kind = 'newticket' 
     44            for download in api.get_new_downloads(context, start, stop): 
     45                yield ('newticket', download['time'], download['author'], 
     46                  (download['id'], download['file'], download['description'], 
     47                  download['size'])) 
    3948 
    40                 title = Markup("New download <em>%s</em> created by %s" % 
    41                   (download['file'], download['author'])) 
    42                 time = download['time'] 
    43                 author = download['author'] 
     49    def render_timeline_event(self, context, field, event): 
     50        # Decompose event data. 
     51        id, name, description, size = event[3] 
    4452 
    45                 if format == 'rss': 
    46                     href = req.abs_href.downloads(download['id']) 
    47                     message = wiki_to_html(download['description'], self.env, 
    48                       req) 
    49                 else: 
    50                     href = req.href.downloads(download['id']) 
    51                     message = wiki_to_oneliner(download['description'], self.env) 
    52  
    53                 yield kind, href, title, time, author, message 
    54  
    55     def get_timeline_filters(self, req): 
    56         if req.perm.has_permission('DOWNLOADS_VIEW'): 
    57             yield ('downloads', 'Downloads changes') 
     53        # Return apropriate content. 
     54        if field == 'url': 
     55           return context.req.href.downloads(id) 
     56        elif field == 'title': 
     57           return tag('New download ', tag.em(name), ' created') 
     58        elif field == 'description': 
     59           return tag('(%s) %s' % (pretty_size(size), description)) 
  • downloadsplugin/0.11/tracdownloads/wiki.py

    r2999 r3114  
    22 
    33from trac.core import * 
     4from trac.mimeview import Context 
    45from trac.util.html import html 
    56 
     
    2627        if ns == 'download': 
    2728            if formatter.req.perm.has_permission('DOWNLOADS_VIEW'): 
    28                 # Get cursor. 
     29                # Create context. 
     30                context = Context.from_request(formatter.req)('downloads-wiki') 
    2931                db = self.env.get_db_cnx() 
    30                 cursor = db.cursor() 
     32                context.cursor = db.cursor() 
    3133 
    3234                # Get API component. 
     
    3436 
    3537                # Get download. 
    36                 download = api.get_download(cursor, params) 
     38                if re.match(r'\d+', params):  
     39                    download = api.get_download(context, params) 
     40                else: 
     41                    download = api.get_download_by_file(context, params) 
    3742 
    3843                if download: 
    3944                    # Return link to existing file. 
    4045                    return html.a(label, href = formatter.href.downloads(params), 
    41                       title = download['file']) 
     46                      title = '%s (%s)' % (download['file'], 
     47                      pretty_size(download['size']))) 
    4248                else: 
    4349                    # Return link to non-existing file.