Exemplo n.º 1
0
def create_asset(
    *,
    item=None,
    title="Test Asset",
    slug="test-asset",
    media_type=MediaType.IMAGE,
    media_url="1.jpg",
    published=True,
    do_save=True,
    **kwargs,
):
    if item is None:
        item = create_item()
    asset = Asset(
        item=item,
        title=title,
        slug=slug,
        media_type=media_type,
        published=published,
        media_url=media_url,
        **kwargs,
    )
    asset.full_clean()
    if do_save:
        asset.save()
    return asset
Exemplo n.º 2
0
def import_item(self, import_item):
    item_assets = []
    import_assets = []
    item_resource_url = None

    asset_urls, item_resource_url = get_asset_urls_from_item_resources(
        import_item.item.metadata.get("resources", [])
    )

    for idx, asset_url in enumerate(asset_urls, start=1):
        asset_title = f"{import_item.item.item_id}-{idx}"
        item_asset = Asset(
            item=import_item.item,
            title=asset_title,
            slug=slugify(asset_title, allow_unicode=True),
            sequence=idx,
            media_url=f"{idx}.jpg",
            media_type=MediaType.IMAGE,
            download_url=asset_url,
            resource_url=item_resource_url,
        )
        item_asset.full_clean()
        item_assets.append(item_asset)

    Asset.objects.bulk_create(item_assets)

    for asset in item_assets:
        import_asset = ImportItemAsset(
            import_item=import_item,
            asset=asset,
            url=asset.download_url,
            sequence_number=asset.sequence,
        )
        import_asset.full_clean()
        import_assets.append(import_asset)

    import_item.assets.bulk_create(import_assets)

    download_asset_group = group(download_asset_task.s(i.pk) for i in import_assets)

    import_item.full_clean()
    import_item.save()

    return download_asset_group()