def catalogue(request, as_json=False): common_categories = ('author',) split_categories = ('epoch', 'genre', 'kind') categories = split_tags( get_top_level_related_tags(categories=common_categories), models.Tag.objects.usage_for_model( models.Fragment, counts=True).filter(category='theme'), models.Tag.objects.usage_for_model( Picture, counts=True).filter(category__in=common_categories), models.Tag.objects.usage_for_model( PictureArea, counts=True).filter( category='theme') ) book_categories = split_tags( get_top_level_related_tags(categories=split_categories) ) picture_categories = split_tags( models.Tag.objects.usage_for_model( Picture, counts=True).filter( category__in=split_categories), ) collections = models.Collection.objects.all() def render_tag_list(tags): return render_to_string('catalogue/tag_list.html', tag_list(tags)) def render_split(with_books, with_pictures): ctx = {} if with_books: ctx['books'] = render_tag_list(with_books) if with_pictures: ctx['pictures'] = render_tag_list(with_pictures) return render_to_string('catalogue/tag_list_split.html', ctx) output = {} output['theme'] = render_tag_list(categories.get('theme', [])) for category in common_categories: output[category] = render_tag_list(categories.get(category, [])) for category in split_categories: output[category] = render_split( book_categories.get(category, []), picture_categories.get(category, [])) output['collections'] = render_to_string( 'catalogue/collection_list.html', collection_list(collections)) if as_json: return JsonResponse(output) else: return render_to_response('catalogue/catalogue.html', locals(), context_instance=RequestContext(request))
def related_info(self): """Keeps info about related objects (tags, media) in cache field.""" if self._related_info is not None: return self._related_info else: rel = {'tags': {}, 'media': {}} tags = self.tags.filter(category__in=( 'author', 'kind', 'genre', 'epoch')) tags = split_tags(tags) for category in tags: rel['tags'][category] = [ (t.name, t.slug) for t in tags[category]] for media_format in BookMedia.formats: rel['media'][media_format] = self.has_media(media_format) book = self parents = [] while book.parent: parents.append((book.parent.title, book.parent.slug)) book = book.parent parents = parents[::-1] if parents: rel['parents'] = parents if self.pk: type(self).objects.filter(pk=self.pk).update(_related_info=rel) return rel
def book_detail(request, slug): try: book = models.Book.objects.get(slug=slug) except models.Book.DoesNotExist: return pdcounter_views.book_stub_detail(request, slug) book_tag = book.book_tag() tags = list(book.tags.filter(~Q(category='set'))) categories = split_tags(tags) book_children = book.children.all().order_by('parent_number', 'title') _book = book parents = [] while _book.parent: parents.append(_book.parent) _book = _book.parent parents = reversed(parents) theme_counter = book.theme_counter book_themes = models.Tag.objects.filter(pk__in=theme_counter.keys()) for tag in book_themes: tag.count = theme_counter[tag.pk] extra_info = book.get_extra_info_value() form = forms.SearchForm() return render_to_response('catalogue/book_detail.html', locals(), context_instance=RequestContext(request))
def book_wide(request, pk): book = get_object_or_404(Book, pk=pk) stage_note, stage_note_url = book.stage_note() extra_info = book.extra_info audiobooks, projects, have_oggs = get_audiobooks(book) return render( request, 'catalogue/book_wide.html', { 'book': book, 'has_audio': book.has_media('mp3'), 'parents': book.parents(), 'tags': split_tags(book.tags.exclude(category__in=('set', 'theme'))), 'show_lang': book.language_code() != settings.LANGUAGE_CODE, 'stage_note': stage_note, 'stage_note_url': stage_note_url, 'main_link': reverse('book_text', args=[book.slug]) if book.html_file else None, 'extra_info': extra_info, 'hide_about': extra_info.get('about', '').startswith('http://wiki.wolnepodreczniki.pl'), 'audiobooks': audiobooks, 'have_oggs': have_oggs, })
def catalogue(request): cache_key='catalogue.catalogue' output = permanent_cache.get(cache_key) if output is None: tags = models.Tag.objects.exclude( category__in=('set', 'book')).exclude(book_count=0) tags = list(tags) for tag in tags: tag.count = tag.book_count categories = split_tags(tags) fragment_tags = categories.get('theme', []) collections = models.Collection.objects.all() render_tag_list = lambda x: render_to_string( 'catalogue/tag_list.html', tag_list(x)) output = {'theme': render_tag_list(fragment_tags)} for category, tags in categories.items(): output[category] = render_tag_list(tags) output['collections'] = render_to_string( 'catalogue/collection_list.html', collection_list(collections)) permanent_cache.set(cache_key, output) if request.is_ajax(): return JSONResponse(output) else: return render_to_response('catalogue/catalogue.html', locals(), context_instance=RequestContext(request))
def picture_wide(context, picture): context.update({ 'picture': picture, 'main_link': reverse('picture_viewer', args=[picture.slug]), 'request': context.get('request'), 'tags': split_tags(picture.tags), }) return context
def picture_wide(context, picture): context.update({ 'picture': picture, 'main_link': picture.get_absolute_url(), 'request': context.get('request'), 'tags': split_tags(picture.tags), }) return context
def picture_short(request, pk): picture = get_object_or_404(Picture, pk=pk) return render(request, 'picture/picture_short.html', { 'picture': picture, 'main_link': picture.get_absolute_url(), 'request': request, 'tags': split_tags(picture.tags), })
def picture_short(request, pk): picture = get_object_or_404(Picture, pk=pk) return render( request, 'picture/picture_short.html', { 'picture': picture, 'main_link': picture.get_absolute_url(), 'request': request, 'tags': split_tags(picture.tags), })
def picture_detail(request, slug): picture = get_object_or_404(Picture, slug=slug) theme_things = split_tags(picture.related_themes()) return render(request, "picture/picture_detail.html", { 'picture': picture, 'themes': theme_things.get('theme', []), 'things': theme_things.get('thing', []), 'active_menu_item': 'gallery', })
def catalogue(request): tags = models.Tag.objects.exclude( category__in=('set', 'book')).exclude(book_count=0) tags = list(tags) for tag in tags: tag.count = tag.book_count categories = split_tags(tags) fragment_tags = categories.get('theme', []) return render_to_response('catalogue/catalogue.html', locals(), context_instance=RequestContext(request))
def object_list(request, objects, fragments=None, related_tags=None, tags=None, list_type='books', extra=None): if not tags: tags = [] tag_ids = [tag.pk for tag in tags] related_tag_lists = [] if related_tags: related_tag_lists.append(related_tags) else: related_tag_lists.append( Tag.objects.usage_for_queryset(objects, counts=True).exclude(category='set').exclude(pk__in=tag_ids)) if not (extra and extra.get('theme_is_set')): if fragments is None: if list_type == 'gallery': fragments = PictureArea.objects.filter(picture__in=objects) else: fragments = Fragment.objects.filter(book__in=objects) related_tag_lists.append( Tag.objects.usage_for_queryset(fragments, counts=True).filter(category='theme').exclude(pk__in=tag_ids) .only('name', 'sort_key', 'category', 'slug')) if isinstance(objects, QuerySet): objects = prefetch_relations(objects, 'author') categories = split_tags(*related_tag_lists) objects = list(objects) if not objects and len(tags) == 1 and list_type == 'books': if PictureArea.tagged.with_any(tags).exists() or Picture.tagged.with_any(tags).exists(): return redirect('tagged_object_list_gallery', '/'.join(tag.url_chunk for tag in tags)) if len(objects) > 3: best = random.sample(objects, 3) else: best = objects result = { 'object_list': objects, 'categories': categories, 'list_type': list_type, 'tags': tags, 'formats_form': forms.DownloadFormatsForm(), 'best': best, 'active_menu_item': list_type, } if extra: result.update(extra) return render( request, 'catalogue/tagged_object_list.html', result, )
def main_page(request): if request.user.is_authenticated(): shelves = models.Tag.objects.filter(category='set', user=request.user) new_set_form = forms.NewSetForm() tags = models.Tag.objects.exclude(category__in=('set', 'book')) for tag in tags: tag.count = tag.get_count() categories = split_tags(tags) fragment_tags = categories.get('theme', []) form = forms.SearchForm() return render_to_response('catalogue/main_page.html', locals(), context_instance=RequestContext(request))
def book_short(request, pk): book = get_object_or_404(models.Book, pk=pk) stage_note, stage_note_url = book.stage_note() return render(request, 'catalogue/book_short.html', { 'book': book, 'has_audio': book.has_media('mp3'), 'main_link': book.get_absolute_url(), 'parents': book.parents(), 'tags': split_tags(book.tags.exclude(category__in=('set', 'theme'))), 'show_lang': book.language_code() != settings.LANGUAGE_CODE, 'stage_note': stage_note, 'stage_note_url': stage_note_url, })
def picture_detail(request, slug): picture = get_object_or_404(Picture, slug=slug) theme_things = split_tags(picture.related_themes()) # categories = SortedDict() # for tag in picture.tags.iterator(): # categories.setdefault(tag.category, []).append(tag) return render_to_response("picture/picture_detail.html", { 'picture': picture, 'themes': theme_things.get('theme', []), 'things': theme_things.get('thing', []), 'active_menu_item': 'gallery', }, context_instance=RequestContext(request))
def picture_detail(request, slug): picture = get_object_or_404(Picture, slug=slug) theme_things = split_tags(picture.related_themes()) # categories = SortedDict() # for tag in picture.tags.iterator(): # categories.setdefault(tag.category, []).append(tag) themes = theme_things.get('theme', []) things = theme_things.get('thing', []) extra_info = picture.extra_info return render_to_response("picture/picture_detail.html", locals(), context_instance=RequestContext(request))
def short_html(self): if self.id: cache_key = "Picture.short_html/%d" % (self.id) short_html = get_cache("permanent").get(cache_key) else: short_html = None if short_html is not None: return mark_safe(short_html) else: tags = self.tags.filter(category__in=("author", "kind", "epoch")) tags = split_tags(tags) short_html = unicode(render_to_string("picture/picture_short.html", {"picture": self, "tags": tags})) if self.id: get_cache("permanent").set(cache_key, short_html) return mark_safe(short_html)
def short_html(self): if self.id: cache_key = "Picture.short_html/%d" % (self.id) short_html = get_cache('permanent').get(cache_key) else: short_html = None if short_html is not None: return mark_safe(short_html) else: tags = self.tags.filter(category__in=('author', 'kind', 'epoch')) tags = split_tags(tags) short_html = unicode(render_to_string('picture/picture_short.html', {'picture': self, 'tags': tags})) if self.id: get_cache('permanent').set(cache_key, short_html) return mark_safe(short_html)
def book_wide(request, pk): book = get_object_or_404(models.Book, pk=pk) stage_note, stage_note_url = book.stage_note() extra_info = book.extra_info return render(request, 'catalogue/book_wide.html', { 'book': book, 'has_audio': book.has_media('mp3'), 'parents': book.parents(), 'tags': split_tags(book.tags.exclude(category__in=('set', 'theme'))), 'show_lang': book.language_code() != settings.LANGUAGE_CODE, 'stage_note': stage_note, 'stage_note_url': stage_note_url, 'main_link': reverse('book_text', args=[book.slug]) if book.html_file else None, 'extra_info': extra_info, 'hide_about': extra_info.get('about', '').startswith('http://wiki.wolnepodreczniki.pl'), 'themes': book.related_themes(), })
def book_short(request, pk): book = get_object_or_404(Book, pk=pk) stage_note, stage_note_url = book.stage_note() audiobooks, projects, have_oggs = get_audiobooks(book) return render( request, 'catalogue/book_short.html', { 'book': book, 'has_audio': book.has_media('mp3'), 'main_link': book.get_absolute_url(), 'parents': book.parents(), 'tags': split_tags( book.tags.exclude(category__in=('set', 'theme'))), 'show_lang': book.language_code() != settings.LANGUAGE_CODE, 'stage_note': stage_note, 'stage_note_url': stage_note_url, 'audiobooks': audiobooks, 'have_oggs': have_oggs, })
def short_html(self): if self.id: cache_key = "Picture.short_html/%d" % (self.id) short_html = get_cache('permanent').get(cache_key) else: short_html = None if short_html is not None: return mark_safe(short_html) else: tags = self.tags.filter(category__in=('author', 'kind', 'epoch')) tags = split_tags(tags) short_html = unicode( render_to_string('picture/picture_short.html', { 'picture': self, 'tags': tags })) if self.id: get_cache('permanent').set(cache_key, short_html) return mark_safe(short_html)
objects = None if theme_is_set: shelf_tags = [tag for tag in tags if tag.category == 'set'] fragment_tags = [tag for tag in tags if tag.category != 'set'] fragments = models.Fragment.tagged.with_all(fragment_tags) areas = PictureArea.tagged.with_all(fragment_tags) if shelf_tags: books = models.Book.tagged.with_all(shelf_tags).order_by() fragments = fragments.filter(Q(book__in=books) | Q(book__ancestor__in=books)) areas = PictureArea.objects.none() categories = split_tags( models.Tag.objects.usage_for_queryset(fragments, counts=True ).exclude(pk__in=tags_pks), models.Tag.objects.usage_for_queryset(areas, counts=True ).exclude(pk__in=tags_pks) ) # we want the Pictures to go first objects = MultiQuerySet(areas, fragments) else: all_books = models.Book.tagged.with_all(tags) if shelf_is_set: books = all_books.order_by('sort_key_author', 'title') pictures = Picture.objects.none() related_book_tags = models.Tag.objects.usage_for_queryset( books, counts=True).exclude( category='set').exclude(pk__in=tags_pks) else: books = models.Book.tagged_top_level(tags).order_by(
def tags_by_category(self): return split_tags(self.tags.exclude(category__in=('set', 'theme')))
def title_from_tags(tags): def split_tags(tags): result = {} for tag in tags: result[tag.category] = tag return result # TODO: Remove this after adding flection mechanism return simple_title(tags) class Flection(object): def get_case(self, name, flection): return name flection = Flection() self = split_tags(tags) title = u'' # Specjalny przypadek oglądania wszystkich lektur na danej półce if len(self) == 1 and 'set' in self: return u'Półka %s' % self['set'] # Specjalny przypadek "Twórczość w pozytywizmie", wtedy gdy tylko epoka # jest wybrana przez użytkownika if 'epoch' in self and len(self) == 1: text = u'Twórczość w %s' % flection.get_case(unicode(self['epoch']), u'miejscownik') return capfirst(text) # Specjalny przypadek "Dramat w twórczości Sofoklesa", wtedy gdy podane # są tylko rodzaj literacki i autor if 'kind' in self and 'author' in self and len(self) == 2: text = u'%s w twórczości %s' % (unicode(self['kind']), flection.get_case(unicode(self['author']), u'dopełniacz')) return capfirst(text) # Przypadki ogólniejsze if 'theme' in self: title += u'Motyw %s' % unicode(self['theme']) if 'genre' in self: if 'theme' in self: title += u' w %s' % flection.get_case(unicode(self['genre']), u'miejscownik') else: title += unicode(self['genre']) if 'kind' in self or 'author' in self or 'epoch' in self: if 'genre' in self or 'theme' in self: if 'kind' in self: title += u' w %s ' % flection.get_case(unicode(self['kind']), u'miejscownik') else: title += u' w twórczości ' else: title += u'%s ' % unicode(self.get('kind', u'twórczość')) if 'author' in self: title += flection.get_case(unicode(self['author']), u'dopełniacz') elif 'epoch' in self: title += flection.get_case(unicode(self['epoch']), u'dopełniacz') return capfirst(title)
def catalogue_menu(): tags = Tag.objects.filter( category__in=('author', 'epoch', 'genre', 'kind', 'theme') ).exclude(book_count=0) return split_tags(tags)
def tags_by_category(self): return split_tags(self.tags)
if shelf_tags: books = models.Book.tagged.with_all(shelf_tags).order_by() l_tags = models.Tag.objects.filter(category='book', slug__in=[book.book_tag_slug() for book in books]) fragments = models.Fragment.tagged.with_any(l_tags, fragments) # newtagging goes crazy if we just try: #related_tags = models.Tag.objects.usage_for_queryset(fragments, counts=True, # extra={'where': ["catalogue_tag.category != 'book'"]}) fragment_keys = [fragment.pk for fragment in fragments] if fragment_keys: related_tags = models.Fragment.tags.usage(counts=True, filters={'pk__in': fragment_keys}, extra={'where': ["catalogue_tag.category != 'book'"]}) related_tags = (tag for tag in related_tags if tag not in fragment_tags) categories = split_tags(related_tags) objects = fragments else: # get relevant books and their tags objects = models.Book.tagged.with_all(tags) if not shelf_is_set: # eliminate descendants l_tags = models.Tag.objects.filter(category='book', slug__in=[book.book_tag_slug() for book in objects]) descendants_keys = [book.pk for book in models.Book.tagged.with_any(l_tags)] if descendants_keys: objects = objects.exclude(pk__in=descendants_keys) # get related tags from `tag_counter` and `theme_counter` related_counts = {} tags_pks = [tag.pk for tag in tags]
def main(request): results = {} results = None query = None query = request.GET.get('q', '') if len(query) < 2: return render_to_response('catalogue/search_too_short.html', {'prefix': query}, context_instance=RequestContext(request)) query = remove_query_syntax_chars(query) search = Search() theme_terms = search.index.analyze(text=query, field="themes_pl") \ + search.index.analyze(text=query, field="themes") # change hints tags = search.hint_tags(query, pdcounter=True, prefix=False) tags = split_tags(tags) author_results = search.search_phrase(query, 'authors', book=True) translator_results = search.search_phrase(query, 'translators', book=True) title_results = search.search_phrase(query, 'title', book=True) # Boost main author/title results with mixed search, and save some of its results for end of list. # boost author, title results author_title_mixed = search.search_some( query, ['authors', 'translators', 'title', 'tags'], query_terms=theme_terms) author_title_rest = [] for b in author_title_mixed: also_in_mixed = filter( lambda ba: ba.book_id == b.book_id, author_results + translator_results + title_results) for b2 in also_in_mixed: b2.boost *= 1.1 if also_in_mixed is []: author_title_rest.append(b) # Do a phrase search but a term search as well - this can give us better snippets then search_everywhere, # Because the query is using only one field. text_phrase = SearchResult.aggregate( search.search_phrase(query, 'text', snippets=True, book=False), search.search_some(query, ['text'], snippets=True, book=False, query_terms=theme_terms)) everywhere = search.search_everywhere(query, query_terms=theme_terms) def already_found(results): def f(e): for r in results: if e.book_id == r.book_id: e.boost = 0.9 results.append(e) return True return False return f f = already_found(author_results + translator_results + title_results + text_phrase) everywhere = filter(lambda x: not f(x), everywhere) author_results = SearchResult.aggregate(author_results) translator_results = SearchResult.aggregate(translator_results) title_results = SearchResult.aggregate(title_results) everywhere = SearchResult.aggregate(everywhere, author_title_rest) for field, res in [('authors', author_results), ('translators', translator_results), ('title', title_results), ('text', text_phrase), ('text', everywhere)]: res.sort(reverse=True) for r in res: search.get_snippets(r, query, field, 3) suggestion = u'' def ensure_exists(r): try: return r.book except Book.DoesNotExist: return False author_results = filter(ensure_exists, author_results) translator_results = filter(ensure_exists, translator_results) title_results = filter(ensure_exists, title_results) text_phrase = filter(ensure_exists, text_phrase) everywhere = filter(ensure_exists, everywhere) results = author_results + translator_results + title_results + text_phrase + everywhere # ensure books do exists & sort them for res in (author_results, translator_results, title_results, text_phrase, everywhere): res.sort(reverse=True) # We don't want to redirect to book text, but rather display result page even with one result. # if len(results) == 1: # fragment_hits = filter(lambda h: 'fragment' in h, results[0].hits) # if len(fragment_hits) == 1: # #anchor = fragment_hits[0]['fragment'] # #frag = Fragment.objects.get(anchor=anchor) # return HttpResponseRedirect(fragment_hits[0]['fragment'].get_absolute_url()) # return HttpResponseRedirect(results[0].book.get_absolute_url()) if len(results) == 0: form = PublishingSuggestForm(initial={"books": query + ", "}) return render_to_response('catalogue/search_no_hits.html', { 'tags': tags, 'prefix': query, "form": form, 'did_you_mean': suggestion }, context_instance=RequestContext(request)) return render_to_response('catalogue/search_multiple_hits.html', { 'tags': tags, 'prefix': query, 'results': { 'author': author_results, 'translator': translator_results, 'title': title_results, 'content': text_phrase, 'other': everywhere }, 'did_you_mean': suggestion }, context_instance=RequestContext(request))
def main(request): results = {} results = None query = None query = request.GET.get('q', '') if len(query) < 2: return render_to_response('catalogue/search_too_short.html', {'prefix': query}, context_instance=RequestContext(request)) query = remove_query_syntax_chars(query) search = Search() theme_terms = search.index.analyze(text=query, field="themes_pl") \ + search.index.analyze(text=query, field="themes") # change hints tags = search.hint_tags(query, pdcounter=True, prefix=False) tags = split_tags(tags) author_results = search.search_phrase(query, 'authors', book=True) translator_results = search.search_phrase(query, 'translators', book=True) title_results = search.search_phrase(query, 'title', book=True) # Boost main author/title results with mixed search, and save some of its results for end of list. # boost author, title results author_title_mixed = search.search_some(query, ['authors', 'translators', 'title', 'tags'], query_terms=theme_terms) author_title_rest = [] for b in author_title_mixed: also_in_mixed = filter(lambda ba: ba.book_id == b.book_id, author_results + translator_results + title_results) for b2 in also_in_mixed: b2.boost *= 1.1 if also_in_mixed is []: author_title_rest.append(b) # Do a phrase search but a term search as well - this can give us better snippets then search_everywhere, # Because the query is using only one field. text_phrase = SearchResult.aggregate( search.search_phrase(query, 'text', snippets=True, book=False), search.search_some(query, ['text'], snippets=True, book=False, query_terms=theme_terms)) everywhere = search.search_everywhere(query, query_terms=theme_terms) def already_found(results): def f(e): for r in results: if e.book_id == r.book_id: e.boost = 0.9 results.append(e) return True return False return f f = already_found(author_results + translator_results + title_results + text_phrase) everywhere = filter(lambda x: not f(x), everywhere) author_results = SearchResult.aggregate(author_results) translator_results = SearchResult.aggregate(translator_results) title_results = SearchResult.aggregate(title_results) everywhere = SearchResult.aggregate(everywhere, author_title_rest) for field, res in [('authors', author_results), ('translators', translator_results), ('title', title_results), ('text', text_phrase), ('text', everywhere)]: res.sort(reverse=True) for r in res: search.get_snippets(r, query, field, 3) suggestion = u'' def ensure_exists(r): try: return r.book except Book.DoesNotExist: return False author_results = filter(ensure_exists, author_results) translator_results = filter(ensure_exists, translator_results) title_results = filter(ensure_exists, title_results) text_phrase = filter(ensure_exists, text_phrase) everywhere = filter(ensure_exists, everywhere) results = author_results + translator_results + title_results + text_phrase + everywhere # ensure books do exists & sort them for res in (author_results, translator_results, title_results, text_phrase, everywhere): res.sort(reverse=True) # We don't want to redirect to book text, but rather display result page even with one result. # if len(results) == 1: # fragment_hits = filter(lambda h: 'fragment' in h, results[0].hits) # if len(fragment_hits) == 1: # #anchor = fragment_hits[0]['fragment'] # #frag = Fragment.objects.get(anchor=anchor) # return HttpResponseRedirect(fragment_hits[0]['fragment'].get_absolute_url()) # return HttpResponseRedirect(results[0].book.get_absolute_url()) if len(results) == 0: form = PublishingSuggestForm(initial={"books": query + ", "}) return render_to_response('catalogue/search_no_hits.html', {'tags': tags, 'prefix': query, "form": form, 'did_you_mean': suggestion}, context_instance=RequestContext(request)) return render_to_response('catalogue/search_multiple_hits.html', {'tags': tags, 'prefix': query, 'results': {'author': author_results, 'translator': translator_results, 'title': title_results, 'content': text_phrase, 'other': everywhere}, 'did_you_mean': suggestion}, context_instance=RequestContext(request))
def main(request): results = {} JVM.attachCurrentThread() # where to put this? results = None query = None fuzzy = False #0.8 query = request.GET.get('q','') # book_id = request.GET.get('book', None) # book = None # if book_id is not None: # book = get_object_or_404(Book, id=book_id) # hint = search.hint() # try: # tag_list = Tag.get_tag_list(tags) # except: # tag_list = [] if len(query) < 2: return render_to_response('catalogue/search_too_short.html', {'prefix': query}, context_instance=RequestContext(request)) search = get_search() # hint.tags(tag_list) # if book: # hint.books(book) tags = search.hint_tags(query, pdcounter=True, prefix=False, fuzzy=fuzzy) tags = split_tags(tags) toks = StringReader(query) tokens_cache = {} author_results = search.search_phrase(toks, 'authors', fuzzy=fuzzy, tokens_cache=tokens_cache) title_results = search.search_phrase(toks, 'title', fuzzy=fuzzy, tokens_cache=tokens_cache) # Boost main author/title results with mixed search, and save some of its results for end of list. # boost author, title results author_title_mixed = search.search_some(toks, ['authors', 'title', 'tags'], fuzzy=fuzzy, tokens_cache=tokens_cache) author_title_rest = [] for b in author_title_mixed: bks = filter(lambda ba: ba.book_id == b.book_id, author_results + title_results) for b2 in bks: b2.boost *= 1.1 if bks is []: author_title_rest.append(b) # Do a phrase search but a term search as well - this can give us better snippets then search_everywhere, # Because the query is using only one field. text_phrase = SearchResult.aggregate( search.search_phrase(toks, 'content', fuzzy=fuzzy, tokens_cache=tokens_cache, snippets=True, book=False, slop=4), search.search_some(toks, ['content'], tokens_cache=tokens_cache, snippets=True, book=False)) everywhere = search.search_everywhere(toks, fuzzy=fuzzy, tokens_cache=tokens_cache) def already_found(results): def f(e): for r in results: if e.book_id == r.book_id: e.boost = 0.9 results.append(e) return True return False return f f = already_found(author_results + title_results + text_phrase) everywhere = filter(lambda x: not f(x), everywhere) author_results = SearchResult.aggregate(author_results) title_results = SearchResult.aggregate(title_results) everywhere = SearchResult.aggregate(everywhere, author_title_rest) for res in [author_results, title_results, text_phrase, everywhere]: res.sort(reverse=True) for r in res: for h in r.hits: h['snippets'] = map(lambda s: re.subn(r"(^[ \t\n]+|[ \t\n]+$)", u"", re.subn(r"[ \t\n]*\n[ \t\n]*", u"\n", s)[0])[0], h['snippets']) suggestion = did_you_mean(query, search.get_tokens(toks, field="SIMPLE")) def ensure_exists(r): try: return r.book except Book.DoesNotExist: return False author_results = filter(ensure_exists, author_results) title_results = filter(ensure_exists, title_results) text_phrase = filter(ensure_exists, text_phrase) everywhere = filter(ensure_exists, everywhere) results = author_results + title_results + text_phrase + everywhere # ensure books do exists & sort them results.sort(reverse=True) if len(results) == 1: fragment_hits = filter(lambda h: 'fragment' in h, results[0].hits) if len(fragment_hits) == 1: #anchor = fragment_hits[0]['fragment'] #frag = Fragment.objects.get(anchor=anchor) return HttpResponseRedirect(fragment_hits[0]['fragment'].get_absolute_url()) return HttpResponseRedirect(results[0].book.get_absolute_url()) elif len(results) == 0: form = PublishingSuggestForm(initial={"books": query + ", "}) return render_to_response('catalogue/search_no_hits.html', {'tags': tags, 'prefix': query, "form": form, 'did_you_mean': suggestion}, context_instance=RequestContext(request)) return render_to_response('catalogue/search_multiple_hits.html', {'tags': tags, 'prefix': query, 'results': { 'author': author_results, 'title': title_results, 'content': text_phrase, 'other': everywhere}, 'did_you_mean': suggestion}, context_instance=RequestContext(request))
if shelf_tags: books = models.Book.tagged.with_all(shelf_tags).order_by() l_tags = models.Tag.objects.filter(category='book', slug__in=[book.book_tag_slug() for book in books.iterator()]) fragments = models.Fragment.tagged.with_any(l_tags, fragments) # newtagging goes crazy if we just try: #related_tags = models.Tag.objects.usage_for_queryset(fragments, counts=True, # extra={'where': ["catalogue_tag.category != 'book'"]}) fragment_keys = [fragment.pk for fragment in fragments.iterator()] if fragment_keys: related_tags = models.Fragment.tags.usage(counts=True, filters={'pk__in': fragment_keys}, extra={'where': ["catalogue_tag.category != 'book'"]}) related_tags = (tag for tag in related_tags if tag not in fragment_tags) categories = split_tags(related_tags) objects = fragments else: if shelf_is_set: objects = models.Book.tagged.with_all(tags) else: objects = models.Book.tagged_top_level(tags) # get related tags from `tag_counter` and `theme_counter` related_counts = {} tags_pks = [tag.pk for tag in tags] for book in objects.iterator(): for tag_pk, value in itertools.chain(book.tag_counter.iteritems(), book.theme_counter.iteritems()): if tag_pk in tags_pks: continue
def object_list(request, objects, fragments=None, related_tags=None, tags=None, list_type='books', extra=None): if not tags: tags = [] tag_ids = [tag.pk for tag in tags] related_tag_lists = [] if related_tags: related_tag_lists.append(related_tags) else: related_tag_lists.append( Tag.objects.usage_for_queryset( objects, counts=True).exclude(category='set').exclude(pk__in=tag_ids)) if not (extra and extra.get('theme_is_set')): if fragments is None: if list_type == 'gallery': fragments = PictureArea.objects.filter(picture__in=objects) else: fragments = Fragment.objects.filter(book__in=objects) related_tag_lists.append( Tag.objects.usage_for_queryset(fragments, counts=True).filter( category='theme').exclude(pk__in=tag_ids).only( 'name', 'sort_key', 'category', 'slug')) if isinstance(objects, QuerySet): objects = prefetch_relations(objects, 'author') categories = split_tags(*related_tag_lists) objects = list(objects) if not objects and len(tags) == 1 and list_type == 'books': if PictureArea.tagged.with_any( tags).exists() or Picture.tagged.with_any(tags).exists(): return redirect('tagged_object_list_gallery', '/'.join(tag.url_chunk for tag in tags)) if len(objects) > 3: best = random.sample(objects, 3) else: best = objects result = { 'object_list': objects, 'categories': categories, 'list_type': list_type, 'tags': tags, 'formats_form': forms.DownloadFormatsForm(), 'best': best, 'active_menu_item': list_type, } if extra: result.update(extra) return render_to_response('catalogue/tagged_object_list.html', result, context_instance=RequestContext(request))
def title_from_tags(tags): def split_tags(tags): result = {} for tag in tags: result[tag.category] = tag return result # TODO: Remove this after adding flection mechanism return simple_title(tags) class Flection(object): def get_case(self, name, flection): return name flection = Flection() self = split_tags(tags) title = u'' # Specjalny przypadek oglądania wszystkich lektur na danej półce if len(self) == 1 and 'set' in self: return u'Półka %s' % self['set'] # Specjalny przypadek "Twórczość w pozytywizmie", wtedy gdy tylko epoka # jest wybrana przez użytkownika if 'epoch' in self and len(self) == 1: text = u'Twórczość w %s' % flection.get_case(unicode(self['epoch']), u'miejscownik') return capfirst(text) # Specjalny przypadek "Dramat w twórczości Sofoklesa", wtedy gdy podane # są tylko rodzaj literacki i autor if 'kind' in self and 'author' in self and len(self) == 2: text = u'%s w twórczości %s' % ( unicode(self['kind']), flection.get_case(unicode(self['author']), u'dopełniacz')) return capfirst(text) # Przypadki ogólniejsze if 'theme' in self: title += u'Motyw %s' % unicode(self['theme']) if 'genre' in self: if 'theme' in self: title += u' w %s' % flection.get_case(unicode(self['genre']), u'miejscownik') else: title += unicode(self['genre']) if 'kind' in self or 'author' in self or 'epoch' in self: if 'genre' in self or 'theme' in self: if 'kind' in self: title += u' w %s ' % flection.get_case(unicode(self['kind']), u'miejscownik') else: title += u' w twórczości ' else: title += u'%s ' % unicode(self.get('kind', u'twórczość')) if 'author' in self: title += flection.get_case(unicode(self['author']), u'dopełniacz') elif 'epoch' in self: title += flection.get_case(unicode(self['epoch']), u'dopełniacz') return capfirst(title)