a management command to load a user wishlist based on a file of isbns

pull/1/head
Ed Summers 2012-01-17 09:42:16 -05:00
parent c3dcd3d294
commit 245fefe1f1
1 changed files with 24 additions and 0 deletions

View File

@ -0,0 +1,24 @@
from django.core.management.base import BaseCommand
from django.contrib.auth.models import User
from regluit.core import bookloader
class Command(BaseCommand):
help = "populate a user's wishlist with books from a file of isbns"
args = "<filename> <username>"
def handle(self, filename, username, **options):
user = User.objects.get(username=username)
wishlist = user.wishlist
for isbn in open(filename):
isbn = isbn.strip()
edition = bookloader.add_by_isbn(isbn)
bookloader.add_related(isbn)
user.wishlist.add_work(edition.work, source="user")
if edition:
print "loaded %s as %s" % (isbn, edition)
else:
print "failed to load book for %s" % isbn