2011-10-20 03:31:16 +00:00
|
|
|
import logging
|
|
|
|
from decimal import Decimal as D
|
|
|
|
|
2011-11-15 02:13:39 +00:00
|
|
|
from django.db.models import Q, Count
|
2011-10-20 03:31:16 +00:00
|
|
|
from django.conf import settings
|
2011-09-12 05:53:54 +00:00
|
|
|
from django.contrib.auth.models import User
|
2011-09-27 09:27:15 +00:00
|
|
|
from django.core.urlresolvers import reverse
|
2011-10-23 18:40:06 +00:00
|
|
|
from django.core.exceptions import ObjectDoesNotExist
|
2011-11-01 00:26:05 +00:00
|
|
|
from django.forms import Select
|
2011-09-27 09:27:15 +00:00
|
|
|
from django.http import HttpResponseRedirect
|
2011-10-12 17:47:48 +00:00
|
|
|
from django.http import HttpResponse
|
2011-09-29 06:23:50 +00:00
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
|
|
from django.views.decorators.http import require_POST
|
2011-10-12 17:47:48 +00:00
|
|
|
from django.views.generic.edit import FormView
|
2011-10-25 01:29:01 +00:00
|
|
|
from django.views.generic.base import TemplateView
|
2011-09-29 06:23:50 +00:00
|
|
|
from django.contrib.auth.decorators import login_required
|
2011-09-29 01:36:47 +00:00
|
|
|
from django.shortcuts import render, render_to_response, get_object_or_404
|
2011-09-27 09:27:15 +00:00
|
|
|
|
2011-10-25 01:29:01 +00:00
|
|
|
import oauth2 as oauth
|
2011-10-25 23:07:44 +00:00
|
|
|
from itertools import islice
|
2011-10-25 01:29:01 +00:00
|
|
|
|
2011-11-10 23:14:33 +00:00
|
|
|
import re
|
|
|
|
|
2011-10-20 03:31:16 +00:00
|
|
|
from regluit.core import tasks
|
2011-09-29 06:23:50 +00:00
|
|
|
from regluit.core import models, bookloader
|
2011-11-14 20:16:12 +00:00
|
|
|
from regluit.core import userlists
|
2011-09-29 06:23:50 +00:00
|
|
|
from regluit.core.search import gluejar_search
|
2011-10-25 01:29:01 +00:00
|
|
|
from regluit.core.goodreads import GoodreadsClient
|
2011-11-15 04:28:55 +00:00
|
|
|
from regluit.frontend.forms import UserData, ProfileForm, CampaignPledgeForm, GoodreadsShelfLoadingForm, RightsHolderForm
|
2011-10-13 21:13:37 +00:00
|
|
|
from regluit.payment.manager import PaymentManager
|
|
|
|
from regluit.payment.parameters import TARGET_TYPE_CAMPAIGN
|
|
|
|
|
2011-10-25 01:29:01 +00:00
|
|
|
from regluit.core import goodreads
|
2011-11-01 16:50:05 +00:00
|
|
|
from tastypie.models import ApiKey
|
2011-10-25 01:29:01 +00:00
|
|
|
|
2011-10-12 17:47:48 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2011-10-03 16:36:04 +00:00
|
|
|
|
2011-10-13 16:23:57 +00:00
|
|
|
from regluit.payment.models import Transaction
|
|
|
|
|
2011-11-15 16:27:32 +00:00
|
|
|
import urllib
|
|
|
|
from re import sub
|
|
|
|
|
2011-08-31 03:46:55 +00:00
|
|
|
def home(request):
|
2011-09-27 09:27:15 +00:00
|
|
|
if request.user.is_authenticated():
|
|
|
|
return HttpResponseRedirect(reverse('supporter',
|
|
|
|
args=[request.user.username]))
|
2011-10-14 02:32:30 +00:00
|
|
|
return render(request, 'home.html', {'suppress_search_box': True})
|
2011-09-12 03:44:21 +00:00
|
|
|
|
2011-11-02 19:40:43 +00:00
|
|
|
def stub(request):
|
2011-11-09 17:24:26 +00:00
|
|
|
path = request.path[6:] # get rid of /stub/
|
|
|
|
return render(request,'stub.html', {'path': path})
|
2011-11-06 21:46:46 +00:00
|
|
|
|
2011-11-06 23:54:48 +00:00
|
|
|
def work(request, work_id, action='display'):
|
2011-10-20 03:31:16 +00:00
|
|
|
work = get_object_or_404(models.Work, id=work_id)
|
2011-11-06 22:44:50 +00:00
|
|
|
campaign = work.last_campaign()
|
|
|
|
if campaign:
|
2011-11-07 15:18:46 +00:00
|
|
|
q = Q(campaign=campaign) | Q(campaign__isnull=True)
|
|
|
|
premiums = models.Premium.objects.filter(q)
|
2011-11-07 20:39:02 +00:00
|
|
|
else:
|
|
|
|
premiums = None
|
2011-11-06 23:54:48 +00:00
|
|
|
if action == 'setup_campaign':
|
|
|
|
return render(request, 'setup_campaign.html', {'work': work})
|
|
|
|
else:
|
2011-11-14 20:40:33 +00:00
|
|
|
return render(request, 'work.html', {'work': work, 'premiums': premiums, 'ungluers': userlists.supporting_users(work, 5)})
|
2011-11-15 16:27:32 +00:00
|
|
|
|
|
|
|
def workstub(request, title, imagebase, image, author, googlebooks_id, action='display'):
|
|
|
|
premiums = None
|
2011-11-15 16:47:47 +00:00
|
|
|
title = urllib.unquote_plus(title)
|
|
|
|
imagebase = urllib.unquote_plus(imagebase)
|
|
|
|
image = urllib.unquote_plus(image)
|
|
|
|
author = urllib.unquote_plus(author)
|
|
|
|
return render(request, 'workstub.html', {'title': title, 'image': image, 'imagebase': imagebase, 'author': author, 'googlebooks_id': googlebooks_id, 'premiums': premiums, 'ungluers': userlists.other_users(supporter, 5)})
|
2011-10-20 03:31:16 +00:00
|
|
|
|
2011-11-15 02:13:39 +00:00
|
|
|
def subjects(request):
|
|
|
|
order = request.GET.get('order')
|
|
|
|
subjects = models.Subject.objects.all()
|
|
|
|
subjects = subjects.annotate(Count('editions'))
|
|
|
|
|
|
|
|
if request.GET.get('order') == 'count':
|
|
|
|
subjects = subjects.order_by('-editions__count')
|
|
|
|
else:
|
|
|
|
subjects = subjects.order_by('name')
|
2011-10-20 03:31:16 +00:00
|
|
|
|
2011-11-15 02:13:39 +00:00
|
|
|
return render(request, 'subjects.html', {'subjects': subjects})
|
2011-11-07 21:01:08 +00:00
|
|
|
|
|
|
|
def pledge(request,work_id):
|
|
|
|
work = get_object_or_404(models.Work, id=work_id)
|
|
|
|
campaign = work.last_campaign()
|
|
|
|
if campaign:
|
|
|
|
premiums = campaign.premiums.all()
|
|
|
|
if premiums.count() == 0:
|
2011-11-09 18:05:03 +00:00
|
|
|
premiums = models.Premium.objects.filter(campaign__isnull=True)
|
|
|
|
premium_id = request.GET.get('premium_id', None)
|
|
|
|
if premium_id is not None:
|
|
|
|
preapproval_amount = D(models.Premium.objects.get(id=premium_id).amount)
|
|
|
|
else:
|
|
|
|
preapproval_amount = D('0.00')
|
|
|
|
data = {'preapproval_amount':preapproval_amount}
|
|
|
|
form = CampaignPledgeForm(data)
|
|
|
|
|
2011-11-07 21:01:08 +00:00
|
|
|
return render(request,'pledge.html',{'work':work,'campaign':campaign, 'premiums':premiums, 'form':form})
|
|
|
|
|
2011-11-15 04:28:55 +00:00
|
|
|
def rh_admin(request):
|
|
|
|
if not is_admin(request.user):
|
|
|
|
return render(request, "admins_only.html")
|
|
|
|
if request.method == 'POST':
|
|
|
|
form = RightsHolderForm(data=request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
form.save()
|
|
|
|
else:
|
|
|
|
form = RightsHolderForm()
|
|
|
|
rights_holders = models.RightsHolder.objects.all()
|
|
|
|
context = { 'request': request, 'rights_holders': rights_holders, 'form': form }
|
|
|
|
return render(request, "rights_holders.html", context)
|
|
|
|
|
|
|
|
def is_admin(user):
|
|
|
|
for name,email in settings.ADMINS :
|
|
|
|
if email == user.email :
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2011-11-03 20:28:53 +00:00
|
|
|
def supporter(request, supporter_username, template_name):
|
2011-10-19 07:13:29 +00:00
|
|
|
supporter = get_object_or_404(User, username=supporter_username)
|
|
|
|
wishlist = supporter.wishlist
|
|
|
|
backed = 0
|
|
|
|
backing = 0
|
|
|
|
transet = Transaction.objects.all().filter(user = supporter)
|
|
|
|
|
|
|
|
for transaction in transet:
|
2011-11-09 17:24:26 +00:00
|
|
|
try:
|
|
|
|
if(transaction.campaign.status == 'SUCCESSFUL'):
|
|
|
|
backed += 1
|
|
|
|
elif(transaction.campaign.status == 'ACTIVE'):
|
|
|
|
backing += 1
|
|
|
|
except:
|
|
|
|
continue
|
2011-10-19 07:13:29 +00:00
|
|
|
|
|
|
|
wished = supporter.wishlist.works.count()
|
|
|
|
|
|
|
|
date = supporter.date_joined.strftime("%B %d, %Y")
|
2011-10-14 14:18:38 +00:00
|
|
|
|
2011-10-19 07:13:29 +00:00
|
|
|
# figure out what works the users have in commmon if someone
|
2011-11-03 20:28:53 +00:00
|
|
|
# is looking at someone else's supporter page
|
2011-10-19 07:13:29 +00:00
|
|
|
if not request.user.is_anonymous and request.user != supporter:
|
|
|
|
w1 = request.user.wishlist
|
|
|
|
w2 = supporter.wishlist
|
|
|
|
shared_works = models.Work.objects.filter(wishlists__in=[w1])
|
|
|
|
shared_works = list(shared_works.filter(wishlists__in=[w2]))
|
|
|
|
else:
|
|
|
|
shared_works = []
|
|
|
|
|
2011-10-23 18:40:06 +00:00
|
|
|
# following block to support profile admin form in supporter page
|
2011-10-19 07:13:29 +00:00
|
|
|
if request.user.is_authenticated() and request.user.username == supporter_username:
|
2011-11-09 17:24:26 +00:00
|
|
|
try:
|
|
|
|
profile_obj=request.user.get_profile()
|
|
|
|
except ObjectDoesNotExist:
|
|
|
|
profile_obj= models.UserProfile()
|
|
|
|
profile_obj.user=request.user
|
2011-10-19 07:13:29 +00:00
|
|
|
if request.method == 'POST':
|
2011-10-23 18:40:06 +00:00
|
|
|
profile_form = ProfileForm(data=request.POST,instance=profile_obj)
|
2011-10-19 07:13:29 +00:00
|
|
|
if profile_form.is_valid():
|
2011-11-14 18:23:14 +00:00
|
|
|
if profile_form.cleaned_data['clear_facebook'] or profile_form.cleaned_data['clear_twitter'] :
|
|
|
|
if profile_form.cleaned_data['clear_facebook']:
|
|
|
|
profile_obj.facebook_id=0
|
|
|
|
if profile_form.cleaned_data['clear_twitter']:
|
|
|
|
profile_obj.twitter_id=""
|
|
|
|
profile_obj.save()
|
2011-10-19 07:13:29 +00:00
|
|
|
profile_form.save()
|
|
|
|
else:
|
2011-11-09 17:24:26 +00:00
|
|
|
profile_form= ProfileForm(instance=profile_obj)
|
2011-10-19 16:55:48 +00:00
|
|
|
else:
|
|
|
|
profile_form = ''
|
2011-10-19 07:13:29 +00:00
|
|
|
|
|
|
|
context = {
|
2011-10-14 14:18:38 +00:00
|
|
|
"supporter": supporter,
|
|
|
|
"wishlist": wishlist,
|
|
|
|
"backed": backed,
|
|
|
|
"backing": backing,
|
|
|
|
"wished": wished,
|
|
|
|
"date": date,
|
|
|
|
"shared_works": shared_works,
|
2011-10-19 07:13:29 +00:00
|
|
|
"profile_form": profile_form,
|
2011-11-14 20:16:12 +00:00
|
|
|
"ungluers": userlists.other_users(supporter, 5 )
|
2011-10-19 07:13:29 +00:00
|
|
|
}
|
|
|
|
|
2011-11-03 20:28:53 +00:00
|
|
|
return render(request, template_name, context)
|
2011-10-03 16:36:04 +00:00
|
|
|
|
|
|
|
def edit_user(request):
|
|
|
|
form=UserData()
|
|
|
|
if not request.user.is_authenticated():
|
|
|
|
return HttpResponseRedirect(reverse('auth_login'))
|
|
|
|
oldusername=request.user.username
|
|
|
|
if request.method == 'POST':
|
|
|
|
# surely there's a better way to add data to the POST data?
|
|
|
|
postcopy=request.POST.copy()
|
|
|
|
postcopy['oldusername']=oldusername
|
|
|
|
form = UserData(postcopy)
|
|
|
|
if form.is_valid(): # All validation rules pass, go and change the username
|
|
|
|
request.user.username=form.cleaned_data['username']
|
|
|
|
request.user.save()
|
|
|
|
return HttpResponseRedirect(reverse('home')) # Redirect after POST
|
|
|
|
return render(request,'registration/user_change_form.html', {'form': form},)
|
|
|
|
|
|
|
|
|
2011-09-29 01:36:47 +00:00
|
|
|
def search(request):
|
2011-09-29 01:54:50 +00:00
|
|
|
q = request.GET.get('q', None)
|
2011-09-29 06:23:50 +00:00
|
|
|
results = gluejar_search(q)
|
|
|
|
|
2011-10-10 22:08:53 +00:00
|
|
|
# flag search result as on wishlist as appropriate
|
2011-09-30 01:01:28 +00:00
|
|
|
if not request.user.is_anonymous():
|
2011-10-10 22:08:53 +00:00
|
|
|
# get a list of all the googlebooks_ids for works on the user's wishlist
|
|
|
|
wishlist = request.user.wishlist
|
|
|
|
editions = models.Edition.objects.filter(work__wishlists__in=[wishlist])
|
|
|
|
googlebooks_ids = [e['googlebooks_id'] for e in editions.values('googlebooks_id')]
|
2011-11-14 20:40:33 +00:00
|
|
|
ungluers = userlists.other_users(request.user, 5)
|
2011-10-10 22:08:53 +00:00
|
|
|
# if the results is on their wishlist flag it
|
2011-09-29 06:23:50 +00:00
|
|
|
for result in results:
|
2011-10-10 22:08:53 +00:00
|
|
|
if result['googlebooks_id'] in googlebooks_ids:
|
2011-09-29 06:23:50 +00:00
|
|
|
result['on_wishlist'] = True
|
|
|
|
else:
|
|
|
|
result['on_wishlist'] = False
|
2011-11-14 20:40:33 +00:00
|
|
|
else:
|
2011-11-15 16:47:47 +00:00
|
|
|
ungluers = userlists.other_users(None, 5)
|
2011-11-15 16:27:32 +00:00
|
|
|
|
|
|
|
# also urlencode some parameters we'll need to pass to workstub in the title links
|
|
|
|
# needs to be done outside the if condition
|
|
|
|
for result in results:
|
2011-11-15 16:47:47 +00:00
|
|
|
result['urlimage'] = urllib.quote_plus(sub('^https?:\/\/','', result['image']).encode("utf-8"), safe='')
|
|
|
|
result['urlauthor'] = urllib.quote_plus(result['author'].encode("utf-8"), safe='')
|
|
|
|
result['urltitle'] = urllib.quote_plus(result['title'].encode("utf-8"), safe='')
|
2011-09-29 06:23:50 +00:00
|
|
|
|
2011-09-29 01:54:50 +00:00
|
|
|
context = {
|
|
|
|
"q": q,
|
|
|
|
"results": results,
|
2011-11-14 20:40:33 +00:00
|
|
|
"ungluers": ungluers
|
2011-09-29 01:54:50 +00:00
|
|
|
}
|
|
|
|
return render(request, 'search.html', context)
|
2011-09-29 06:23:50 +00:00
|
|
|
|
2011-09-29 11:44:03 +00:00
|
|
|
# TODO: perhaps this functionality belongs in the API?
|
2011-09-29 06:23:50 +00:00
|
|
|
@require_POST
|
|
|
|
@login_required
|
2011-10-10 16:57:10 +00:00
|
|
|
@csrf_exempt
|
2011-09-29 06:23:50 +00:00
|
|
|
def wishlist(request):
|
2011-10-10 16:57:10 +00:00
|
|
|
googlebooks_id = request.POST.get('googlebooks_id', None)
|
2011-09-29 11:44:03 +00:00
|
|
|
remove_work_id = request.POST.get('remove_work_id', None)
|
2011-10-10 16:57:10 +00:00
|
|
|
if googlebooks_id:
|
|
|
|
edition = bookloader.add_by_googlebooks_id(googlebooks_id)
|
2011-10-20 03:31:16 +00:00
|
|
|
# add related editions asynchronously
|
|
|
|
tasks.add_related.delay(edition.isbn_10)
|
2011-10-10 16:57:10 +00:00
|
|
|
request.user.wishlist.works.add(edition.work)
|
2011-09-29 11:44:03 +00:00
|
|
|
# TODO: redirect to work page, when it exists
|
|
|
|
return HttpResponseRedirect('/')
|
|
|
|
elif remove_work_id:
|
|
|
|
work = models.Work.objects.get(id=int(remove_work_id))
|
|
|
|
request.user.wishlist.works.remove(work)
|
2011-09-29 16:42:19 +00:00
|
|
|
# TODO: where to redirect?
|
2011-09-29 11:44:03 +00:00
|
|
|
return HttpResponseRedirect('/')
|
2011-10-14 02:16:28 +00:00
|
|
|
|
2011-10-12 17:47:48 +00:00
|
|
|
class CampaignFormView(FormView):
|
|
|
|
template_name="campaign_detail.html"
|
|
|
|
form_class = CampaignPledgeForm
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
pk = self.kwargs["pk"]
|
|
|
|
campaign = models.Campaign.objects.get(id=int(pk))
|
|
|
|
context = super(CampaignFormView, self).get_context_data(**kwargs)
|
|
|
|
context.update({
|
2011-10-14 02:16:28 +00:00
|
|
|
'campaign': campaign
|
2011-10-13 21:13:37 +00:00
|
|
|
})
|
|
|
|
return context
|
2011-10-20 03:31:16 +00:00
|
|
|
|
2011-10-13 21:13:37 +00:00
|
|
|
def form_valid(self,form):
|
|
|
|
pk = self.kwargs["pk"]
|
2011-10-18 23:27:20 +00:00
|
|
|
preapproval_amount = form.cleaned_data["preapproval_amount"]
|
2011-11-01 21:08:09 +00:00
|
|
|
anonymous = form.cleaned_data["anonymous"]
|
2011-10-18 23:27:20 +00:00
|
|
|
|
|
|
|
# right now, if there is a non-zero pledge amount, go with that. otherwise, do the pre_approval
|
2011-10-13 21:13:37 +00:00
|
|
|
campaign = models.Campaign.objects.get(id=int(pk))
|
|
|
|
|
|
|
|
p = PaymentManager()
|
|
|
|
|
2011-10-18 23:27:20 +00:00
|
|
|
# we should force login at this point -- or if no account, account creation, login, and return to this spot
|
2011-10-13 21:13:37 +00:00
|
|
|
if self.request.user.is_authenticated():
|
|
|
|
user = self.request.user
|
|
|
|
else:
|
|
|
|
user = None
|
2011-10-18 23:27:20 +00:00
|
|
|
|
2011-11-09 18:05:03 +00:00
|
|
|
# calculate the work corresponding to the campaign id
|
|
|
|
work_id = campaign.work.id
|
|
|
|
return_url = self.request.build_absolute_uri(reverse('work',kwargs={'work_id': str(work_id)}))
|
|
|
|
t, url = p.authorize('USD', TARGET_TYPE_CAMPAIGN, preapproval_amount, campaign=campaign, list=None, user=user,
|
|
|
|
return_url=return_url, anonymous=anonymous)
|
|
|
|
|
|
|
|
#else:
|
|
|
|
# # instant payment: send to the partnering RH
|
|
|
|
# # right now, all money going to Gluejar.
|
|
|
|
# receiver_list = [{'email':settings.PAYPAL_GLUEJAR_EMAIL, 'amount':pledge_amount}]
|
|
|
|
#
|
|
|
|
# # redirect the page back to campaign page on success
|
|
|
|
# #return_url = self.request.build_absolute_uri("/campaigns/%s" %(str(pk)))
|
|
|
|
# return_url = self.request.build_absolute_uri(reverse('campaign_by_id',kwargs={'pk': str(pk)}))
|
|
|
|
# t, url = p.pledge('USD', TARGET_TYPE_CAMPAIGN, receiver_list, campaign=campaign, list=None, user=user,
|
|
|
|
# return_url=return_url, anonymous=anonymous)
|
2011-10-13 21:13:37 +00:00
|
|
|
|
|
|
|
if url:
|
2011-10-14 02:16:28 +00:00
|
|
|
logger.info("CampaignFormView paypal: " + url)
|
2011-10-13 21:13:37 +00:00
|
|
|
return HttpResponseRedirect(url)
|
|
|
|
else:
|
|
|
|
response = t.reference
|
2011-10-14 02:16:28 +00:00
|
|
|
logger.info("CampaignFormView paypal: Error " + str(t.reference))
|
2011-10-14 14:42:33 +00:00
|
|
|
return HttpResponse(response)
|
2011-10-25 01:29:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GoodreadsDisplayView(TemplateView):
|
|
|
|
template_name = "goodreads_display.html"
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(GoodreadsDisplayView, self).get_context_data(**kwargs)
|
|
|
|
session = self.request.session
|
|
|
|
gr_client = GoodreadsClient(key=settings.GOODREADS_API_KEY, secret=settings.GOODREADS_API_SECRET)
|
2011-10-29 22:40:00 +00:00
|
|
|
|
|
|
|
user = self.request.user
|
2011-11-01 16:50:05 +00:00
|
|
|
if user.is_authenticated():
|
|
|
|
api_key = ApiKey.objects.filter(user=user)[0].key
|
|
|
|
context['api_key'] = api_key
|
2011-10-25 01:29:01 +00:00
|
|
|
|
2011-10-29 22:40:00 +00:00
|
|
|
if user.profile.goodreads_user_id is None:
|
2011-10-25 01:29:01 +00:00
|
|
|
# calculate the Goodreads authorization URL
|
|
|
|
(context["goodreads_auth_url"], request_token) = gr_client.begin_authorization(self.request.build_absolute_uri(reverse('goodreads_cb')))
|
2011-10-29 22:40:00 +00:00
|
|
|
logger.info("goodreads_auth_url: %s" %(context["goodreads_auth_url"]))
|
2011-10-25 01:29:01 +00:00
|
|
|
# store request token in session so that we can redeem it for auth_token if authorization works
|
|
|
|
session['goodreads_request_token'] = request_token['oauth_token']
|
|
|
|
session['goodreads_request_secret'] = request_token['oauth_token_secret']
|
2011-10-29 22:40:00 +00:00
|
|
|
else:
|
2011-11-01 00:26:05 +00:00
|
|
|
gr_shelves = gr_client.shelves_list(user_id=user.profile.goodreads_user_id)
|
|
|
|
context["shelves_info"] = gr_shelves
|
|
|
|
gr_shelf_load_form = GoodreadsShelfLoadingForm()
|
|
|
|
# load the shelves into the form
|
2011-11-10 23:14:33 +00:00
|
|
|
choices = [('all:%d' % (gr_shelves["total_book_count"]),'all (%d)' % (gr_shelves["total_book_count"]))] + \
|
|
|
|
[("%s:%d" % (s["name"], s["book_count"]) ,"%s (%d)" % (s["name"],s["book_count"])) for s in gr_shelves["user_shelves"]]
|
2011-11-15 20:51:38 +00:00
|
|
|
gr_shelf_load_form.fields['goodreads_shelf_name_number'].widget = Select(choices=tuple(choices))
|
2011-10-25 01:29:01 +00:00
|
|
|
|
2011-11-01 00:26:05 +00:00
|
|
|
context["gr_shelf_load_form"] = gr_shelf_load_form
|
2011-11-10 23:14:33 +00:00
|
|
|
|
|
|
|
# also load any CeleryTasks associated with the user
|
|
|
|
context["celerytasks"] = models.CeleryTask.objects.filter(user=user)
|
|
|
|
|
2011-10-25 01:29:01 +00:00
|
|
|
return context
|
2011-10-29 22:40:00 +00:00
|
|
|
|
|
|
|
@login_required
|
2011-10-25 01:29:01 +00:00
|
|
|
def goodreads_cb(request):
|
2011-10-29 22:40:00 +00:00
|
|
|
"""handle callback from Goodreads"""
|
|
|
|
|
2011-10-25 01:29:01 +00:00
|
|
|
session = request.session
|
|
|
|
authorized_flag = request.GET['authorize'] # is it '1'?
|
|
|
|
request_oauth_token = request.GET['oauth_token']
|
|
|
|
|
|
|
|
if authorized_flag == '1':
|
|
|
|
request_token = {'oauth_token': session.get('goodreads_request_token'),
|
|
|
|
'oauth_token_secret': session.get('goodreads_request_secret')}
|
|
|
|
gr_client = GoodreadsClient(key=settings.GOODREADS_API_KEY, secret=settings.GOODREADS_API_SECRET)
|
|
|
|
|
|
|
|
access_token = gr_client.complete_authorization(request_token)
|
|
|
|
|
2011-10-29 22:40:00 +00:00
|
|
|
# store the access token in the user profile
|
|
|
|
profile = request.user.profile
|
|
|
|
profile.goodreads_auth_token = access_token["oauth_token"]
|
|
|
|
profile.goodreads_auth_secret = access_token["oauth_token_secret"]
|
2011-10-25 01:29:01 +00:00
|
|
|
|
|
|
|
# let's get the userid, username
|
|
|
|
user = gr_client.auth_user()
|
2011-10-29 22:40:00 +00:00
|
|
|
|
|
|
|
profile.goodreads_user_id = user["userid"]
|
|
|
|
profile.goodreads_user_name = user["name"]
|
|
|
|
profile.goodreads_user_link = user["link"]
|
|
|
|
|
|
|
|
profile.save() # is this needed?
|
2011-10-25 01:29:01 +00:00
|
|
|
|
|
|
|
# redirect to the Goodreads display page -- should observe some next later
|
|
|
|
return HttpResponseRedirect(reverse('goodreads_display'))
|
2011-11-01 00:26:05 +00:00
|
|
|
|
|
|
|
@require_POST
|
|
|
|
@login_required
|
|
|
|
@csrf_exempt
|
|
|
|
def goodreads_flush_assoc(request):
|
2011-10-29 22:40:00 +00:00
|
|
|
user = request.user
|
|
|
|
if user.is_authenticated():
|
|
|
|
profile = user.profile
|
|
|
|
profile.goodreads_user_id = None
|
|
|
|
profile.goodreads_user_name = None
|
|
|
|
profile.goodreads_user_link = None
|
|
|
|
profile.goodreads_auth_token = None
|
|
|
|
profile.goodreads_auth_secret = None
|
|
|
|
profile.save()
|
2011-11-10 15:36:17 +00:00
|
|
|
logger.info('Goodreads association flushed for user %s', user)
|
2011-11-01 00:26:05 +00:00
|
|
|
return HttpResponseRedirect(reverse('goodreads_display'))
|
2011-10-25 01:29:01 +00:00
|
|
|
|
2011-11-01 00:26:05 +00:00
|
|
|
@require_POST
|
|
|
|
@login_required
|
|
|
|
@csrf_exempt
|
|
|
|
def goodreads_load_shelf(request):
|
|
|
|
"""
|
|
|
|
a view to allow user load goodreads shelf into her wishlist
|
|
|
|
"""
|
|
|
|
# Should be moved to the API
|
2011-11-15 20:51:38 +00:00
|
|
|
goodreads_shelf_name_number = request.POST.get('goodreads_shelf_name_number', 'all:0')
|
2011-11-01 00:26:05 +00:00
|
|
|
user = request.user
|
|
|
|
try:
|
2011-11-10 23:14:33 +00:00
|
|
|
# parse out shelf name and expected number of books
|
2011-11-15 20:51:38 +00:00
|
|
|
(shelf_name, expected_number_of_books) = re.match(r'^(.*):(\d+)$', goodreads_shelf_name_number).groups()
|
2011-11-10 23:14:33 +00:00
|
|
|
expected_number_of_books = int(expected_number_of_books)
|
|
|
|
logger.info('Adding task to load shelf %s to user %s with %d books', shelf_name, user, expected_number_of_books)
|
|
|
|
load_task_name = "load_goodreads_shelf_into_wishlist"
|
|
|
|
load_task = getattr(tasks, load_task_name)
|
2011-11-15 20:51:38 +00:00
|
|
|
task_id = load_task.delay(user, shelf_name, expected_number_of_books=expected_number_of_books)
|
2011-11-10 23:14:33 +00:00
|
|
|
|
|
|
|
ct = models.CeleryTask()
|
|
|
|
ct.task_id = task_id
|
|
|
|
ct.function_name = load_task_name
|
|
|
|
ct.user = user
|
|
|
|
ct.description = "Loading Goodread shelf %s to user %s with %s books" % (shelf_name, user, expected_number_of_books)
|
|
|
|
ct.save()
|
|
|
|
|
2011-11-01 00:26:05 +00:00
|
|
|
return HttpResponse("Shelf loading placed on task queue.")
|
|
|
|
except Exception,e:
|
|
|
|
return HttpResponse("Error in loading shelf: %s " % (e))
|
2011-11-10 15:36:17 +00:00
|
|
|
logger.info("Error in loading shelf for user %s: %s ", user, e)
|
2011-11-01 00:26:05 +00:00
|
|
|
|
|
|
|
@require_POST
|
|
|
|
@login_required
|
2011-11-01 17:41:39 +00:00
|
|
|
@csrf_exempt
|
2011-11-01 00:26:05 +00:00
|
|
|
def clear_wishlist(request):
|
2011-11-01 17:41:39 +00:00
|
|
|
try:
|
|
|
|
request.user.wishlist.works.clear()
|
2011-11-10 15:36:17 +00:00
|
|
|
logger.info("Wishlist for user %s cleared", request.user)
|
2011-11-01 17:41:39 +00:00
|
|
|
return HttpResponse('wishlist cleared')
|
|
|
|
except Exception, e:
|
|
|
|
return HttpResponse("Error in clearing wishlist: %s " % (e))
|
2011-11-10 15:36:17 +00:00
|
|
|
logger.info("Error in clearing wishlist for user %s: %s ", request.user, e)
|
2011-11-01 21:24:18 +00:00
|
|
|
|
2011-11-10 23:14:33 +00:00
|
|
|
|
|
|
|
@require_POST
|
|
|
|
@login_required
|
|
|
|
@csrf_exempt
|
|
|
|
def clear_celery_tasks(request):
|
|
|
|
try:
|
|
|
|
request.user.tasks.clear()
|
|
|
|
logger.info("Celery tasks for user %s cleared", request.user)
|
|
|
|
return HttpResponse('Celery Tasks List cleared')
|
|
|
|
except Exception, e:
|
|
|
|
return HttpResponse("Error in clearing Celery Tasks: %s " % (e))
|
|
|
|
logger.info("Error in clearing Celery Tasks for user %s: %s ", request.user, e)
|
2011-11-06 21:46:46 +00:00
|
|
|
|
2011-11-10 01:31:31 +00:00
|
|
|
def celery_test(request):
|
|
|
|
return HttpResponse("celery_test")
|