Skip to content

Commit

Permalink
🔨 Add dump command for products
Browse files Browse the repository at this point in the history
  • Loading branch information
dariomory committed Dec 22, 2022
1 parent 2a642a6 commit 6dc1ced
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,5 @@ local.py
# Static files
src/sdg/static/bundles/
src/sdg/fonts/

products.json
41 changes: 41 additions & 0 deletions src/sdg/producten/management/commands/dump_products.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import json

from django.core.management import BaseCommand
from django.http import HttpRequest

from sdg.api.views import ProductViewSet

OUTPUT_FILE = "products.json"


class Command(BaseCommand):
help = "Dump all products to a JSON file."

def handle(self, **options):
class auth:
api_default_most_recent = True

request = HttpRequest()
request.auth = auth
request.META["SERVER_NAME"] = "localhost"
request.META["SERVER_PORT"] = 0

_ViewSet = ProductViewSet()
_Serializer = _ViewSet.get_serializer_class()
_ViewSet.request = request

serializer = _Serializer(
_ViewSet.get_queryset(), many=True, context={"request": request}
)

dump = json.dumps(serializer.data, indent=4)

with open(OUTPUT_FILE, "w") as f:
f.write(dump)

self.stdout.write(dump)
self.stdout.write(
self.style.SUCCESS(
f"Successfully dumped {len(serializer.data)} products (filename: {OUTPUT_FILE})"
)
)

0 comments on commit 6dc1ced

Please sign in to comment.