def delete(self, dataset): """ Delete a specific dataset (TODO)""" with api.commit_or_abort( db.session, default_error_message="Failed to delete a dataset." ): Dataset.delete(dataset=dataset)
def init_datasets(user): data_file = open( os.path.join(os.path.dirname(__file__), 'data/datasets.yml')) datasets = yaml.load(data_file)['datasets'] with db.session.begin(): for data_desc in datasets: Dataset.create(contributor=user, **data_desc)
def delete(self, story_id, dataset_id): """Remove the link between a dataset and a story """ with api.commit_or_abort( db.session, default_error_message="Failed to unlink a story and dataset." ): user = current_user # Check existence of dataset and story dataset = Dataset.get(id=dataset_id) story = Story.get(story_id=story_id) # TODO: unauthorized to unlink others' post. sd = StoryDatasetAssociation.query.filter_by( story_id=story.id, dataset_id=dataset.id, linker_id=user.id ).first() if sd is not None: db.session.delete(sd) else: raise CatalogException('Unlinked already') return dataset
def test_modifying_dataset_info_by_admin(flask_app_client, example_dataset, admin_user, regular_user, db): with flask_app_client.login(admin_user, auth_scopes=('datasets:write', )): response = flask_app_client.patch('/api/v1/datasets/%s' % example_dataset.id, content_type='application/json', data=json.dumps([{ 'op': 'replace', 'path': '/title', 'value': "Modified Dataset Title", }, { 'op': 'replace', 'path': '/license_id', 'value': 1 }])) assert response.status_code == 200 assert response.content_type == 'application/json' assert isinstance(response.json, dict) assert set(response.json.keys()) >= {'id', 'name'} assert response.json['id'] == example_dataset.id # Restore original state from catalog.modules.datasets.models import Dataset dataset_instance = Dataset.get(id=response.json['id']) assert dataset_instance.license_id == 1 assert dataset_instance.license is None
def example_dataset(db, regular_user): from catalog.modules.datasets.models import Dataset import hashlib example_dataset = Dataset( id=Dataset.dataset_id(), contributor_id=regular_user.id, name='dataset_name_' + hashlib.md5().hexdigest()[-6:-1], title='dataset_title', description='Description of dataset', homepage='http://example.com/dataset', keywords='Data', access_level='PUBLIC', data_quality=True, category='Awesome category' ) with db.session.begin(): db.session.add(example_dataset) yield example_dataset with db.session.begin(): db.session.delete(example_dataset)
def patch(self, dataset_id): """Star a dataset by the login user """ with api.commit_or_abort( db.session, default_error_message="Failed to star a dataset." ): user = current_user # Check existence of dataset dataset = Dataset.get(id=dataset_id) user_star = UserStarDataset(user_id=user.id, dataset_id=dataset.id) db.session.add(user_star) dataset.stars += 1 db.session.add(dataset) return dataset
def patch(self, story_id, dataset_id): """Create a new link between a story and a dataset """ with api.commit_or_abort( db.session, default_error_message="Failed to link a story and dataset." ): user = current_user # Check existence of dataset and story dataset = Dataset.get(id=dataset_id) story = Story.get(story_id=story_id) sd = StoryDatasetAssociation( story_id=story.id, dataset_id=dataset.id, linker_id=user.id ) db.session.add(sd) return dataset
def post(self, args, dataset_id): """ Add a new comment for a dataset""" with api.commit_or_abort( db.session, default_error_message="Failed to create a new comment for dataset" ): user = current_user # Check existence of dataset dataset = Dataset.get(id=dataset_id) # TODO: add comment flooding attack comment = Comment.create( user_id=user.id, target_id=dataset.id, target_type=CommentType.DATASET_COMMENT, comment=args.get('comment') ) return comment
def delete(self, dataset_id): """Unstar a dataset by the login user """ with api.commit_or_abort( db.session, default_error_message="Failed to unstar a dataset." ): user = current_user # Check existence of dataset dataset = Dataset.get(id=dataset_id) user_star = UserStarDataset.query.filter_by( user_id=user.id, dataset_id=dataset.id ).first() if user_star is not None: db.session.delete(user_star) dataset.stars -= 1 db.session.add(dataset) return dataset
def post(self, args): """Create a new dataset """ with api.commit_or_abort( db.session, default_error_message="Failed to create a new dataset." ): contributor = current_user args['contributor_id'] = contributor.id if 'license_name' in args: license_name = args.pop('license_name') try: license = License.get(license_name=license_name) except ObjectDoesNotExist: log.warning('License "{}" not found, and created automatically.'.format(license_name)) license = License.create(name=license_name) args['license_id'] = license.id if 'organization_name' in args: organization_name = args.pop('organization_name') try: organization = Organization.get(org_name=organization_name) except ObjectDoesNotExist: log.warning('Organization "{}" not found, and created automatically.'.format(organization_name)) organization = Organization.create(name=organization_name) args['organization_id'] = organization.id if 'publisher_name' in args: publisher_name = args.pop('publisher_name') try: publisher = Publisher.get(publisher_name=publisher_name) except ObjectDoesNotExist: log.warning('Publisher "{}" not found, and created automatically.'.format(publisher_name)) publisher = Publisher.create(name=publisher_name) args['publisher_id'] = publisher.id return Dataset.create(**args)
def post(self, args, dataset_id): """ Create a new story for a dataset""" with api.commit_or_abort( db.session, default_error_message="Failed to create a new data story." ): contributor = current_user args['contributor_id'] = contributor.id # Check existing dataset dataset = Dataset.get(id=dataset_id) # Detail object resolved by parameter handler details_obj = args.pop('details', None) new_story = Story.create(details_obj, **args) # Update dataset-story link association = StoryDatasetAssociation(linker_id=contributor.id) association.dataset = dataset new_story.datasets_association.append(association) db.session.add(new_story) return new_story
def get(self, dataset_id): """ Get all associated stories of a dataset""" associations = Dataset.get(id=dataset_id).stories_association return [i.story for i in associations]