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 _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 _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()
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()