Example #1
0
def export_search():
	""" Ajax route for exporting search results."""
	if request.method == 'POST':
		query = request.json['query']
		page = 1
		limit = int(request.json['advancedOptions']['limit'])
		sorting = []
		if request.json['advancedOptions']['show_properties'] != []:
			show_properties = request.json['advancedOptions']['show_properties']
		else:
			show_properties = ''
		paginated = False
		sorting = []
		for item in request.json['advancedOptions']['sort']:
			if item[1] == 'ASC':
				opt = (item[0], pymongo.ASCENDING)
			else:
				opt = (item[0], pymongo.DESCENDING)
			sorting.append(opt)
		result, num_pages, errors = methods.search_corpus(query, limit, paginated, page, show_properties, sorting)
		if len(result) == 0:
			errors.append('No records were found matching your search criteria.')
		# Need to write the results to temp folder
		for item in result:
			filename = item['name'] + '.json'
			filepath = os.path.join('app/temp', filename)
			with open(filepath, 'w') as f:
				f.write(json.dumps(item, indent=2, sort_keys=False, default=JSON_UTIL))
		# Need to zip up multiple files
		if len(result) > 1:
			filename = 'search_results.zip'
			methods.zipfolder('app/temp', 'search_results')
		return json.dumps({'filename': filename, 'errors': errors}, default=JSON_UTIL)
Example #2
0
def export_search():
    """Ajax route for exporting search results."""
    if request.method == 'POST':
        query = request.json['query']
        page = 1
        limit = int(request.json['advancedOptions']['limit'])
        sorting = []
        if request.json['advancedOptions']['show_properties'] != []:
            show_properties = request.json['advancedOptions']['show_properties']
        else:
            show_properties = ''
        paginated = False
        sorting = []
        for item in request.json['advancedOptions']['sort']:
            if item[1] == 'ASC':
                opt = (item[0], pymongo.ASCENDING)
            else:
                opt = (item[0], pymongo.DESCENDING)
            sorting.append(opt)
        result, _, errors = methods.search_corpus(query, limit, paginated, page, show_properties, sorting)
        if not result:
            errors.append('No records were found matching your search criteria.')
        # Need to write the results to temp folder
        for item in result:
            filename = item['name'] + '.json'
            filepath = os.path.join('app/temp', filename)
            with open(filepath, 'w') as f:
                f.write(json.dumps(item, indent=2, sort_keys=False, default=JSON_UTIL))
        # Need to zip up multiple files
        if len(result) > 1:
            filename = 'search_results.zip'
            methods.zipfolder('app/temp', 'search_results')
        return json.dumps({'filename': filename, 'errors': errors}, default=JSON_UTIL)
Example #3
0
def send_export():
	""" Ajax route to process user export options and write 
	the export files to the temp folder.
	"""
	data = request.json
	# The user only wants to print the manifest
	if data['exportoptions'] == ['manifestonly']:
		query = {'name': data['name'], 'path': data['path']}
		try:
			result = corpus_db.find_one(query)
			assert result != None
			manifest = {}
			for key, value in result.items():
				if value != '' and value != []:
					manifest[key] = value
			manifest = json.dumps(manifest, indent=2, sort_keys=False, default=JSON_UTIL)
			filename = data['name'] + '.json'
			doc = filename
			methods.make_dir('app/temp')
			filepath = os.path.join('app/temp', filename)
			with open(filepath, 'w') as f:
				f.write(manifest)
		except:
			print('Could not find the manifest in the database.')
	# The user wants a zip of multiple data documents
	else:
		# Get the exportoptions with the correct case
		methods.make_dir('app/temp/Corpus')
		name = data['name']
		path = data['path']
		# Ensures that there is a Corpus and collection folder with a collection manifest
		methods.make_dir('app/temp/Corpus')
		if path == ',Corpus,':
			collection = name
		else:
			collection = path.split(',')[2]
		methods.make_dir('app/temp/Corpus/' + collection)
		# result = corpus_db.find_one({'path': path, 'name': collection})
		result = corpus_db.find_one({'path': path})
		# assert result != None
		manifest = {}
		for key, value in result.items():
			if value != '' and value != []:
				manifest[key] = value
		manifest = json.dumps(manifest, indent=2, sort_keys=False, default=JSON_UTIL)
		filename = name + '.json'
		filepath = os.path.join('app/temp/Corpus', filename)
		with open(filepath, 'w') as f:
			f.write(manifest)
		exportoptions = []
		exportopts = [x.replace('export', '') for x in data['exportoptions']]
		exclude = []
		options = ['Corpus', 'RawData', 'ProcessedData', 'Metadata', 'Outputs', 'Results']
		if not path.startswith(',Corpus,'):
			path = ',Corpus,' + path
		for option in options:
			if option.lower() in exportopts:
				exportoptions.append(option)
			else:
				exclude.append(',Corpus,' + ',' + name + ',' + option + ',.*')
		# We have a path and a list of paths to exclude
		excluded = '|'.join(exclude)
		excluded = re.compile(excluded)
		regex_path = re.compile(path + name + ',.*')
		result = corpus_db.find(
			{'path': {
				'$regex': regex_path,
				'$not': excluded
			}}
		)
		for item in list(result):
			# Handle schema node manifests
			path = item['path'].replace(',', '/')
			if item['name'] in exportoptions:
				folder_path = os.path.join(path, item['name'])
				methods.make_dir('app/temp' + folder_path)
				folder = 'app/temp' + path
				doc = item['name'] + '.json'
			# Handle data and branches
			else:
				# If the document has content, just give it a filename
				try:
					assert item['content']
					doc = item['name'] + '.json'
					folder = 'app/temp' + path
					methods.make_dir(folder)
				# Otherwise, use it to create a folder with a manifest file
				except:
					path = os.path.join(path, item['name'])
					folder = 'app/temp' + path
					methods.make_dir(folder)
					doc = item['name'] + '.json'
			filepath = os.path.join(folder, doc)
			output = json.dumps(item, indent=2, sort_keys=False, default=JSON_UTIL)
			with open(filepath, 'w') as f:
				f.write(output)
		# Zip up the file structure
		try:
			source_dir = 'app/temp/Corpus'
			doc = 'Corpus.zip'
			methods.zipfolder(source_dir, source_dir)
		except:
			print('Could not make zip archive.')
	return json.dumps({'filename': doc})
Example #4
0
def send_export():
    """Ajax route to process user export options and write the export files to the temp folder."""
    data = request.json
    print('FLUFFFFFFYYY')
    print(data)
    # The user only wants to print the manifest
    if data['exportoptions'] == ['manifestonly']:
        query = {'name': data['name'], 'metapath': data['metapath']}
        try:
            result = corpus_db.find_one(query)
            assert result is not None
            manifest = {}
            for key, value in result.items():
                if value != '' and value != []:
                    manifest[key] = value
            manifest = json.dumps(manifest, indent=2, sort_keys=False, default=JSON_UTIL)
            filename = data['name'] + '.json'
            doc = filename
            methods.make_dir('app/temp')
            filepath = os.path.join('app/temp', filename)
            with open(filepath, 'w') as f:
                f.write(manifest)
        except:
            print('Could not find the manifest in the database.')
    # The user wants a zip of multiple data documents
    else:
        # Get the exportoptions with the correct case
        methods.make_dir('app/temp/Corpus')
        name = data['name']
        metapath = data['metapath']
        # Ensures that there is a Corpus and collection folder with a collection manifest
        methods.make_dir('app/temp/Corpus')
        if metapath == 'Corpus':
            collection = name
        else:
            collection = metapath.split(',')[2]
        methods.make_dir('app/temp/Corpus/' + collection)
        # result = corpus_db.find_one({'metapath': metapath, 'name': collection})
        result = corpus_db.find_one({'metapath': metapath})
        # assert result is not None
        manifest = {}
        for key, value in result.items():
            if value != '' and value != []:
                manifest[key] = value
        manifest = json.dumps(manifest, indent=2, sort_keys=False, default=JSON_UTIL)
        filename = name + '.json'
        filepath = os.path.join('app/temp/Corpus', filename)
        with open(filepath, 'w') as f:
            f.write(manifest)
        exportoptions = []
        exportopts = [x.replace('export', '') for x in data['exportoptions']]
        exclude = []
        options = ['Corpus', 'RawData', 'ProcessedData', 'Metadata', 'Outputs', 'Results']
        if not metapath.startswith('Corpus,'):
            metapath = 'Corpus,' + metapath
        for option in options:
            if option.lower() in exportopts:
                exportoptions.append(option)
            else:
                exclude.append('Corpus' + ',' + name + ',' + option + ',.*')
        # We have a path and a list of metapaths to exclude
        excluded = '|'.join(exclude)
        excluded = re.compile(excluded)
        regex_path = re.compile(metapath + name + ',.*')
        result = corpus_db.find(
            {'metapath': {
                '$regex': regex_path,
                '$not': excluded
            }}
        )
        for item in list(result):
            # Handle schema node manifests
            path = item['metapath'].replace(',', '/')
            if item['name'] in exportoptions:
                folder_path = os.path.join(path, item['name'])
                methods.make_dir('app/temp' + folder_path)
                folder = 'app/temp' + path
                doc = item['name'] + '.json'
            # Handle data and branches
            else:
                # If the document has content, just give it a filename
                try:
                    assert item['content']
                    doc = item['name'] + '.json'
                    folder = 'app/temp' + path
                    methods.make_dir(folder)
                # Otherwise, use it to create a folder with a manifest file
                except:
                    path = os.path.join(path, item['name'])
                    folder = 'app/temp' + path
                    methods.make_dir(folder)
                    doc = item['name'] + '.json'
            filepath = os.path.join(folder, doc)
            output = json.dumps(item, indent=2, sort_keys=False, default=JSON_UTIL)
            with open(filepath, 'w') as f:
                f.write(output)
        # Zip up the file structure
        try:
            source_dir = 'app/temp/Corpus'
            doc = 'Corpus.zip'
            methods.zipfolder(source_dir, source_dir)
        except:
            print('Could not make zip archive.')
    return json.dumps({'filename': doc})