and now, you can borrow a library book! yay!

pull/1/head
eric 2013-10-18 15:35:48 -04:00
parent 60cd2f626b
commit 6e7344d8a4
2 changed files with 33 additions and 3 deletions

View File

@ -331,9 +331,14 @@ class Acq(models.Model):
if self.on_reserve:
self.license=BORROWED
self.expire_in(timedelta(days=14))
self.user.wishlist.add_work( self.work, "borrow")
return self
if self.borrowable and user:
return Acq.objects.create(user=user,work=self.work,license= BORROWED)
elif self.borrowable and user:
user.wishlist.add_work( self.work, "borrow")
borrowed = Acq.objects.create(user=user,work=self.work,license= BORROWED, lib_acq=self)
from regluit.core.tasks import watermark_acq
watermark_acq.delay(borrowed)
return borrowed
@property
def borrowable(self):
@ -1150,6 +1155,7 @@ class Work(models.Model):
@property
def lib_acqs(self):
return self.acqs.filter(license=LIBRARY)
@property
def borrowable(self):

View File

@ -2624,8 +2624,32 @@ def download(request, work_id):
return render(request, "download.html", context)
@login_required
def borrow(request, work_id):
return download(request, work_id)
work = safe_get_work(work_id)
library = request.GET.get('library', '')
try:
libuser = User.objects.get(username = library)
except User.DoesNotExist:
libuser = None
if libuser:
acqs= models.Acq.objects.filter(user = libuser, license = LIBRARY, refreshes__lt = now())
if not libuser or acqs.count()==0:
acq=None
for other_library in request.user.profile.libraries:
if other_library.user!=libuser:
acqs= models.Acq.objects.filter(user = other_library.user, license = LIBRARY, refreshes__lt = now())
if acqs.count()>0:
acq=acqs[0]
continue
else:
acq=acqs[0]
if acq:
borrowed = acq.borrow(request.user)
return download(request, work_id)
else:
# shouldn't happen
return work(request, work_id)
def download_ebook(request, ebook_id):
ebook = get_object_or_404(models.Ebook,id=ebook_id)