- form = forms.ObjectSetsForm(book, request.user)
- new_set_form = forms.NewSetForm()
-
- return render_to_response('catalogue/book_sets.html', locals(),
- context_instance=RequestContext(request))
-
-
-@login_required
-@require_POST
-@cache.never_cache
-def remove_from_shelf(request, shelf, book):
- book = get_object_or_404(models.Book, slug=book)
- shelf = get_object_or_404(models.Tag, slug=shelf, category='set', user=request.user)
-
- if shelf in book.tags:
- models.Tag.objects.remove_tag(book, shelf)
-
- shelf.book_count = None
- shelf.save()
-
- return HttpResponse(_('Book was successfully removed from the shelf'))
- else:
- return HttpResponse(_('This book is not on the shelf'))
-
-
-def collect_books(books):
- """
- Returns all real books in collection.
- """
- result = []
- for book in books:
- if len(book.children.all()) == 0:
- result.append(book)
- else:
- result += collect_books(book.children.all())
- return result
-
-
-@cache.never_cache
-def download_shelf(request, slug):
- """"
- Create a ZIP archive on disk and transmit it in chunks of 8KB,
- without loading the whole file into memory. A similar approach can
- be used for large dynamic PDF files.
- """
- shelf = get_object_or_404(models.Tag, slug=slug, category='set')
-
- formats = []
- form = forms.DownloadFormatsForm(request.GET)
- if form.is_valid():
- formats = form.cleaned_data['formats']
- if len(formats) == 0:
- formats = ['pdf', 'epub', 'odt', 'txt', 'mp3', 'ogg']
-
- # Create a ZIP archive
- temp = tempfile.TemporaryFile()
- archive = zipfile.ZipFile(temp, 'w')
-
- for book in collect_books(models.Book.tagged.with_all(shelf)):
- if 'pdf' in formats and book.pdf_file:
- filename = book.pdf_file.path
- archive.write(filename, str('%s.pdf' % book.slug))
- if 'epub' in formats and book.epub_file:
- filename = book.epub_file.path
- archive.write(filename, str('%s.epub' % book.slug))
- if 'odt' in formats and book.odt_file:
- filename = book.odt_file.path
- archive.write(filename, str('%s.odt' % book.slug))
- if 'txt' in formats and book.txt_file:
- filename = book.txt_file.path
- archive.write(filename, str('%s.txt' % book.slug))
- if 'mp3' in formats and book.mp3_file:
- filename = book.mp3_file.path
- archive.write(filename, str('%s.mp3' % book.slug))
- if 'ogg' in formats and book.ogg_file:
- filename = book.ogg_file.path
- archive.write(filename, str('%s.ogg' % book.slug))
- archive.close()
-
- response = HttpResponse(content_type='application/zip', mimetype='application/x-zip-compressed')
- response['Content-Disposition'] = 'attachment; filename=%s.zip' % shelf.sort_key
- response['Content-Length'] = temp.tell()
-
- temp.seek(0)
- response.write(temp.read())
- return response
-
-
-@cache.never_cache
-def shelf_book_formats(request, shelf):
- """"
- Returns a list of formats of books in shelf.
- """
- shelf = get_object_or_404(models.Tag, slug=shelf, category='set')
-
- formats = {'pdf': False, 'epub': False, 'odt': False, 'txt': False, 'mp3': False, 'ogg': False}
-
- for book in collect_books(models.Book.tagged.with_all(shelf)):
- if book.pdf_file:
- formats['pdf'] = True
- if book.epub_file:
- formats['epub'] = True
- if book.odt_file:
- formats['odt'] = True
- if book.txt_file:
- formats['txt'] = True
- if book.mp3_file:
- formats['mp3'] = True
- if book.ogg_file:
- formats['ogg'] = True
-
- return HttpResponse(LazyEncoder().encode(formats))
-
-
-@login_required
-@require_POST
-@cache.never_cache
-def new_set(request):
- new_set_form = forms.NewSetForm(request.POST)
- if new_set_form.is_valid():
- new_set = new_set_form.save(request.user)
-
- if request.is_ajax():
- return HttpResponse(_('<p>Shelf <strong>%s</strong> was successfully created</p>') % new_set)
- else:
- return HttpResponseRedirect('/')
-
- return HttpResponseRedirect('/')
-
-
-@login_required
-@require_POST
-@cache.never_cache
-def delete_shelf(request, slug):
- user_set = get_object_or_404(models.Tag, slug=slug, category='set', user=request.user)
- user_set.delete()
-
- if request.is_ajax():
- return HttpResponse(_('<p>Shelf <strong>%s</strong> was successfully removed</p>') % user_set.name)
- else:
- return HttpResponseRedirect('/')
-
-
-# ==================
-# = Authentication =
-# ==================
-@require_POST
-@cache.never_cache
-def login(request):
- form = AuthenticationForm(data=request.POST, prefix='login')
- if form.is_valid():
- auth.login(request, form.get_user())
- response_data = {'success': True, 'errors': {}}
- else:
- response_data = {'success': False, 'errors': form.errors}
- return HttpResponse(LazyEncoder(ensure_ascii=False).encode(response_data))
-
-
-@require_POST
-@cache.never_cache
-def register(request):
- registration_form = UserCreationForm(request.POST, prefix='registration')
- if registration_form.is_valid():
- user = registration_form.save()
- user = auth.authenticate(
- username=registration_form.cleaned_data['username'],
- password=registration_form.cleaned_data['password1']
- )
- auth.login(request, user)
- response_data = {'success': True, 'errors': {}}
- else:
- response_data = {'success': False, 'errors': registration_form.errors}
- return HttpResponse(LazyEncoder(ensure_ascii=False).encode(response_data))
-
-
-@cache.never_cache
-def logout_then_redirect(request):
- auth.logout(request)
- return HttpResponseRedirect(urlquote_plus(request.GET.get('next', '/'), safe='/?='))
-