def create_budget_data_package(url, user, private): try: bdpkg = BudgetDataPackage(url) except Exception as problem: # Lots of different types of problems can arise with a # BudgetDataPackage, but their message should be understandable # so we catch just any Exception and email it's message to the user log.error("Failed to parse budget data package: {0}".format( problem.message)) return [] sources = [] for (idx, resource) in enumerate(bdpkg.resources): dataset = Dataset.by_name(bdpkg.name) if dataset is None: # Get information from the descriptior file for the given # resource (at index idx) info = get_dataset_info_from_descriptor(bdpkg, idx) # Set the dataset name based on the previously computed one info['dataset']['name'] = bdpkg.name # Create the model from the resource schema model = create_model_from_schema(resource.schema) # Set the default value for the time to the fiscal year of the # resource, because it isn't included in the budget CSV so we # won't be able to load it along with the data. model['time']['default_value'] = resource.fiscalYear # Add the model as the mapping info['mapping'] = model # Create the dataset dataset = Dataset(info) dataset.managers.append(user) dataset.private = private db.session.add(dataset) db.session.commit() else: if not dataset.can_update(user): log.error( "User {0} not permitted to update dataset {1}".format( user.name, bdpkg.name)) return [] if 'url' in resource: resource_url = resource.url elif 'path' in resource: if 'base' in bdpkg: resource_url = urlparse.urljoin(bdpkg.base, resource.path) else: resource_url = urlparse.urljoin(url, resource.path) else: log.error('Url not found') return [] # We do not re-add old sources so if we find the same source # we don't do anything, else we create the source and append it # to the source list for dataset_source in dataset.sources: if dataset_source.url == resource_url: break else: source = Source(dataset=dataset, creator=user, url=resource_url) db.session.add(source) db.session.commit() sources.append(source) return sources