Made change so that Goodreads API is hit only when supporter wants to load books: https://www.pivotaltracker.com/story/show/23590565

Also allow supporter to clear association with Goodreads account
pull/1/head
Raymond Yee 2012-01-24 09:36:45 -08:00
parent 2173695bbd
commit d255fa0335
5 changed files with 75 additions and 30 deletions

View File

@ -62,9 +62,10 @@ class RightsHolderForm(forms.ModelForm):
class ProfileForm(forms.ModelForm):
clear_facebook=forms.BooleanField(required=False)
clear_twitter=forms.BooleanField(required=False)
clear_goodreads=forms.BooleanField(required=False)
class Meta:
model = UserProfile
fields = 'tagline', 'librarything_id', 'home_url', 'clear_facebook', 'clear_twitter'
fields = 'tagline', 'librarything_id', 'home_url', 'clear_facebook', 'clear_twitter', 'clear_goodreads'
widgets = {
'tagline': forms.Textarea(attrs={'cols': 25, 'rows': 5}),
}

View File

@ -155,7 +155,7 @@ how do I integrate the your wishlist thing with the tabs thing?
</div>
<div class="check-list">
{% if user.profile.goodreads_user_id %}
<a href="{{goodreads_auth_url}}">Update your GoodReads connection</a>
<a href="{{goodreads_auth_url}}">Update your GoodReads connection</a> <br/> or disconnect GoodReads {{ profile_form.clear_goodreads }}
{% else %}
<a href="{{goodreads_auth_url}}">Connect your GoodReads account</a> to Unglue.it
{% endif %}
@ -168,15 +168,16 @@ how do I integrate the your wishlist thing with the tabs thing?
</form>
<div class="block block3">
<h3 class="title">Import your books</h3>
{% if goodreads_shelf_load_form %}
{% if goodreads_id %}
<form id="load_shelf_form" method="post" action="#">
{% csrf_token %}
{{ goodreads_shelf_load_form.non_field_errors }}
<div class="fieldWrapper">
<div id="loadgr"><span><div>{{ goodreads_shelf_load_form.goodreads_shelf_name_number.errors }}
{{ goodreads_shelf_load_form.goodreads_shelf_name_number }}</div>
<input type="submit" value="Add this shelf" />
</div></span>
<div id="loadgr"><span>
<div id="goodreads_shelves"></div>
<input id="goodreads_input" type="submit" value="Display your GoodReads shelves" />
</span>
</div>
</div>
</form>
{% else %}

View File

@ -35,6 +35,7 @@ urlpatterns = patterns(
url(r"^goodreads/auth_cb/$", "goodreads_cb", name="goodreads_cb"),
url(r"^goodreads/flush/$","goodreads_flush_assoc", name="goodreads_flush_assoc"),
url(r"^goodreads/load_shelf/$","goodreads_load_shelf", name="goodreads_load_shelf"),
url(r"^goodreads/shelves/$","goodreads_calc_shelves", name="goodreads_calc_shelves"),
url(r"^stub/", "stub", name="stub"),
url(r"^work/(?P<work_id>\d+)/$", "work", name="work"),
url(r"^work/(?P<work_id>\d+)/librarything/$", "work_librarything", name="work_librarything"),

View File

@ -735,31 +735,28 @@ def supporter(request, supporter_username, template_name):
if request.method == 'POST':
profile_form = ProfileForm(data=request.POST,instance=profile_obj)
if profile_form.is_valid():
if profile_form.cleaned_data['clear_facebook'] or profile_form.cleaned_data['clear_twitter'] :
if profile_form.cleaned_data['clear_facebook'] or profile_form.cleaned_data['clear_twitter'] or profile_form.cleaned_data['clear_goodreads'] :
if profile_form.cleaned_data['clear_facebook']:
profile_obj.facebook_id=0
if profile_form.cleaned_data['clear_twitter']:
profile_obj.twitter_id=""
if profile_form.cleaned_data['clear_goodreads']:
profile_obj.goodreads_user_id = None
profile_obj.goodreads_user_name = None
profile_obj.goodreads_user_link = None
profile_obj.goodreads_auth_token = None
profile_obj.goodreads_auth_secret = None
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))
goodreads_id = request.user.profile.goodreads_user_id
else:
goodreads_shelf_load_form = None
goodreads_id = None
if request.user.profile.librarything_id is not None:
librarything_id = request.user.profile.librarything_id
@ -767,7 +764,7 @@ def supporter(request, supporter_username, template_name):
librarything_id = None
else:
profile_form = ''
goodreads_shelf_load_form = None
goodreads_id = None
librarything_id = None
@ -783,7 +780,7 @@ def supporter(request, supporter_username, template_name):
"profile_form": profile_form,
"ungluers": userlists.other_users(supporter, 5 ),
"goodreads_auth_url": reverse('goodreads_auth'),
"goodreads_shelf_load_form": goodreads_shelf_load_form,
"goodreads_id": goodreads_id,
"librarything_id": librarything_id
}
@ -1055,6 +1052,26 @@ def goodreads_load_shelf(request):
logger.info("Error in loading shelf for user %s: %s ", user, e)
@login_required
def goodreads_calc_shelves(request):
# 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
return HttpResponse(json.dumps(goodreads_shelves), content_type="application/json")
@require_POST
@login_required
@csrf_exempt

View File

@ -11,16 +11,41 @@ jQuery(document).ready(function($) {
return bubble;
}
};
$('#load_shelf_form').submit(function(){
post_and_alert('/goodreads/load_shelf/')(false,'#load_shelf_form',$('#load_shelf_form').serialize());
return false;
});
$('#librarything_load').submit(function(){
post_and_alert('/librarything/load/')(false,'#librarything_load',$('#librarything_load').serialize());
return false;
});
});
$('#load_shelf_form').submit(function(){
// do ajax call to pick up the list of shelves
if ($('#id_goodreads_shelf_name_number').length == 0) {
var params = {};
$.getJSON('/goodreads/shelves', params, function(json) {
// say waiting
$('#goodreads_input').attr('value', 'Loading....');
var sel = $('<select id="id_goodreads_shelf_name_number" name="goodreads_shelf_name_number"></select>').appendTo('#goodreads_shelves');
$('<option value="' + 'all:' + json.total_book_count + '">' + 'all (' + json.total_book_count +')' + '</option>').appendTo(sel);
for (var i = 0; i < json.user_shelves.length; i++) {
$('<option value="' + json.user_shelves[i].name + ':' + json.user_shelves[i].book_count + '">' + json.user_shelves[i].name +
' (' + json.user_shelves[i].book_count + ')' + '</option>').appendTo(sel);
}
$('#load_gr_shelves_list').attr('id','load_shelf_form');
$('#goodreads_input').attr('value', 'Add this shelf');
});
} else {
post_and_alert('/goodreads/load_shelf/')(false,'#load_shelf_form',$('#load_shelf_form').serialize());
}
return false;
// change the button value
// change form id to 'load_shelf_form'
});
});