-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
system_meta/management/commands/update_product_image_data.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from django.core.management.base import BaseCommand | ||
|
||
from system_meta.models import Product | ||
from system_meta.tasks import update_products | ||
|
||
|
||
class Command(BaseCommand): | ||
""" | ||
A management command to update image_metadata for all Product objects | ||
Example usage: python manage.py update_product_image_data --product_id 1 | ||
""" | ||
|
||
help = "Update image_metadata for all Product objects" | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
"--product-id", | ||
type=int, | ||
help="The ID of the product to update", | ||
) | ||
parser.add_argument( | ||
"--sku", | ||
type=str, | ||
help="The SKU of the product to update", | ||
) | ||
parser.add_argument( | ||
"--name", | ||
type=str, | ||
help="The name of the product to update", | ||
) | ||
parser.add_argument( | ||
"--system-name", | ||
type=str, | ||
help="The system name of the product to update", | ||
) | ||
|
||
def handle(self, *args, **kwargs): # noqa: ARG002 | ||
product_id = kwargs.get("product_id") | ||
sku = kwargs.get("sku") | ||
name = kwargs.get("name") | ||
system_name = kwargs.get("system_name") | ||
|
||
if product_id: | ||
products = Product.objects.filter(id=product_id) | ||
elif sku: | ||
products = Product.objects.filter(sku=sku, system__name=system_name) | ||
elif name: | ||
products = Product.objects.filter(name=name) | ||
else: | ||
products = Product.objects.all() | ||
|
||
for product in products: | ||
update_products.delay(product.id) | ||
self.stdout.write( | ||
self.style.SUCCESS( | ||
f"Successfully updated image metadata for product {product.id}" | ||
) | ||
) |