def _do_remove(self, identifier): # Get downloads API component. api = self.env[DownloadsApi] # Create context. context = Context('downloads-consoleadmin') db = self.env.get_db_cnx() context.cursor = db.cursor() context.req = FakeRequest(self.env, self.consoleadmin_user) # Get download by ID or filename. try: download_id = int(identifier) download = api.get_download(context, download_id) except ValueError: download = api.get_download_by_file(context, identifier) # Check if download exists. if not download: raise AdminCommandError( _('Invalid download identifier: %(value)s', value=identifier)) # Delete download by ID. api.delete_download(context, download_id) # Commit changes in DB. db.commit()
def _resolve_ids(self, download): # Create context. context = Context('downloads-core') db = self.env.get_db_cnx() context.cursor = db.cursor() # Resolve platform and type names. api = self.env[DownloadsApi] platform = api.get_platform(context, download['platform']) type = api.get_type(context, download['type']) download['platform'] = platform['name'] download['type'] = type['name']
def get_resource_description(self, resource, format='default', context=None, **kwargs): # Create context. context = Context('downloads-core') db = self.env.get_db_cnx() context.cursor = db.cursor() # Get download from ID. api = self.env[DownloadsApi] download = api.get_download(context, safe_int(resource.id)) if format == 'compact': return download['file'] elif format == 'summary': return '(%s) %s' % (pretty_size( download['size']), download['description']) return download['file']
def _do_list(self): # Get downloads API component. api = self.env[DownloadsApi] # Create context. context = Context('downloads-consoleadmin') db = self.env.get_db_cnx() context.cursor = db.cursor() # Print uploded download downloads = api.get_downloads(context) print_table( [(download['id'], download['file'], pretty_size(download['size']), format_datetime(download['time']), download['component'], download['version'], download['architecture']['name'], download['platform']['name'], download['type']['name']) for download in downloads], [ 'ID', 'Filename', 'Size', 'Uploaded', 'Component', 'Version', 'Architecture', 'Platform', 'Type' ])
def _do_add(self, filename, *arguments): # Get downloads API component. api = self.env[DownloadsApi] # Create context. context = Context('downloads-consoleadmin') db = self.env.get_db_cnx() context.cursor = db.cursor() context.req = FakeRequest(self.env, self.consoleadmin_user) # Convert relative path to absolute. if not os.path.isabs(filename): filename = os.path.join(self.path, filename) # Open file object. file, filename, file_size = self._get_file(filename) # Create download dictionary from arbitrary attributes. download = { 'file': filename, 'size': file_size, 'time': to_timestamp(datetime.now(utc)), 'count': 0 } # Read optional attributes from arguments. for argument in arguments: # Check correct format. argument = argument.split("=") if len(argument) != 2: AdminCommandError( _('Invalid format of download attribute:' ' %(value)s', value=argument)) name, value = argument # Check known arguments. if not name in ('description', 'author', 'tags', 'component', 'version', 'architecture', 'platform', 'type'): raise AdminCommandError( _('Invalid download attribute:' ' %(value)s', value=name)) # Transform architecture, platform and type name to ID. if name == 'architecture': value = api.get_architecture_by_name(context, value)['id'] elif name == 'platform': value = api.get_platform_by_name(context, value)['id'] elif name == 'type': value = api.get_type_by_name(context, value)['id'] # Add attribute to download. download[name] = value self.log.debug(download) # Upload file to DB and file storage. api._add_download(context, download, file) # Close input file and commit changes in DB. file.close() db.commit()