oops sorry; commited local mysqldb setup
[wolnelektury.git] / apps / social / views.py
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.
4 #
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 _
13
14 from ajaxable.utils import LazyEncoder, JSONResponse, AjaxableFormView
15
16 from catalogue.models import Book, Tag
17 from social import forms
18 from social.utils import get_set, likes, set_sets
19
20
21 # ====================
22 # = Shelf management =
23 # ====================
24
25
26 @require_POST
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])
34
35     if request.is_ajax():
36         return JSONResponse({"success": True, "msg": "ok", "like": True})
37     else:
38         return redirect(book)
39
40
41 @login_required
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())
45
46
47 class ObjectSetsFormView(AjaxableFormView):
48     form_class = forms.ObjectSetsForm
49     template = 'social/sets_form.html'
50     ajax_redirect = True
51     POST_login = True
52
53     def form_args(self, request, slug):
54         book = get_object_or_404(Book, slug=slug)
55         return (book, request.user), {}
56
57
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, [])
62
63     if request.is_ajax():
64         return JSONResponse({"success": True, "msg": "ok", "like": False})
65     else:
66         return redirect(book)
67
68
69 #~ @login_required
70 #~ @cache.never_cache
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))
76 #~ 
77 #~ @cache.never_cache
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>'))
81 #~ 
82     #~ book = get_object_or_404(models.Book, slug=slug)
83 #~ 
84     #~ user_sets = models.Tag.objects.filter(category='set', user=request.user)
85     #~ book_sets = book.tags.filter(category='set', user=request.user)
86 #~ 
87     #~ if request.method == 'POST':
88         #~ form = forms.ObjectSetsForm(book, request.user, request.POST)
89         #~ if form.is_valid():
90             #~ DONE!
91             #~ if request.is_ajax():
92                 #~ return JSONResponse('{"msg":"'+_("<p>Shelves were sucessfully saved.</p>")+'", "after":"close"}')
93             #~ else:
94                 #~ return HttpResponseRedirect('/')
95     #~ else:
96         #~ form = forms.ObjectSetsForm(book, request.user)
97         #~ new_set_form = forms.NewSetForm()
98 #~ 
99     #~ return render_to_response('social/book_sets.html', locals(),
100         #~ context_instance=RequestContext(request))
101 #~ 
102 #~ 
103 #~ @login_required
104 #~ @require_POST
105 #~ @cache.never_cache
106 #~ def remove_from_shelf(request, shelf, slug):
107     #~ book = get_object_or_404(models.Book, slug=slug)
108 #~ 
109     #~ shelf = get_object_or_404(models.Tag, slug=shelf, category='set', user=request.user)
110 #~ 
111     #~ if shelf in book.tags:
112         #~ models.Tag.objects.remove_tag(book, shelf)
113         #~ touch_tag(shelf)
114 #~ 
115         #~ return HttpResponse(_('Book was successfully removed from the shelf'))
116     #~ else:
117         #~ return HttpResponse(_('This book is not on the shelf'))
118 #~ 
119 #~ 
120 #~ def collect_books(books):
121     #~ """
122     #~ Returns all real books in collection.
123     #~ """
124     #~ result = []
125     #~ for book in books:
126         #~ if len(book.children.all()) == 0:
127             #~ result.append(book)
128         #~ else:
129             #~ result += collect_books(book.children.all())
130     #~ return result
131 #~ 
132 #~ 
133 #~ @cache.never_cache
134 #~ def download_shelf(request, slug):
135     #~ """"
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.
139     #~ """
140     #~ from slughifi import slughifi
141     #~ import tempfile
142     #~ import zipfile
143 #~ 
144     #~ shelf = get_object_or_404(models.Tag, slug=slug, category='set')
145 #~ 
146     #~ formats = []
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
152 #~ 
153     #~ # Create a ZIP archive
154     #~ temp = tempfile.TemporaryFile()
155     #~ archive = zipfile.ZipFile(temp, 'w')
156 #~ 
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)))
162     #~ archive.close()
163 #~ 
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()
167 #~ 
168     #~ temp.seek(0)
169     #~ response.write(temp.read())
170     #~ return response
171 #~ 
172 #~ 
173 #~ @cache.never_cache
174 #~ def shelf_book_formats(request, shelf):
175     #~ """"
176     #~ Returns a list of formats of books in shelf.
177     #~ """
178     #~ shelf = get_object_or_404(models.Tag, slug=shelf, category='set')
179 #~ 
180     #~ formats = {}
181     #~ for ebook_format in models.Book.ebook_formats:
182         #~ formats[ebook_format] = False
183 #~ 
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
188 #~ 
189     #~ return HttpResponse(LazyEncoder().encode(formats))
190 #~ 
191 #~ 
192 #~ @login_required
193 #~ @require_POST
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)
199 #~ 
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))
202         #~ else:
203             #~ return HttpResponseRedirect('/')
204 #~ 
205     #~ return HttpResponseRedirect('/')
206 #~ 
207 #~ 
208 #~ @login_required
209 #~ @require_POST
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)
213     #~ user_set.delete()
214 #~ 
215     #~ if request.is_ajax():
216         #~ return HttpResponse(_('<p>Shelf <strong>%s</strong> was successfully removed</p>') % user_set.name)
217     #~ else:
218         #~ return HttpResponseRedirect('/')