1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 from django.shortcuts import render, get_object_or_404, redirect
6 from django.http import HttpResponseForbidden
7 from django.contrib.auth.decorators import login_required
8 #~ from django.utils.datastructures import SortedDict
9 from django.views.decorators.http import require_POST
10 #~ from django.contrib import auth
11 #~ from django.views.decorators import cache
12 from django.utils.translation import ugettext as _
14 from ajaxable.utils import LazyEncoder, JSONResponse, AjaxableFormView
16 from catalogue.models import Book, Tag
17 from social import forms
18 from social.utils import get_set, likes, set_sets
21 # ====================
22 # = Shelf management =
23 # ====================
27 def like_book(request, slug):
28 if not request.user.is_authenticated():
29 return HttpResponseForbidden('Login required.')
30 book = get_object_or_404(Book, slug=slug)
31 if not likes(request.user, book):
32 tag = get_set(request.user, '')
33 set_sets(request.user, book, [tag])
36 return JSONResponse({"success": True, "msg": "ok", "like": True})
42 def my_shelf(request):
43 books = Book.tagged.with_any(request.user.tag_set.all())
44 return render(request, 'social/my_shelf.html', locals())
47 class ObjectSetsFormView(AjaxableFormView):
48 form_class = forms.ObjectSetsForm
50 template = 'social/sets_form.html'
54 def get_object(self, request, slug):
55 return get_object_or_404(Book, slug=slug)
57 def context_description(self, request, obj):
58 return obj.pretty_title()
60 def form_args(self, request, obj):
61 return (obj, request.user), {}
64 def unlike_book(request, slug):
65 book = get_object_or_404(Book, slug=slug)
66 if likes(request.user, book):
67 set_sets(request.user, book, [])
70 return JSONResponse({"success": True, "msg": "ok", "like": False})
77 #~ def user_shelves(request):
78 #~ shelves = models.Tag.objects.filter(category='set', user=request.user)
79 #~ new_set_form = forms.NewSetForm()
80 #~ return render_to_response('social/user_shelves.html', locals(),
81 #~ context_instance=RequestContext(request))
84 #~ def book_sets(request, slug):
85 #~ if not request.user.is_authenticated():
86 #~ return HttpResponse(_('<p>To maintain your shelves you need to be logged in.</p>'))
88 #~ book = get_object_or_404(models.Book, slug=slug)
90 #~ user_sets = models.Tag.objects.filter(category='set', user=request.user)
91 #~ book_sets = book.tags.filter(category='set', user=request.user)
93 #~ if request.method == 'POST':
94 #~ form = forms.ObjectSetsForm(book, request.user, request.POST)
95 #~ if form.is_valid():
97 #~ if request.is_ajax():
98 #~ return JSONResponse('{"msg":"'+_("<p>Shelves were sucessfully saved.</p>")+'", "after":"close"}')
100 #~ return HttpResponseRedirect('/')
102 #~ form = forms.ObjectSetsForm(book, request.user)
103 #~ new_set_form = forms.NewSetForm()
105 #~ return render_to_response('social/book_sets.html', locals(),
106 #~ context_instance=RequestContext(request))
111 #~ @cache.never_cache
112 #~ def remove_from_shelf(request, shelf, slug):
113 #~ book = get_object_or_404(models.Book, slug=slug)
115 #~ shelf = get_object_or_404(models.Tag, slug=shelf, category='set', user=request.user)
117 #~ if shelf in book.tags:
118 #~ models.Tag.objects.remove_tag(book, shelf)
121 #~ return HttpResponse(_('Book was successfully removed from the shelf'))
123 #~ return HttpResponse(_('This book is not on the shelf'))
126 #~ def collect_books(books):
128 #~ Returns all real books in collection.
131 #~ for book in books:
132 #~ if len(book.children.all()) == 0:
133 #~ result.append(book)
135 #~ result += collect_books(book.children.all())
139 #~ @cache.never_cache
140 #~ def download_shelf(request, slug):
142 #~ Create a ZIP archive on disk and transmit it in chunks of 8KB,
143 #~ without loading the whole file into memory. A similar approach can
144 #~ be used for large dynamic PDF files.
146 #~ from slughifi import slughifi
150 #~ shelf = get_object_or_404(models.Tag, slug=slug, category='set')
153 #~ form = forms.DownloadFormatsForm(request.GET)
154 #~ if form.is_valid():
155 #~ formats = form.cleaned_data['formats']
156 #~ if len(formats) == 0:
157 #~ formats = models.Book.ebook_formats
159 #~ # Create a ZIP archive
160 #~ temp = tempfile.TemporaryFile()
161 #~ archive = zipfile.ZipFile(temp, 'w')
163 #~ for book in collect_books(models.Book.tagged.with_all(shelf)):
164 #~ for ebook_format in models.Book.ebook_formats:
165 #~ if ebook_format in formats and book.has_media(ebook_format):
166 #~ filename = book.get_media(ebook_format).path
167 #~ archive.write(filename, str('%s.%s' % (book.slug, ebook_format)))
170 #~ response = HttpResponse(content_type='application/zip', mimetype='application/x-zip-compressed')
171 #~ response['Content-Disposition'] = 'attachment; filename=%s.zip' % slughifi(shelf.name)
172 #~ response['Content-Length'] = temp.tell()
175 #~ response.write(temp.read())
179 #~ @cache.never_cache
180 #~ def shelf_book_formats(request, shelf):
182 #~ Returns a list of formats of books in shelf.
184 #~ shelf = get_object_or_404(models.Tag, slug=shelf, category='set')
187 #~ for ebook_format in models.Book.ebook_formats:
188 #~ formats[ebook_format] = False
190 #~ for book in collect_books(models.Book.tagged.with_all(shelf)):
191 #~ for ebook_format in models.Book.ebook_formats:
192 #~ if book.has_media(ebook_format):
193 #~ formats[ebook_format] = True
195 #~ return HttpResponse(LazyEncoder().encode(formats))
200 #~ @cache.never_cache
201 #~ def new_set(request):
202 #~ new_set_form = forms.NewSetForm(request.POST)
203 #~ if new_set_form.is_valid():
204 #~ new_set = new_set_form.save(request.user)
206 #~ if request.is_ajax():
207 #~ return JSONResponse('{"id":"%d", "name":"%s", "msg":"<p>Shelf <strong>%s</strong> was successfully created</p>"}' % (new_set.id, new_set.name, new_set))
209 #~ return HttpResponseRedirect('/')
211 #~ return HttpResponseRedirect('/')
216 #~ @cache.never_cache
217 #~ def delete_shelf(request, slug):
218 #~ user_set = get_object_or_404(models.Tag, slug=slug, category='set', user=request.user)
221 #~ if request.is_ajax():
222 #~ return HttpResponse(_('<p>Shelf <strong>%s</strong> was successfully removed</p>') % user_set.name)
224 #~ return HttpResponseRedirect('/')