diff --git a/core/models.py b/core/models.py index f0de84bf..4c283d60 100755 --- a/core/models.py +++ b/core/models.py @@ -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): diff --git a/frontend/views.py b/frontend/views.py index 1f0c2bd5..091cc906 100755 --- a/frontend/views.py +++ b/frontend/views.py @@ -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)