2011-09-14 15:38:29 +00:00
|
|
|
from django.template import RequestContext
|
|
|
|
from django.shortcuts import render_to_response, get_object_or_404
|
2011-09-21 05:22:48 +00:00
|
|
|
from django.contrib import auth
|
2011-09-29 18:40:45 +00:00
|
|
|
from django.contrib.auth.models import User
|
2011-09-14 15:38:29 +00:00
|
|
|
from django.db.models import Q
|
2011-10-20 00:08:17 +00:00
|
|
|
from django.views.generic.base import TemplateView
|
2011-09-14 15:38:29 +00:00
|
|
|
|
|
|
|
from regluit.core import models
|
2011-12-20 04:26:55 +00:00
|
|
|
import regluit.core.isbn
|
|
|
|
|
2011-10-20 00:08:17 +00:00
|
|
|
from tastypie.models import ApiKey
|
2011-09-14 15:38:29 +00:00
|
|
|
|
|
|
|
def isbn(request,isbn):
|
2011-12-20 04:26:55 +00:00
|
|
|
if len(isbn)==10:
|
|
|
|
isbn=regluit.core.isbn.convert_10_to_13(isbn)
|
2012-01-10 20:20:02 +00:00
|
|
|
try:
|
|
|
|
edition = models.Identifier.objects.get( Q(type = 'isbn', value = isbn)).edition
|
|
|
|
editions = [edition]
|
|
|
|
except models.Identifier.DoesNotExist:
|
|
|
|
editions = []
|
2011-09-14 15:38:29 +00:00
|
|
|
return render_to_response('isbn.html',
|
|
|
|
{'isbn':isbn, 'editions':editions},
|
|
|
|
context_instance=RequestContext(request)
|
|
|
|
)
|
|
|
|
|
2011-09-14 17:31:16 +00:00
|
|
|
def editions(request):
|
|
|
|
editions = models.Edition.objects.all()
|
|
|
|
return render_to_response('editions.html',
|
|
|
|
{'editions':editions},
|
|
|
|
context_instance=RequestContext(request)
|
|
|
|
)
|
2011-09-21 05:22:48 +00:00
|
|
|
|
|
|
|
def widget(request,isbn):
|
|
|
|
"""
|
|
|
|
Aim is to ultimately supply the following info:
|
|
|
|
- campaign name
|
|
|
|
- campaign description
|
|
|
|
- rights holder
|
|
|
|
- work title
|
|
|
|
- work author
|
|
|
|
- a link to an edition cover
|
|
|
|
- a link to the campaign on unglue.it- the status, or progress of the ungluing
|
|
|
|
- when the campaign is finished
|
|
|
|
- whether the logged in user is a supporter
|
|
|
|
- whether the logged in user is currently supporting the campaign
|
|
|
|
Current implementation is to supply info for current book panel design
|
|
|
|
"""
|
|
|
|
|
2011-12-20 04:26:55 +00:00
|
|
|
if len(isbn)==10:
|
2012-01-10 20:20:02 +00:00
|
|
|
isbn = regluit.core.isbn.convert_10_to_13(isbn)
|
|
|
|
try:
|
|
|
|
identifier = models.Identifier.objects.get( Q( type = 'isbn', value = isbn ))
|
|
|
|
work = identifier.work
|
|
|
|
edition = identifier.edition
|
|
|
|
campaigns = work.campaigns.all()
|
|
|
|
except models.Identifer.DoesNotExist:
|
2011-09-21 05:22:48 +00:00
|
|
|
edition = None
|
|
|
|
work = None
|
|
|
|
campaigns = []
|
|
|
|
|
|
|
|
u = auth.get_user(request)
|
|
|
|
if isinstance(u, User):
|
|
|
|
logged_in_username = u.username
|
|
|
|
else:
|
|
|
|
logged_in_username = None
|
|
|
|
|
2011-10-19 16:45:25 +00:00
|
|
|
# for now pass in first campaign -- but should loop through to prioritize any active campaigns
|
|
|
|
if len(campaigns):
|
|
|
|
campaign = campaigns[0]
|
|
|
|
progress = int(100*campaign.current_total/campaign.target)
|
|
|
|
else:
|
|
|
|
campaign = None
|
|
|
|
progress = None
|
|
|
|
|
2011-09-21 05:22:48 +00:00
|
|
|
return render_to_response('widget.html',
|
2011-10-19 16:45:25 +00:00
|
|
|
{'isbn':isbn,'edition':edition, 'work':work, 'campaign':campaign, 'progress': progress,
|
|
|
|
'logged_in_username':logged_in_username},
|
2011-09-21 05:22:48 +00:00
|
|
|
context_instance=RequestContext(request)
|
|
|
|
)
|
2011-10-20 00:08:17 +00:00
|
|
|
|
|
|
|
class ApiHelpView(TemplateView):
|
|
|
|
template_name = "api_help.html"
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(ApiHelpView, self).get_context_data(**kwargs)
|
|
|
|
|
|
|
|
# base_url passed in to allow us to write absolute URLs for this site
|
|
|
|
base_url = self.request.build_absolute_uri("/")[:-1]
|
|
|
|
context["base_url"] = base_url
|
|
|
|
|
|
|
|
# if user is logged in, pass in the user's API key
|
|
|
|
u = auth.get_user(self.request)
|
|
|
|
if u.is_authenticated():
|
|
|
|
api_key = ApiKey.objects.filter(user=u)[0].key
|
|
|
|
context['api_key'] = api_key
|
|
|
|
|
|
|
|
# pass in a sample Campaign whose widget can be displayed
|
|
|
|
campaigns = models.Campaign.objects.all()
|
|
|
|
if len(campaigns):
|
|
|
|
c = campaigns[0]
|
2012-01-10 20:20:02 +00:00
|
|
|
isbn = c.work.first_isbn_13
|
2011-10-20 00:08:17 +00:00
|
|
|
context["campaign"] = campaigns[0]
|
|
|
|
context["campaign_isbn"] = isbn
|
|
|
|
|
|
|
|
return context
|