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
49 template = 'social/sets_form.html'
53 def form_args(self, request, slug):
54 book = get_object_or_404(Book, slug=slug)
55 return (book, request.user), {}
58 def unlike_book(request, slug):
59 book = get_object_or_404(Book, slug=slug)
60 if likes(request.user, book):
61 set_sets(request.user, book, [])
64 return JSONResponse({"success": True, "msg": "ok", "like": False})
71 #~ def user_shelves(request):
72 #~ shelves = models.Tag.objects.filter(category='set', user=request.user)
73 #~ new_set_form = forms.NewSetForm()
74 #~ return render_to_response('social/user_shelves.html', locals(),
75 #~ context_instance=RequestContext(request))
78 #~ def book_sets(request, slug):
79 #~ if not request.user.is_authenticated():
80 #~ return HttpResponse(_('<p>To maintain your shelves you need to be logged in.</p>'))
82 #~ book = get_object_or_404(models.Book, slug=slug)
84 #~ user_sets = models.Tag.objects.filter(category='set', user=request.user)
85 #~ book_sets = book.tags.filter(category='set', user=request.user)
87 #~ if request.method == 'POST':
88 #~ form = forms.ObjectSetsForm(book, request.user, request.POST)
89 #~ if form.is_valid():
91 #~ if request.is_ajax():
92 #~ return JSONResponse('{"msg":"'+_("<p>Shelves were sucessfully saved.</p>")+'", "after":"close"}')
94 #~ return HttpResponseRedirect('/')
96 #~ form = forms.ObjectSetsForm(book, request.user)
97 #~ new_set_form = forms.NewSetForm()
99 #~ return render_to_response('social/book_sets.html', locals(),
100 #~ context_instance=RequestContext(request))
105 #~ @cache.never_cache
106 #~ def remove_from_shelf(request, shelf, slug):
107 #~ book = get_object_or_404(models.Book, slug=slug)
109 #~ shelf = get_object_or_404(models.Tag, slug=shelf, category='set', user=request.user)
111 #~ if shelf in book.tags:
112 #~ models.Tag.objects.remove_tag(book, shelf)
115 #~ return HttpResponse(_('Book was successfully removed from the shelf'))
117 #~ return HttpResponse(_('This book is not on the shelf'))
120 #~ def collect_books(books):
122 #~ Returns all real books in collection.
125 #~ for book in books:
126 #~ if len(book.children.all()) == 0:
127 #~ result.append(book)
129 #~ result += collect_books(book.children.all())
133 #~ @cache.never_cache
134 #~ def download_shelf(request, slug):
136 #~ Create a ZIP archive on disk and transmit it in chunks of 8KB,
137 #~ without loading the whole file into memory. A similar approach can
138 #~ be used for large dynamic PDF files.
140 #~ from slughifi import slughifi
144 #~ shelf = get_object_or_404(models.Tag, slug=slug, category='set')
147 #~ form = forms.DownloadFormatsForm(request.GET)
148 #~ if form.is_valid():
149 #~ formats = form.cleaned_data['formats']
150 #~ if len(formats) == 0:
151 #~ formats = models.Book.ebook_formats
153 #~ # Create a ZIP archive
154 #~ temp = tempfile.TemporaryFile()
155 #~ archive = zipfile.ZipFile(temp, 'w')
157 #~ for book in collect_books(models.Book.tagged.with_all(shelf)):
158 #~ for ebook_format in models.Book.ebook_formats:
159 #~ if ebook_format in formats and book.has_media(ebook_format):
160 #~ filename = book.get_media(ebook_format).path
161 #~ archive.write(filename, str('%s.%s' % (book.slug, ebook_format)))
164 #~ response = HttpResponse(content_type='application/zip', mimetype='application/x-zip-compressed')
165 #~ response['Content-Disposition'] = 'attachment; filename=%s.zip' % slughifi(shelf.name)
166 #~ response['Content-Length'] = temp.tell()
169 #~ response.write(temp.read())
173 #~ @cache.never_cache
174 #~ def shelf_book_formats(request, shelf):
176 #~ Returns a list of formats of books in shelf.
178 #~ shelf = get_object_or_404(models.Tag, slug=shelf, category='set')
181 #~ for ebook_format in models.Book.ebook_formats:
182 #~ formats[ebook_format] = False
184 #~ for book in collect_books(models.Book.tagged.with_all(shelf)):
185 #~ for ebook_format in models.Book.ebook_formats:
186 #~ if book.has_media(ebook_format):
187 #~ formats[ebook_format] = True
189 #~ return HttpResponse(LazyEncoder().encode(formats))
194 #~ @cache.never_cache
195 #~ def new_set(request):
196 #~ new_set_form = forms.NewSetForm(request.POST)
197 #~ if new_set_form.is_valid():
198 #~ new_set = new_set_form.save(request.user)
200 #~ if request.is_ajax():
201 #~ return JSONResponse('{"id":"%d", "name":"%s", "msg":"<p>Shelf <strong>%s</strong> was successfully created</p>"}' % (new_set.id, new_set.name, new_set))
203 #~ return HttpResponseRedirect('/')
205 #~ return HttpResponseRedirect('/')
210 #~ @cache.never_cache
211 #~ def delete_shelf(request, slug):
212 #~ user_set = get_object_or_404(models.Tag, slug=slug, category='set', user=request.user)
215 #~ if request.is_ajax():
216 #~ return HttpResponse(_('<p>Shelf <strong>%s</strong> was successfully removed</p>') % user_set.name)
218 #~ return HttpResponseRedirect('/')