from django.template.loader import render_to_string
from django.shortcuts import get_object_or_404, render, redirect
from django.http import HttpResponse, HttpResponseRedirect, Http404, HttpResponsePermanentRedirect
from django.urls import reverse
from django.db.models import Q, QuerySet
from django.contrib.auth.decorators import login_required, user_passes_test
from django.template.loader import render_to_string
from django.shortcuts import get_object_or_404, render, redirect
from django.http import HttpResponse, HttpResponseRedirect, Http404, HttpResponsePermanentRedirect
from django.urls import reverse
from django.db.models import Q, QuerySet
from django.contrib.auth.decorators import login_required, user_passes_test
-from club.models import Membership
+from club.forms import ScheduleForm, DonationStep1Form
+from club.models import Club
+from annoy.models import DynamicTextInsert
from pdcounter import views as pdcounter_views
from picture.models import Picture, PictureArea
from catalogue import constants
from pdcounter import views as pdcounter_views
from picture.models import Picture, PictureArea
from catalogue import constants
from catalogue.models import Book, Collection, Tag, Fragment
from catalogue.utils import split_tags
from catalogue.models.tag import prefetch_relations
from catalogue.models import Book, Collection, Tag, Fragment
from catalogue.utils import split_tags
from catalogue.models.tag import prefetch_relations
staff_required = user_passes_test(lambda user: user.is_staff)
def catalogue(request):
return render(request, 'catalogue/catalogue.html', {
staff_required = user_passes_test(lambda user: user.is_staff)
def catalogue(request):
return render(request, 'catalogue/catalogue.html', {
return book_list(request, Q(media__type='daisy'), template_name='catalogue/daisy_list.html')
def collection(request, slug):
coll = get_object_or_404(Collection, slug=slug)
return book_list(request, Q(media__type='daisy'), template_name='catalogue/daisy_list.html')
def collection(request, slug):
coll = get_object_or_404(Collection, slug=slug)
- return render(request, 'catalogue/collection.html', {'collection': coll})
+ if request.EXPERIMENTS['layout'].value:
+ template_name = 'catalogue/2022/collection.html'
+ else:
+ template_name = 'catalogue/collection.html'
+ return render(request, template_name, {
+ 'collection': coll,
+ 'active_menu_item': 'collections',
+ })
- 'catalogue/differentiate_tags.html', {'tags': tags, 'options': options, 'unparsed': ambiguous_slugs[1:]})
+ 'catalogue/differentiate_tags.html',
+ {'tags': tags, 'options': options, 'unparsed': ambiguous_slugs[1:]}
+ )
-def object_list(request, objects, fragments=None, related_tags=None, tags=None, list_type='books', extra=None):
+def object_list(request, objects, fragments=None, related_tags=None, tags=None,
+ list_type='books', extra=None):
- Tag.objects.usage_for_queryset(objects, counts=True).exclude(category='set').exclude(pk__in=tag_ids))
+ Tag.objects.usage_for_queryset(
+ objects, counts=True
+ ).exclude(category='set').exclude(pk__in=tag_ids))
+ related_tag_lists.append(
+ Tag.objects.usage_for_queryset(
+ objects, counts=True
+ ).filter(
+ user=request.user
+ ).exclude(name='').exclude(pk__in=tag_ids)
+ )
.only('name', 'sort_key', 'category', 'slug'))
if isinstance(objects, QuerySet):
objects = prefetch_relations(objects, 'author')
.only('name', 'sort_key', 'category', 'slug'))
if isinstance(objects, QuerySet):
objects = prefetch_relations(objects, 'author')
+ suggest = []
+ for c in ['set', 'author', 'epoch', 'kind', 'genre']:
+ suggest.extend(sorted(categories[c], key=lambda t: -t.count)[:3])
+
+ is_theme = len(tags) == 1 and tags[0].category == 'theme'
+ has_theme = any((x.category == 'theme' for x in tags))
+ new_layout = request.EXPERIMENTS['layout']
+
+ if is_theme and new_layout.value:
+ template = 'catalogue/2022/theme_detail.html'
+ elif new_layout.value and not has_theme:
+ template = 'catalogue/2022/author_detail.html'
+ else:
+ template = 'catalogue/tagged_object_list.html'
+
return object_list(request, audiobooks, list_type='audiobooks', extra={
return object_list(request, audiobooks, list_type='audiobooks', extra={
chunks = tag_str.split('/')
if len(chunks) == 2 and chunks[0] == 'autor':
raise ResponseInstead(pdcounter_views.author_detail(request, chunks[1]))
chunks = tag_str.split('/')
if len(chunks) == 2 and chunks[0] == 'autor':
raise ResponseInstead(pdcounter_views.author_detail(request, chunks[1]))
except Tag.MultipleObjectsReturned as e:
# Ask the user to disambiguate
raise ResponseInstead(differentiate_tags(request, e.tags, e.ambiguous_slugs))
except Tag.MultipleObjectsReturned as e:
# Ask the user to disambiguate
raise ResponseInstead(differentiate_tags(request, e.tags, e.ambiguous_slugs))
# TODO: Pictures on shelves not supported yet.
books = Book.tagged.with_all(shelf_tags).order_by()
fragments = fragments.filter(Q(book__in=books) | Q(book__ancestor__in=books))
# TODO: Pictures on shelves not supported yet.
books = Book.tagged.with_all(shelf_tags).order_by()
fragments = fragments.filter(Q(book__in=books) | Q(book__ancestor__in=books))
if not fragments and len(tags) == 1 and list_type == 'books':
if PictureArea.tagged.with_any(tags).exists() or Picture.tagged.with_any(tags).exists():
if not fragments and len(tags) == 1 and list_type == 'books':
if PictureArea.tagged.with_any(tags).exists() or Picture.tagged.with_any(tags).exists():
'fragments': Fragment.objects.filter(book__in=books),
'related_tags': get_top_level_related_tags(tags),
}
elif list_type == 'gallery':
params = {'objects': Picture.tagged.with_all(tags)}
elif list_type == 'audiobooks':
'fragments': Fragment.objects.filter(book__in=books),
'related_tags': get_top_level_related_tags(tags),
}
elif list_type == 'gallery':
params = {'objects': Picture.tagged.with_all(tags)}
elif list_type == 'audiobooks':
- 'daisy': Book.tagged.with_all(tags, audiobooks.filter(media__type='daisy').distinct()),
+ 'daisy': Book.tagged.with_all(
+ tags, audiobooks.filter(media__type='daisy').distinct()
+ ),
except Book.DoesNotExist:
return pdcounter_views.book_stub_detail(request, slug)
except Book.DoesNotExist:
return pdcounter_views.book_stub_detail(request, slug)
'book_children': book.children.all().order_by('parent_number', 'sort_key'),
'active_menu_item': 'books',
'book_children': book.children.all().order_by('parent_number', 'sort_key'),
'active_menu_item': 'books',
+ 'club_form': ScheduleForm() if book.preview else None,
+ 'club': Club.objects.first() if book.preview else None,
+ 'donation_form': DonationStep1Form(),
+
+ 'EXPERIMENTS_SWITCHABLE_layout': True,
def book_text(request, slug):
book = get_object_or_404(Book, slug=slug)
def book_text(request, slug):
book = get_object_or_404(Book, slug=slug)
- return render(request, 'catalogue/book_text.html', {'book': book})
+ with book.html_file.open('r') as f:
+ book_text = f.read()
+
+ return render(request, 'catalogue/book_text.html', {
+ 'book': book,
+ 'book_text': book_text,
+ 'inserts': DynamicTextInsert.get_all(request)
+ })
- _("An error occurred: %(exception)s\n\n%(tb)s") % {'exception': exception, 'tb': tb},
- mimetype='text/plain')
+ _("An error occurred: %(exception)s\n\n%(tb)s") % {
+ 'exception': exception, 'tb': tb
+ },
+ content_type='text/plain'
+ )
-def download_zip(request, format, slug=None):
- if format in Book.ebook_formats:
- url = Book.zip_format(format)
- elif format in ('mp3', 'ogg') and slug is not None:
+def download_zip(request, file_format=None, media_format=None, slug=None):
+ if file_format:
+ url = Book.zip_format(file_format)
+ elif media_format and slug is not None:
return HttpResponseRedirect(book.get_absolute_url())
return super(CustomPDFFormView, self).validate_object(obj, request)
return HttpResponseRedirect(book.get_absolute_url())
return super(CustomPDFFormView, self).validate_object(obj, request)
- return render(request, 'catalogue/tag_catalogue.html', {
+ if request.EXPERIMENTS['layout'].value:
+ template_name = 'catalogue/2022/tag_catalogue.html'
+ else:
+ template_name = 'catalogue/tag_catalogue.html'
+
+ return render(request, template_name, {
- return render(request, 'catalogue/collections.html', {
+ if request.EXPERIMENTS['layout'].value:
+ template_name = 'catalogue/2022/collections.html'
+ else:
+ template_name = 'catalogue/collections.html'
+
+ return render(request, template_name, {
def get_isbn(request, book_format, slug):
book = Book.objects.get(slug=slug)
def get_isbn(request, book_format, slug):
book = Book.objects.get(slug=slug)