remove django_nose added by mistake
[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     placeholdize = True
50     template = 'social/sets_form.html'
51     ajax_redirect = True
52     POST_login = True
53
54     def get_object(self, request, slug):
55         return get_object_or_404(Book, slug=slug)
56
57     def context_description(self, request, obj):
58         return obj.pretty_title()
59
60     def form_args(self, request, obj):
61         return (obj, request.user), {}
62
63
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, [])
68
69     if request.is_ajax():
70         return JSONResponse({"success": True, "msg": "ok", "like": False})
71     else:
72         return redirect(book)
73
74
75 #~ @login_required
76 #~ @cache.never_cache
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))
82 #~ 
83 #~ @cache.never_cache
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>'))
87 #~ 
88     #~ book = get_object_or_404(models.Book, slug=slug)
89 #~ 
90     #~ user_sets = models.Tag.objects.filter(category='set', user=request.user)
91     #~ book_sets = book.tags.filter(category='set', user=request.user)
92 #~ 
93     #~ if request.method == 'POST':
94         #~ form = forms.ObjectSetsForm(book, request.user, request.POST)
95         #~ if form.is_valid():
96             #~ DONE!
97             #~ if request.is_ajax():
98                 #~ return JSONResponse('{"msg":"'+_("<p>Shelves were sucessfully saved.</p>")+'", "after":"close"}')
99             #~ else:
100                 #~ return HttpResponseRedirect('/')
101     #~ else:
102         #~ form = forms.ObjectSetsForm(book, request.user)
103         #~ new_set_form = forms.NewSetForm()
104 #~ 
105     #~ return render_to_response('social/book_sets.html', locals(),
106         #~ context_instance=RequestContext(request))
107 #~ 
108 #~ 
109 #~ @login_required
110 #~ @require_POST
111 #~ @cache.never_cache
112 #~ def remove_from_shelf(request, shelf, slug):
113     #~ book = get_object_or_404(models.Book, slug=slug)
114 #~ 
115     #~ shelf = get_object_or_404(models.Tag, slug=shelf, category='set', user=request.user)
116 #~ 
117     #~ if shelf in book.tags:
118         #~ models.Tag.objects.remove_tag(book, shelf)
119         #~ touch_tag(shelf)
120 #~ 
121         #~ return HttpResponse(_('Book was successfully removed from the shelf'))
122     #~ else:
123         #~ return HttpResponse(_('This book is not on the shelf'))
124 #~ 
125 #~ 
126 #~ def collect_books(books):
127     #~ """
128     #~ Returns all real books in collection.
129     #~ """
130     #~ result = []
131     #~ for book in books:
132         #~ if len(book.children.all()) == 0:
133             #~ result.append(book)
134         #~ else:
135             #~ result += collect_books(book.children.all())
136     #~ return result
137 #~ 
138 #~ 
139 #~ @cache.never_cache
140 #~ def download_shelf(request, slug):
141     #~ """"
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.
145     #~ """
146     #~ from slughifi import slughifi
147     #~ import tempfile
148     #~ import zipfile
149 #~ 
150     #~ shelf = get_object_or_404(models.Tag, slug=slug, category='set')
151 #~ 
152     #~ formats = []
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
158 #~ 
159     #~ # Create a ZIP archive
160     #~ temp = tempfile.TemporaryFile()
161     #~ archive = zipfile.ZipFile(temp, 'w')
162 #~ 
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)))
168     #~ archive.close()
169 #~ 
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()
173 #~ 
174     #~ temp.seek(0)
175     #~ response.write(temp.read())
176     #~ return response
177 #~ 
178 #~ 
179 #~ @cache.never_cache
180 #~ def shelf_book_formats(request, shelf):
181     #~ """"
182     #~ Returns a list of formats of books in shelf.
183     #~ """
184     #~ shelf = get_object_or_404(models.Tag, slug=shelf, category='set')
185 #~ 
186     #~ formats = {}
187     #~ for ebook_format in models.Book.ebook_formats:
188         #~ formats[ebook_format] = False
189 #~ 
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
194 #~ 
195     #~ return HttpResponse(LazyEncoder().encode(formats))
196 #~ 
197 #~ 
198 #~ @login_required
199 #~ @require_POST
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)
205 #~ 
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))
208         #~ else:
209             #~ return HttpResponseRedirect('/')
210 #~ 
211     #~ return HttpResponseRedirect('/')
212 #~ 
213 #~ 
214 #~ @login_required
215 #~ @require_POST
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)
219     #~ user_set.delete()
220 #~ 
221     #~ if request.is_ajax():
222         #~ return HttpResponse(_('<p>Shelf <strong>%s</strong> was successfully removed</p>') % user_set.name)
223     #~ else:
224         #~ return HttpResponseRedirect('/')