1 from collections import defaultdict
2 from datetime import datetime, date, timedelta
5 from StringIO import StringIO
6 from urllib import unquote
7 from urlparse import urlsplit, urlunsplit
9 from django.conf import settings
10 from django.contrib import auth
11 from django.contrib.auth.models import User
12 from django.contrib.auth.decorators import login_required, permission_required
13 from django.core.urlresolvers import reverse
14 from django.db.models import Count, Q
15 from django.db import transaction
16 from django import http
17 from django.http import Http404, HttpResponse, HttpResponseForbidden
18 from django.shortcuts import get_object_or_404, render
19 from django.utils.encoding import iri_to_uri
20 from django.utils.http import urlquote_plus
21 from django.utils.translation import ugettext_lazy as _
22 from django.views.decorators.http import require_POST
24 from apiclient import NotAuthorizedError
25 from catalogue import forms
26 from catalogue import helpers
27 from catalogue.helpers import active_tab
28 from catalogue.models import (Book, Chunk, Image, BookPublishRecord,
29 ChunkPublishRecord, ImagePublishRecord, Project)
30 from fileupload.views import UploadView
33 # Quick hack around caching problems, TODO: use ETags
35 from django.views.decorators.cache import never_cache
37 logger = logging.getLogger("fnp.catalogue")
42 def document_list(request):
43 return render(request, 'catalogue/document_list.html')
48 def image_list(request, user=None):
49 return render(request, 'catalogue/image_list.html')
53 def user(request, username):
54 user = get_object_or_404(User, username=username)
55 return render(request, 'catalogue/user_page.html', {"viewed_user": user})
62 last_books = sorted(request.session.get("wiki_last_books", {}).items(),
63 key=lambda x: x[1]['time'], reverse=True)
64 for k, v in last_books:
65 v['time'] = datetime.fromtimestamp(v['time'])
66 return render(request, 'catalogue/my_page.html', {
67 'last_books': last_books,
74 return render(request, 'catalogue/user_list.html', {
75 'users': User.objects.all().annotate(count=Count('chunk')).order_by(
76 '-count', 'last_name', 'first_name'),
80 @active_tab('activity')
81 def activity(request, isodate=None):
84 day = helpers.parse_isodate(isodate)
91 next_day = day + timedelta(1)
92 prev_day = day - timedelta(1)
94 return render(request, 'catalogue/activity.html', locals())
98 def logout_then_redirect(request):
100 return http.HttpResponseRedirect(urlquote_plus(request.GET.get('next', '/'), safe='/?='))
103 @permission_required('catalogue.add_book')
104 @active_tab('create')
105 def create_missing(request, slug=None):
108 slug = slug.replace(' ', '-')
110 if request.method == "POST":
111 form = forms.DocumentCreateForm(request.POST, request.FILES)
114 if request.user.is_authenticated():
115 creator = request.user
119 text=form.cleaned_data['text'],
121 slug=form.cleaned_data['slug'],
122 title=form.cleaned_data['title'],
123 gallery=form.cleaned_data['gallery'],
126 return http.HttpResponseRedirect(reverse("catalogue_book", args=[book.slug]))
128 form = forms.DocumentCreateForm(initial={
130 "title": slug.replace('-', ' ').title(),
134 return render(request, "catalogue/document_create_missing.html", {
142 @permission_required('catalogue.add_book')
143 @active_tab('upload')
145 if request.method == "POST":
146 form = forms.DocumentsUploadForm(request.POST, request.FILES)
148 from slugify import slugify
150 if request.user.is_authenticated():
151 creator = request.user
155 zip = form.cleaned_data['zip']
160 existing = [book.slug for book in Book.objects.all()]
161 for filename in zip.namelist():
162 if filename[-1] == '/':
164 title = os.path.basename(filename)[:-4]
165 slug = slugify(title)
166 if not (slug and filename.endswith('.xml')):
167 skipped_list.append(filename)
169 error_list.append((filename, slug, _('Slug already used for %s' % slugs[slug])))
170 elif slug in existing:
171 error_list.append((filename, slug, _('Slug already used in repository.')))
174 zip.read(filename).decode('utf-8') # test read
175 ok_list.append((filename, slug, title))
176 except UnicodeDecodeError:
177 error_list.append((filename, title, _('File should be UTF-8 encoded.')))
178 slugs[slug] = filename
181 for filename, slug, title in ok_list:
183 text=zip.read(filename).decode('utf-8'),
189 return render(request, "catalogue/document_upload.html", {
192 "skipped_list": skipped_list,
193 "error_list": error_list,
198 form = forms.DocumentsUploadForm()
200 return render(request, "catalogue/document_upload.html", {
207 def serve_xml(request, book, slug):
208 xml = book.materialize(publishable=True)
209 response = http.HttpResponse(xml, content_type='application/xml')
210 response['Content-Disposition'] = 'attachment; filename=%s.xml' % slug
215 def book_xml(request, slug):
216 book = get_object_or_404(Book, slug=slug)
217 if not book.accessible(request):
218 return HttpResponseForbidden("Not authorized.")
219 return serve_xml(request, book, slug)
223 def book_xml_dc(request, slug):
224 # no permission check, because non-public books
225 book = get_object_or_404(Book, dc_slug=slug)
226 return serve_xml(request, book, slug)
230 def book_txt(request, slug):
231 book = get_object_or_404(Book, slug=slug)
232 if not book.accessible(request):
233 return HttpResponseForbidden("Not authorized.")
235 doc = book.wldocument()
236 text = doc.as_text().get_string()
237 response = http.HttpResponse(text, content_type='text/plain')
238 response['Content-Disposition'] = 'attachment; filename=%s.txt' % slug
243 def book_html(request, slug):
244 book = get_object_or_404(Book, slug=slug)
245 if not book.accessible(request):
246 return HttpResponseForbidden("Not authorized.")
248 doc = book.wldocument(parse_dublincore=False)
249 html = doc.as_html(options={'gallery': "'%s'" % book.gallery_url()})
251 html = html.get_string() if html is not None else ''
252 # response = http.HttpResponse(html, content_type='text/html')
255 # for fragment in book.fragments.all().iterator():
256 # for theme in fragment.tags.filter(category='theme').iterator():
257 # book_themes.setdefault(theme, []).append(fragment)
259 # book_themes = book_themes.items()
260 # book_themes.sort(key=lambda s: s[0].sort_key)
261 return render(request, 'catalogue/book_text.html', locals())
265 def book_pdf(request, slug):
266 book = get_object_or_404(Book, slug=slug)
267 if not book.accessible(request):
268 return HttpResponseForbidden("Not authorized.")
270 # TODO: move to celery
271 doc = book.wldocument()
272 # TODO: error handling
273 pdf_file = doc.as_pdf(cover=True, ilustr_path=book.gallery_path())
274 from catalogue.ebook_utils import serve_file
275 return serve_file(pdf_file.get_filename(),
276 book.slug + '.pdf', 'application/pdf')
280 def book_epub(request, slug):
281 book = get_object_or_404(Book, slug=slug)
282 if not book.accessible(request):
283 return HttpResponseForbidden("Not authorized.")
285 # TODO: move to celery
286 doc = book.wldocument()
287 # TODO: error handling
288 epub = doc.as_epub(ilustr_path=book.gallery_path()).get_string()
289 response = HttpResponse(content_type='application/epub+zip')
290 response['Content-Disposition'] = 'attachment; filename=%s' % book.slug + '.epub'
296 def book_mobi(request, slug):
297 book = get_object_or_404(Book, slug=slug)
298 if not book.accessible(request):
299 return HttpResponseForbidden("Not authorized.")
301 # TODO: move to celery
302 doc = book.wldocument()
303 # TODO: error handling
304 mobi = doc.as_mobi(ilustr_path=book.gallery_path()).get_string()
305 response = HttpResponse(content_type='application/x-mobipocket-ebook')
306 response['Content-Disposition'] = 'attachment; filename=%s' % book.slug + '.mobi'
312 def revision(request, slug, chunk=None):
314 doc = Chunk.get(slug, chunk)
315 except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
317 if not doc.book.accessible(request):
318 return HttpResponseForbidden("Not authorized.")
319 return http.HttpResponse(str(doc.revision()))
322 def book(request, slug):
323 book = get_object_or_404(Book, slug=slug)
324 if not book.accessible(request):
325 return HttpResponseForbidden("Not authorized.")
327 if request.user.has_perm('catalogue.change_book'):
328 if request.method == "POST":
329 form = forms.BookForm(request.POST, instance=book)
332 return http.HttpResponseRedirect(book.get_absolute_url())
334 form = forms.BookForm(instance=book)
337 form = forms.ReadonlyBookForm(instance=book)
340 publish_error = book.publishable_error()
341 publishable = publish_error is None
343 return render(request, "catalogue/book_detail.html", {
345 "publishable": publishable,
346 "publishable_error": publish_error,
348 "editable": editable,
352 def image(request, slug):
353 image = get_object_or_404(Image, slug=slug)
354 if not image.accessible(request):
355 return HttpResponseForbidden("Not authorized.")
357 if request.user.has_perm('catalogue.change_image'):
358 if request.method == "POST":
359 form = forms.ImageForm(request.POST, instance=image)
362 return http.HttpResponseRedirect(image.get_absolute_url())
364 form = forms.ImageForm(instance=image)
367 form = forms.ReadonlyImageForm(instance=image)
370 publish_error = image.publishable_error()
371 publishable = publish_error is None
373 return render(request, "catalogue/image_detail.html", {
375 "publishable": publishable,
376 "publishable_error": publish_error,
378 "editable": editable,
382 @permission_required('catalogue.add_chunk')
383 def chunk_add(request, slug, chunk):
385 doc = Chunk.get(slug, chunk)
386 except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
388 if not doc.book.accessible(request):
389 return HttpResponseForbidden("Not authorized.")
391 if request.method == "POST":
392 form = forms.ChunkAddForm(request.POST, instance=doc)
394 if request.user.is_authenticated():
395 creator = request.user
398 doc.split(creator=creator,
399 slug=form.cleaned_data['slug'],
400 title=form.cleaned_data['title'],
401 gallery_start=form.cleaned_data['gallery_start'],
402 user=form.cleaned_data['user'],
403 stage=form.cleaned_data['stage']
406 return http.HttpResponseRedirect(doc.book.get_absolute_url())
408 form = forms.ChunkAddForm(initial={
409 "slug": str(doc.number + 1),
410 "title": "cz. %d" % (doc.number + 1, ),
413 return render(request, "catalogue/chunk_add.html", {
420 def chunk_edit(request, slug, chunk):
422 doc = Chunk.get(slug, chunk)
423 except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
425 if not doc.book.accessible(request):
426 return HttpResponseForbidden("Not authorized.")
428 if request.method == "POST":
429 form = forms.ChunkForm(request.POST, instance=doc)
432 go_next = request.GET.get('next', None)
434 go_next = urlquote_plus(unquote(iri_to_uri(go_next)), safe='/?=&')
436 go_next = doc.book.get_absolute_url()
437 return http.HttpResponseRedirect(go_next)
439 form = forms.ChunkForm(instance=doc)
441 referer = request.META.get('HTTP_REFERER')
443 parts = urlsplit(referer)
444 parts = ['', ''] + list(parts[2:])
445 go_next = urlquote_plus(urlunsplit(parts))
449 return render(request, "catalogue/chunk_edit.html", {
459 def chunk_mass_edit(request):
460 ids = map(int, filter(lambda i: i.strip()!='', request.POST.get('ids').split(',')))
461 chunks = map(lambda i: Chunk.objects.get(id=i), ids)
463 stage = request.POST.get('stage')
466 stage = Chunk.tag_model.objects.get(slug=stage)
467 except Chunk.DoesNotExist, e:
470 for c in chunks: c.stage = stage
472 username = request.POST.get('user')
473 logger.info("username: %s" % username)
474 logger.info(request.POST)
477 user = User.objects.get(username=username)
478 except User.DoesNotExist, e:
481 for c in chunks: c.user = user
483 project_id = request.POST.get('project')
486 project = Project.objects.get(pk=int(project_id))
487 except (Project.DoesNotExist, ValueError), e:
491 book.project = project
494 for c in chunks: c.save()
496 return HttpResponse("", content_type="text/plain")
502 def image_mass_edit(request):
503 ids = map(int, filter(lambda i: i.strip()!='', request.POST.get('ids').split(',')))
504 images = map(lambda i: Image.objects.get(id=i), ids)
506 stage = request.POST.get('stage')
509 stage = Image.tag_model.objects.get(slug=stage)
510 except Image.DoesNotExist, e:
513 for c in images: c.stage = stage
515 username = request.POST.get('user')
516 logger.info("username: %s" % username)
517 logger.info(request.POST)
520 user = User.objects.get(username=username)
521 except User.DoesNotExist, e:
524 for c in images: c.user = user
526 project_id = request.POST.get('project')
529 project = Project.objects.get(pk=int(project_id))
530 except (Project.DoesNotExist, ValueError), e:
535 for c in images: c.save()
537 return HttpResponse("", content_type="text/plain")
540 @permission_required('catalogue.change_book')
541 def book_append(request, slug):
542 book = get_object_or_404(Book, slug=slug)
543 if not book.accessible(request):
544 return HttpResponseForbidden("Not authorized.")
546 if request.method == "POST":
547 form = forms.BookAppendForm(book, request.POST)
549 append_to = form.cleaned_data['append_to']
550 append_to.append(book)
551 return http.HttpResponseRedirect(append_to.get_absolute_url())
553 form = forms.BookAppendForm(book)
554 return render(request, "catalogue/book_append_to.html", {
564 def publish(request, slug):
565 book = get_object_or_404(Book, slug=slug)
566 if not book.accessible(request):
567 return HttpResponseForbidden("Not authorized.")
570 protocol = 'https://' if request.is_secure() else 'http://'
571 book.publish(request.user, host=protocol + request.get_host())
572 except NotAuthorizedError:
573 return http.HttpResponseRedirect(reverse('apiclient_oauth'))
574 except BaseException, e:
575 return http.HttpResponse(e)
577 return http.HttpResponseRedirect(book.get_absolute_url())
582 def publish_image(request, slug):
583 image = get_object_or_404(Image, slug=slug)
584 if not image.accessible(request):
585 return HttpResponseForbidden("Not authorized.")
588 image.publish(request.user)
589 except NotAuthorizedError:
590 return http.HttpResponseRedirect(reverse('apiclient_oauth'))
591 except BaseException, e:
592 return http.HttpResponse(e)
594 return http.HttpResponseRedirect(image.get_absolute_url())
597 class GalleryView(UploadView):
598 def get_object(self, request, slug):
599 book = get_object_or_404(Book, slug=slug)
604 def breadcrumbs(self):
606 (_('books'), reverse('catalogue_document_list')),
607 (self.object.title, self.object.get_absolute_url()),
608 (_('scan gallery'),),
611 def get_directory(self):
612 return "%s%s/" % (settings.IMAGE_DIR, self.object.gallery)
615 def active_users_list(request):
616 since = date(date.today().year, 1, 1)
617 by_user = defaultdict(lambda: 0)
618 by_email = defaultdict(lambda: 0)
619 names_by_email = defaultdict(set)
620 for change_model in (Chunk.change_model, Image.change_model):
621 for c in change_model.objects.filter(
622 created_at__gte=since).order_by(
623 'author', 'author_email', 'author_name').values(
624 'author', 'author_name', 'author_email').annotate(
625 c=Count('author'), ce=Count('author_email')).distinct():
627 by_user[c['author']] += c['c']
629 by_email[c['author_email']] += c['ce']
630 if c['author_name'].strip():
631 names_by_email[c['author_email']].add(c['author_name'])
632 for user in User.objects.filter(pk__in=by_user):
633 by_email[user.email] += by_user[user.pk]
634 names_by_email[user.email].add("%s %s" % (user.first_name, user.last_name))
637 for email, count in by_email.items():
638 active_users.append((email, names_by_email[email], count))
639 active_users.sort(key=lambda x: -x[2])
640 return render(request, 'catalogue/active_users_list.html', {
641 'users': active_users,