forked from doubaniux/boofilsic
-
-
Notifications
You must be signed in to change notification settings - Fork 34
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
Her Email
authored and
Their Name
committed
Dec 3, 2023
1 parent
84f42a4
commit 2484bd7
Showing
1 changed file
with
66 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import json | ||
|
||
from django.core.management.base import BaseCommand | ||
|
||
from catalog.models import Item | ||
from common.utils import get_uuid_or_404 | ||
from journal.models import * | ||
from takahe.utils import * | ||
from users.models import APIdentity | ||
|
||
|
||
# TODO make this available in UI | ||
class Command(BaseCommand): | ||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
"--verbose", | ||
action="store_true", | ||
) | ||
parser.add_argument("--export", default="", action="store") | ||
parser.add_argument("--import", default="", action="store") | ||
|
||
def handle(self, *args, **options): | ||
self.verbose = options["verbose"] | ||
|
||
if options["import"]: | ||
self.process_import(options["import"]) | ||
|
||
if options["export"]: | ||
self.process_export(options["export"]) | ||
|
||
self.stderr.write(self.style.SUCCESS(f"Done.")) | ||
|
||
def process_export(self, collection_uuid): | ||
try: | ||
collection = Collection.objects.get(uid=get_uuid_or_404(collection_uuid)) | ||
except Collection.DoesNotExist: | ||
self.stderr.write(self.style.ERROR(f"Collection not found.")) | ||
return | ||
self.stderr.write(self.style.SUCCESS(f"Exporting {collection}")) | ||
data = { | ||
"title": collection.title, | ||
"brief": collection.brief, | ||
"items": [], | ||
} | ||
for member in collection.ordered_members.all(): | ||
data["items"].append( | ||
{ | ||
"title": member.item.title, | ||
"url": member.item.absolute_url, | ||
"note": member.note, | ||
} | ||
) | ||
print(json.dumps(data, indent=2)) | ||
|
||
def process_import(self, username): | ||
owner = APIdentity.objects.get(username=username, local=True) | ||
data = json.load(sys.stdin) | ||
collection = Collection.objects.create( | ||
owner=owner, | ||
title=data["title"], | ||
brief=data["brief"], | ||
) | ||
for item in data["items"]: | ||
i = Item.get_by_url(item["url"]) | ||
collection.append_item(i, note=item["note"]) | ||
self.stderr.write(self.style.SUCCESS(f"Added {i}")) |