diff --git a/core/librarything.py b/core/librarything.py index 73e7ffc3..7955c81d 100644 --- a/core/librarything.py +++ b/core/librarything.py @@ -192,7 +192,7 @@ class LibraryThing(object): if offset >= total: next_page = False -def load_librarything_into_wishlist(user, lt_username, lt_password, max_books=None): +def load_librarything_into_wishlist(user, lt_username, max_books=None): """ Load a specified Goodreads shelf (by default: all the books from the Goodreads account associated with user) """ @@ -200,11 +200,13 @@ def load_librarything_into_wishlist(user, lt_username, lt_password, max_books=No from regluit.core import bookloader from itertools import islice - lt = LibraryThing(lt_username,lt_password) - lt.retrieve_csv() - for (i,book) in enumerate(islice(lt.parse_csv(),max_books)): - isbn = book["isbn"][0] # grab the first one - logger.info("%d %s %s", i, book["title"], isbn) + logger.info("Entering into load_librarything_into_wishlist") + lt = LibraryThing(lt_username) + + + for (i,book) in enumerate(islice(lt.parse_user_catalog(view_style=5),max_books)): + isbn = book["isbn"] # grab the first one + logger.info("%d %s %s", i, book["title"]["title"], isbn) try: edition = bookloader.add_by_isbn(isbn) # let's not trigger too much traffic to Google books for now diff --git a/core/management/commands/librarything_load_books.py b/core/management/commands/librarything_load_books.py index 3a7eb22a..6702879f 100644 --- a/core/management/commands/librarything_load_books.py +++ b/core/management/commands/librarything_load_books.py @@ -6,11 +6,11 @@ from django.contrib.auth.models import User class Command(BaseCommand): help = "load Librarything books into wishlist" - args = "" + args = "" - def handle(self, user_name, lt_username, lt_password, max_books, **options): + def handle(self, user_name, lt_username, max_books, **options): user = User.objects.get(username=user_name) max_books = int(max_books) - tasks.load_librarything_into_wishlist.delay(user, lt_username, lt_password, max_books) \ No newline at end of file + tasks.load_librarything_into_wishlist.delay(user, lt_username, max_books) \ No newline at end of file diff --git a/core/tasks.py b/core/tasks.py index d4fd0dc2..7df15082 100644 --- a/core/tasks.py +++ b/core/tasks.py @@ -17,8 +17,8 @@ def load_goodreads_shelf_into_wishlist(user, shelf_name='all', goodreads_user_id return goodreads.load_goodreads_shelf_into_wishlist(user,shelf_name,goodreads_user_id,max_books, expected_number_of_books) @task -def load_librarything_into_wishlist(user, lt_username, lt_password, max_books=None): - return librarything.load_librarything_into_wishlist(user, lt_username, lt_password, max_books) +def load_librarything_into_wishlist(user, lt_username, max_books=None): + return librarything.load_librarything_into_wishlist(user, lt_username, max_books) @task def add(x,y): diff --git a/frontend/templates/supporter.html b/frontend/templates/supporter.html index 1af55acf..1a6c615b 100644 --- a/frontend/templates/supporter.html +++ b/frontend/templates/supporter.html @@ -14,6 +14,7 @@ + {% endblock %} @@ -149,18 +150,27 @@ how do I integrate the your wishlist thing with the tabs thing? {{ profile_form.librarything_id }}{{ profile_form.librarything_id.errors }} +
-

Goodreads / LibraryThing

-
- -
-
- - -
+

Goodreads / LibraryThing Import

+ {% if goodreads_shelf_load_form %} +
+ {% csrf_token %} + {{ goodreads_shelf_load_form.non_field_errors }} +
+ {{ goodreads_shelf_load_form.goodreads_shelf_name_number.errors }} + {{ goodreads_shelf_load_form.goodreads_shelf_name_number }} + +
+
+ {% endif %} + {% if librarything_id %} +
+ {% csrf_token %} + +
+ {% endif %}
- - {% endifequal %} diff --git a/frontend/urls.py b/frontend/urls.py index 8ade8481..95e40dca 100644 --- a/frontend/urls.py +++ b/frontend/urls.py @@ -39,5 +39,6 @@ urlpatterns = patterns( url(r"^celery/clear/$","clear_celery_tasks", name="clear_celery_tasks"), url(r"^subjects/$", "subjects", name="subjects"), url(r"^librarything/$", LibraryThingView.as_view(), name="librarything"), + url(r"^librarything/load/$","librarything_load", name="librarything_load"), url('^404testing/$', direct_to_template, {'template': '404.html'}) ) diff --git a/frontend/views.py b/frontend/views.py index 680cd474..6c219d59 100755 --- a/frontend/views.py +++ b/frontend/views.py @@ -175,11 +175,13 @@ def supporter(request, supporter_username, template_name): # following block to support profile admin form in supporter page if request.user.is_authenticated() and request.user.username == supporter_username: + try: profile_obj=request.user.get_profile() except ObjectDoesNotExist: profile_obj= models.UserProfile() profile_obj.user=request.user + if request.method == 'POST': profile_form = ProfileForm(data=request.POST,instance=profile_obj) if profile_form.is_valid(): @@ -190,12 +192,34 @@ def supporter(request, supporter_username, template_name): profile_obj.twitter_id="" profile_obj.save() profile_form.save() + else: profile_form= ProfileForm(instance=profile_obj) + + # for now, also calculate the Goodreads shelves of user when loading this page + # we should move towards calculating this only if needed (perhaps with Ajax), caching previous results, etc to speed up + # performance + + if request.user.profile.goodreads_user_id is not None: + gr_client = GoodreadsClient(key=settings.GOODREADS_API_KEY, secret=settings.GOODREADS_API_SECRET) + goodreads_shelves = gr_client.shelves_list(user_id=request.user.profile.goodreads_user_id) + goodreads_shelf_load_form = GoodreadsShelfLoadingForm() + # load the shelves into the form + choices = [('all:%d' % (goodreads_shelves["total_book_count"]),'all (%d)' % (goodreads_shelves["total_book_count"]))] + \ + [("%s:%d" % (s["name"], s["book_count"]) ,"%s (%d)" % (s["name"],s["book_count"])) for s in goodreads_shelves["user_shelves"]] + goodreads_shelf_load_form.fields['goodreads_shelf_name_number'].widget = Select(choices=tuple(choices)) + else: + goodreads_shelf_load_form = None + + if request.user.profile.librarything_id is not None: + librarything_id = request.user.profile.librarything_id + else: + librarything_id = None else: profile_form = '' - - goodreads_auth_url = reverse('goodreads_auth') + goodreads_shelf_load_form = None + librarything_id = None + context = { "supporter": supporter, @@ -207,7 +231,9 @@ def supporter(request, supporter_username, template_name): "shared_works": shared_works, "profile_form": profile_form, "ungluers": userlists.other_users(supporter, 5 ), - "goodreads_auth_url": goodreads_auth_url + "goodreads_auth_url": reverse('goodreads_auth'), + "goodreads_shelf_load_form": goodreads_shelf_load_form, + "librarything_id": librarything_id } return render(request, template_name, context) @@ -465,6 +491,39 @@ def goodreads_load_shelf(request): return HttpResponse("Error in loading shelf: %s " % (e)) logger.info("Error in loading shelf for user %s: %s ", user, e) + +@require_POST +@login_required +@csrf_exempt +def librarything_load(request): + """ + a view to allow user load librarything library into her wishlist + """ + # Should be moved to the API + user = request.user + + + try: + # figure out expected_number_of_books later + + lt_username = request.user.profile.librarything_id + logger.info('Adding task to load librarything %s to user %s', lt_username, user) + load_task_name = "load_librarything_into_wishlist" + load_task = getattr(tasks, load_task_name) + task_id = load_task.delay(user, lt_username, None) + + ct = models.CeleryTask() + ct.task_id = task_id + ct.function_name = load_task_name + ct.user = user + ct.description = "Loading LibraryThing collection of %s to user %s." % (lt_username, user) + ct.save() + + return HttpResponse("LibraryThing loading placed on task queue.") + except Exception,e: + return HttpResponse("Error in loading LibraryThing library: %s " % (e)) + logger.info("Error in loading LibraryThing for user %s: %s ", user, e) + @require_POST @login_required @csrf_exempt diff --git a/static/js/import_books.js b/static/js/import_books.js new file mode 100644 index 00000000..95d9df8f --- /dev/null +++ b/static/js/import_books.js @@ -0,0 +1,26 @@ +jQuery(document).ready(function($) { + + // post to form_loc and alert with response + var post_and_alert = function (form_loc){ + + return function(bubble,params) { + $.post(form_loc, params, function (data) { + alert(data); + }); + + return bubble; + } + }; + + + $('#load_shelf_form').submit(function(){ + post_and_alert('/goodreads/load_shelf/')(false,$('#load_shelf_form').serialize()); + return false; + }); + + $('#librarything_load').submit(function(){ + post_and_alert('/librarything/load/')(false,$('#librarything_load').serialize()); + return false; + }); + +}); \ No newline at end of file