2011-09-04 04:21:51 +00:00
|
|
|
from django.template import RequestContext
|
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
|
|
|
|
from django.http import HttpResponseRedirect
|
2011-09-29 06:23:50 +00:00
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
|
|
from django.views.decorators.http import require_POST
|
|
|
|
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-09-29 06:23:50 +00:00
|
|
|
from regluit.core import models, bookloader
|
|
|
|
from regluit.core.search import gluejar_search
|
2011-09-12 03:44:21 +00:00
|
|
|
|
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-09-29 01:54:50 +00:00
|
|
|
return render(request, 'home.html')
|
2011-09-12 03:44:21 +00:00
|
|
|
|
2011-09-12 18:20:36 +00:00
|
|
|
def supporter(request, supporter_username):
|
|
|
|
supporter = get_object_or_404(User, username=supporter_username)
|
2011-09-29 06:23:50 +00:00
|
|
|
wishlist = supporter.wishlist
|
2011-09-29 01:54:50 +00:00
|
|
|
context = {
|
|
|
|
"supporter": supporter,
|
2011-09-29 06:23:50 +00:00
|
|
|
"wishlist": wishlist,
|
2011-09-29 01:54:50 +00:00
|
|
|
}
|
|
|
|
return render(request, 'supporter.html', context)
|
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)
|
|
|
|
|
|
|
|
# flag search result as on wishlist
|
|
|
|
# TODO: make this better and faster
|
|
|
|
if request.user:
|
|
|
|
for result in results:
|
|
|
|
if not result.has_key('isbn_10'):
|
|
|
|
continue
|
|
|
|
work = models.Work.get_by_isbn(result['isbn_10'])
|
|
|
|
if work and work in request.user.wishlist.works.all():
|
|
|
|
result['on_wishlist'] = True
|
|
|
|
else:
|
|
|
|
result['on_wishlist'] = False
|
|
|
|
|
2011-09-29 01:54:50 +00:00
|
|
|
context = {
|
|
|
|
"q": q,
|
|
|
|
"results": results,
|
|
|
|
}
|
|
|
|
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
|
|
|
@csrf_exempt
|
|
|
|
@require_POST
|
2011-09-29 11:44:03 +00:00
|
|
|
#@login_required
|
2011-09-29 06:23:50 +00:00
|
|
|
def wishlist(request):
|
|
|
|
isbn = request.POST.get('isbn', None)
|
2011-09-29 11:44:03 +00:00
|
|
|
remove_work_id = request.POST.get('remove_work_id', None)
|
|
|
|
if isbn:
|
|
|
|
edition = models.Edition.get_by_isbn(isbn)
|
|
|
|
if not edition:
|
|
|
|
print "loading book"
|
|
|
|
edition = bookloader.add_book(isbn)
|
|
|
|
if edition:
|
|
|
|
print "adding edition"
|
|
|
|
request.user.wishlist.works.add(edition.work)
|
|
|
|
# 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)
|
|
|
|
return HttpResponseRedirect('/')
|