First attempt at placing a Librarything import button

pull/1/head
Raymond Yee 2011-11-16 16:47:29 -08:00
parent a22d23b184
commit 11027f6bc1
7 changed files with 122 additions and 24 deletions

View File

@ -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

View File

@ -6,11 +6,11 @@ from django.contrib.auth.models import User
class Command(BaseCommand):
help = "load Librarything books into wishlist"
args = "<user_name lt_username lt_password max_books>"
args = "<user_name lt_username max_books>"
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)
tasks.load_librarything_into_wishlist.delay(user, lt_username, max_books)

View File

@ -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):

View File

@ -14,6 +14,7 @@
<script type="text/javascript" src="/static/js/greenpanel.js"></script>
<script type="text/javascript" src="/static/js/toggle.js"></script>
<script type="text/javascript" src="/static/js/tabs.js"></script>
<script type="text/javascript" src="/static/js/import_books.js"></script>
{% 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 }}
</div>
</div>
</form>
<div class="block block3">
<h3 class="title">Goodreads / LibraryThing </h3>
<div class="check-list">
<label>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</label>
</div>
<div class="check-list">
<input type="checkbox" />
<label>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</label>
</div>
<h3 class="title">Goodreads / LibraryThing Import </h3>
{% if goodreads_shelf_load_form %}
<form id="load_shelf_form" method="post" action="#">
{% csrf_token %}
{{ goodreads_shelf_load_form.non_field_errors }}
<div class="fieldWrapper">
{{ goodreads_shelf_load_form.goodreads_shelf_name_number.errors }}
{{ goodreads_shelf_load_form.goodreads_shelf_name_number }}
<input type="submit" value="Load Goodreads shelf to wishlist" />
</div>
</form>
{% endif %}
{% if librarything_id %}
<form id="librarything_load" method="post" action="#">
{% csrf_token %}
<input type="submit" value="Load your LibraryThing library" />
</form>
{% endif %}
</div>
</form>
</div>
{% endifequal %}
</div>

View File

@ -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'})
)

View File

@ -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

26
static/js/import_books.js Normal file
View File

@ -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;
});
});