def populate_data(publisher_name): data = json.loads(open('fixtures/datapackage.json').read()) data_csv = open('fixtures/data/demo-resource.csv').read() readme = open('fixtures/README.md').read() package = models.Package.query.join(models.Publisher)\ .filter(models.Package.name == "demo-package", models.Publisher.name == publisher_name).first() if package: db.session.delete(models.Package.query.get(package.id)) db.session.commit() publisher = models.Publisher.query.filter_by(name=publisher_name).first() metadata = models.Package(name="demo-package") metadata.status, metadata.private \ = 'active', False tag = models.PackageTag(descriptor=data, readme=readme) metadata.tags.append(tag) publisher.packages.append(metadata) db.session.add(publisher) db.session.commit() bitstore = BitStore(publisher_name, package='demo-package', body=json.dumps(data)) bitstore.save_metadata() key = bitstore.build_s3_key('demo-resource.csv') bucket_name = app.config['S3_BUCKET_NAME'] s3_client = app.config['S3'] s3_client.put_object(Bucket=bucket_name, Key=key, Body=data_csv, ACL='public-read')
def create_or_update_tag(cls, publisher, package, tag): package = models.Package.get_by_publisher(publisher, package) tag_instance = models.PackageTag.query.join(models.Package) \ .filter(models.Package.id == package.id, models.PackageTag.tag == tag).first() if tag_instance is None: tag_instance = models.PackageTag() tag_instance.tag = tag tag_instance.readme = package.readme tag_instance.descriptor = package.descriptor tag_instance.package_id = package.id db.session.add(tag_instance) db.session.commit() return True
def create_or_update(cls, name, publisher_name, **kwargs): pub_id = models.Publisher.query.filter_by(name=publisher_name).one().id instance = models.Package.get_by_publisher(publisher_name, name) if instance is None: instance = models.Package(name=name) instance.publisher_id = pub_id tag_instance = models.PackageTag() instance.tags.append(tag_instance) else: tag_instance = models.PackageTag.query.join(models.Package) \ .filter(models.Package.id == instance.id, models.PackageTag.tag == 'latest').one() for key, value in kwargs.items(): if key not in ['descriptor', 'readme']: setattr(instance, key, value) else: setattr(tag_instance, key, value) db.session.add(instance) db.session.commit()
def setUp(self): self.publisher_name = 'demo' self.package_name = 'demo-package' self.app = create_app() self.app.app_context().push() with self.app.app_context(): db.drop_all() db.create_all() self.user = models.User() self.user.email, self.user.name, self.user.secret = \ '*****@*****.**', self.publisher_name, 'super_secret' self.publisher = models.Publisher(name=self.publisher_name) self.association = models.PublisherUser( role=models.UserRoleEnum.owner) self.metadata = models.Package(name=self.package_name) self.metadata.tags.append(models.PackageTag(descriptor={})) self.publisher.packages.append(self.metadata) self.association.publisher = self.publisher self.user.publishers.append(self.association) db.session.add(self.user) db.session.commit()
def create_or_update_tag(cls, publisher, package, tag): package = models.Package.get_by_publisher(publisher, package) data_latest = models.PackageTag.query.join(models.Package)\ .filter(models.Package.id == package.id, models.PackageTag.tag == 'latest').one() tag_instance = models.PackageTag.query.join(models.Package) \ .filter(models.Package.id == package.id, models.PackageTag.tag == tag).first() update_props = ['descriptor', 'readme', 'package_id'] if tag_instance is None: tag_instance = models.PackageTag() for update_prop in update_props: setattr(tag_instance, update_prop, getattr(data_latest, update_prop)) tag_instance.tag = tag db.session.add(tag_instance) db.session.commit() return True