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 if not book.accessible(request):
209 return HttpResponseForbidden("Not authorized.")
210 xml = book.materialize(publishable=True)
211 response = http.HttpResponse(xml, content_type='application/xml')
212 response['Content-Disposition'] = 'attachment; filename=%s.xml' % slug
217 def book_xml(request, slug):
218 book = get_object_or_404(Book, slug=slug)
219 return serve_xml(request, book, slug)
223 def book_xml_dc(request, slug):
224 book = get_object_or_404(Book, dc_slug=slug)
225 return serve_xml(request, book, slug)
229 def book_txt(request, slug):
230 book = get_object_or_404(Book, slug=slug)
231 if not book.accessible(request):
232 return HttpResponseForbidden("Not authorized.")
234 doc = book.wldocument()
235 text = doc.as_text().get_string()
236 response = http.HttpResponse(text, content_type='text/plain')
237 response['Content-Disposition'] = 'attachment; filename=%s.txt' % slug
242 def book_html(request, slug):
243 book = get_object_or_404(Book, slug=slug)
244 if not book.accessible(request):
245 return HttpResponseForbidden("Not authorized.")
247 doc = book.wldocument(parse_dublincore=False)
248 html = doc.as_html(options={'gallery': "'%s'" % book.gallery_url()})
250 html = html.get_string() if html is not None else ''
251 # response = http.HttpResponse(html, content_type='text/html')
254 # for fragment in book.fragments.all().iterator():
255 # for theme in fragment.tags.filter(category='theme').iterator():
256 # book_themes.setdefault(theme, []).append(fragment)
258 # book_themes = book_themes.items()
259 # book_themes.sort(key=lambda s: s[0].sort_key)
260 return render(request, 'catalogue/book_text.html', locals())
264 def book_pdf(request, slug):
265 book = get_object_or_404(Book, slug=slug)
266 if not book.accessible(request):
267 return HttpResponseForbidden("Not authorized.")
269 # TODO: move to celery
270 doc = book.wldocument()
271 # TODO: error handling
272 pdf_file = doc.as_pdf(cover=True, ilustr_path=book.gallery_path())
273 from catalogue.ebook_utils import serve_file
274 return serve_file(pdf_file.get_filename(),
275 book.slug + '.pdf', 'application/pdf')
279 def book_epub(request, slug):
280 book = get_object_or_404(Book, slug=slug)
281 if not book.accessible(request):
282 return HttpResponseForbidden("Not authorized.")
284 # TODO: move to celery
285 doc = book.wldocument()
286 # TODO: error handling
287 epub = doc.as_epub(ilustr_path=book.gallery_path()).get_string()
288 response = HttpResponse(content_type='application/epub+zip')
289 response['Content-Disposition'] = 'attachment; filename=%s' % book.slug + '.epub'
295 def book_mobi(request, slug):
296 book = get_object_or_404(Book, slug=slug)
297 if not book.accessible(request):
298 return HttpResponseForbidden("Not authorized.")
300 # TODO: move to celery
301 doc = book.wldocument()
302 # TODO: error handling
303 mobi = doc.as_mobi(ilustr_path=book.gallery_path()).get_string()
304 response = HttpResponse(content_type='application/x-mobipocket-ebook')
305 response['Content-Disposition'] = 'attachment; filename=%s' % book.slug + '.mobi'
311 def revision(request, slug, chunk=None):
313 doc = Chunk.get(slug, chunk)
314 except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
316 if not doc.book.accessible(request):
317 return HttpResponseForbidden("Not authorized.")
318 return http.HttpResponse(str(doc.revision()))
321 def book(request, slug):
322 book = get_object_or_404(Book, slug=slug)
323 if not book.accessible(request):
324 return HttpResponseForbidden("Not authorized.")
326 if request.user.has_perm('catalogue.change_book'):
327 if request.method == "POST":
328 form = forms.BookForm(request.POST, instance=book)
331 return http.HttpResponseRedirect(book.get_absolute_url())
333 form = forms.BookForm(instance=book)
336 form = forms.ReadonlyBookForm(instance=book)
339 publish_error = book.publishable_error()
340 publishable = publish_error is None
342 return render(request, "catalogue/book_detail.html", {
344 "publishable": publishable,
345 "publishable_error": publish_error,
347 "editable": editable,
351 def image(request, slug):
352 image = get_object_or_404(Image, slug=slug)
353 if not image.accessible(request):
354 return HttpResponseForbidden("Not authorized.")
356 if request.user.has_perm('catalogue.change_image'):
357 if request.method == "POST":
358 form = forms.ImageForm(request.POST, instance=image)
361 return http.HttpResponseRedirect(image.get_absolute_url())
363 form = forms.ImageForm(instance=image)
366 form = forms.ReadonlyImageForm(instance=image)
369 publish_error = image.publishable_error()
370 publishable = publish_error is None
372 return render(request, "catalogue/image_detail.html", {
374 "publishable": publishable,
375 "publishable_error": publish_error,
377 "editable": editable,
381 @permission_required('catalogue.add_chunk')
382 def chunk_add(request, slug, chunk):
384 doc = Chunk.get(slug, chunk)
385 except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
387 if not doc.book.accessible(request):
388 return HttpResponseForbidden("Not authorized.")
390 if request.method == "POST":
391 form = forms.ChunkAddForm(request.POST, instance=doc)
393 if request.user.is_authenticated():
394 creator = request.user
397 doc.split(creator=creator,
398 slug=form.cleaned_data['slug'],
399 title=form.cleaned_data['title'],
400 gallery_start=form.cleaned_data['gallery_start'],
401 user=form.cleaned_data['user'],
402 stage=form.cleaned_data['stage']
405 return http.HttpResponseRedirect(doc.book.get_absolute_url())
407 form = forms.ChunkAddForm(initial={
408 "slug": str(doc.number + 1),
409 "title": "cz. %d" % (doc.number + 1, ),
412 return render(request, "catalogue/chunk_add.html", {
419 def chunk_edit(request, slug, chunk):
421 doc = Chunk.get(slug, chunk)
422 except (Chunk.MultipleObjectsReturned, Chunk.DoesNotExist):
424 if not doc.book.accessible(request):
425 return HttpResponseForbidden("Not authorized.")
427 if request.method == "POST":
428 form = forms.ChunkForm(request.POST, instance=doc)
431 go_next = request.GET.get('next', None)
433 go_next = urlquote_plus(unquote(iri_to_uri(go_next)), safe='/?=&')
435 go_next = doc.book.get_absolute_url()
436 return http.HttpResponseRedirect(go_next)
438 form = forms.ChunkForm(instance=doc)
440 referer = request.META.get('HTTP_REFERER')
442 parts = urlsplit(referer)
443 parts = ['', ''] + list(parts[2:])
444 go_next = urlquote_plus(urlunsplit(parts))
448 return render(request, "catalogue/chunk_edit.html", {
458 def chunk_mass_edit(request):
459 ids = map(int, filter(lambda i: i.strip()!='', request.POST.get('ids').split(',')))
460 chunks = map(lambda i: Chunk.objects.get(id=i), ids)
462 stage = request.POST.get('stage')
465 stage = Chunk.tag_model.objects.get(slug=stage)
466 except Chunk.DoesNotExist, e:
469 for c in chunks: c.stage = stage
471 username = request.POST.get('user')
472 logger.info("username: %s" % username)
473 logger.info(request.POST)
476 user = User.objects.get(username=username)
477 except User.DoesNotExist, e:
480 for c in chunks: c.user = user
482 project_id = request.POST.get('project')
485 project = Project.objects.get(pk=int(project_id))
486 except (Project.DoesNotExist, ValueError), e:
490 book.project = project
493 for c in chunks: c.save()
495 return HttpResponse("", content_type="text/plain")
501 def image_mass_edit(request):
502 ids = map(int, filter(lambda i: i.strip()!='', request.POST.get('ids').split(',')))
503 images = map(lambda i: Image.objects.get(id=i), ids)
505 stage = request.POST.get('stage')
508 stage = Image.tag_model.objects.get(slug=stage)
509 except Image.DoesNotExist, e:
512 for c in images: c.stage = stage
514 username = request.POST.get('user')
515 logger.info("username: %s" % username)
516 logger.info(request.POST)
519 user = User.objects.get(username=username)
520 except User.DoesNotExist, e:
523 for c in images: c.user = user
525 project_id = request.POST.get('project')
528 project = Project.objects.get(pk=int(project_id))
529 except (Project.DoesNotExist, ValueError), e:
534 for c in images: c.save()
536 return HttpResponse("", content_type="text/plain")
539 @permission_required('catalogue.change_book')
540 def book_append(request, slug):
541 book = get_object_or_404(Book, slug=slug)
542 if not book.accessible(request):
543 return HttpResponseForbidden("Not authorized.")
545 if request.method == "POST":
546 form = forms.BookAppendForm(book, request.POST)
548 append_to = form.cleaned_data['append_to']
549 append_to.append(book)
550 return http.HttpResponseRedirect(append_to.get_absolute_url())
552 form = forms.BookAppendForm(book)
553 return render(request, "catalogue/book_append_to.html", {
563 def publish(request, slug):
564 book = get_object_or_404(Book, slug=slug)
565 if not book.accessible(request):
566 return HttpResponseForbidden("Not authorized.")
569 protocol = 'https://' if request.is_secure() else 'http://'
570 book.publish(request.user, host=protocol + request.get_host())
571 except NotAuthorizedError:
572 return http.HttpResponseRedirect(reverse('apiclient_oauth'))
573 except BaseException, e:
574 return http.HttpResponse(repr(e))
576 return http.HttpResponseRedirect(book.get_absolute_url())
581 def publish_image(request, slug):
582 image = get_object_or_404(Image, slug=slug)
583 if not image.accessible(request):
584 return HttpResponseForbidden("Not authorized.")
587 image.publish(request.user)
588 except NotAuthorizedError:
589 return http.HttpResponseRedirect(reverse('apiclient_oauth'))
590 except BaseException, e:
591 return http.HttpResponse(e)
593 return http.HttpResponseRedirect(image.get_absolute_url())
596 class GalleryView(UploadView):
597 def get_object(self, request, slug):
598 book = get_object_or_404(Book, slug=slug)
603 def breadcrumbs(self):
605 (_('books'), reverse('catalogue_document_list')),
606 (self.object.title, self.object.get_absolute_url()),
607 (_('scan gallery'),),
610 def get_directory(self):
611 return "%s%s/" % (settings.IMAGE_DIR, self.object.gallery)
614 def active_users_list(request):
615 since = date(date.today().year, 1, 1)
616 by_user = defaultdict(lambda: 0)
617 by_email = defaultdict(lambda: 0)
618 names_by_email = defaultdict(set)
619 for change_model in (Chunk.change_model, Image.change_model):
620 for c in change_model.objects.filter(
621 created_at__gte=since).order_by(
622 'author', 'author_email', 'author_name').values(
623 'author', 'author_name', 'author_email').annotate(
624 c=Count('author'), ce=Count('author_email')).distinct():
626 by_user[c['author']] += c['c']
628 by_email[c['author_email']] += c['ce']
629 if c['author_name'].strip():
630 names_by_email[c['author_email']].add(c['author_name'])
631 for user in User.objects.filter(pk__in=by_user):
632 by_email[user.email] += by_user[user.pk]
633 names_by_email[user.email].add("%s %s" % (user.first_name, user.last_name))
636 for email, count in by_email.items():
637 active_users.append((email, names_by_email[email], count))
638 active_users.sort(key=lambda x: -x[2])
639 return render(request, 'catalogue/active_users_list.html', {
640 'users': active_users,