-
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.
* add image metadata * migration * Update serializers * update model * management command * setting * Add celery task * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * ruff * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * ruff * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * open api * Mock product save api call * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add better description * code review comments * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * better solution * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix tests * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
e1b6e5b
commit 469f918
Showing
11 changed files
with
188 additions
and
3 deletions.
There are no files selected for viewing
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
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
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
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
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}" | ||
) | ||
) |
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,21 @@ | ||
# Generated by Django 4.2.17 on 2024-12-10 16:10 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("system_meta", "0006_integratedsystemapikey"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="product", | ||
name="image_metadata", | ||
field=models.JSONField( | ||
blank=True, | ||
help_text="Image metadata including URL, alt text, and description (in JSON).", | ||
null=True, | ||
), | ||
), | ||
] |
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
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 |
---|---|---|
|
@@ -41,4 +41,5 @@ class Meta: | |
"system", | ||
"price", | ||
"deleted_by_cascade", | ||
"image_metadata", | ||
] |
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,40 @@ | ||
import logging | ||
from typing import Optional | ||
|
||
import requests | ||
from celery import shared_task | ||
from django.conf import settings | ||
|
||
|
||
@shared_task | ||
def update_products(product_id: Optional[int] = None): | ||
""" | ||
Update all product's image metadata. If product_id is provided, only update the | ||
product with that ID. Otherwise, update all products. | ||
""" | ||
from .models import Product | ||
|
||
log = logging.getLogger(__name__) | ||
if product_id: | ||
products = Product.objects.filter(id=product_id) | ||
else: | ||
products = Product.objects.all() | ||
for product in products: | ||
try: | ||
response = requests.get( | ||
f"{settings.MITOL_LEARN_API_URL}learning_resources/", | ||
params={"platform": product.system.slug, "readable_id": product.sku}, | ||
timeout=10, | ||
) | ||
response.raise_for_status() | ||
results_data = response.json() | ||
course_data = results_data.get("results")[0] | ||
image_data = course_data.get("image") | ||
product.image_metadata = { | ||
"image_url": image_data.get("url"), | ||
"alt_text": image_data.get("alt"), | ||
"description": image_data.get("description"), | ||
} | ||
product.save() | ||
except requests.RequestException: | ||
log.exception("Failed to retrieve image data for product %s", product.id) |
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
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