Revamp tracked/not tracked icons in search results to not do one query
per document, and make them work on all search result pages, move the utility function to community/ - Legacy-Id: 10690
This commit is contained in:
parent
22e2a6a2b8
commit
b7232d0ab7
|
@ -1,3 +1,4 @@
|
|||
from ietf.community.models import CommunityList
|
||||
from ietf.group.models import Role
|
||||
|
||||
def can_manage_community_list_for_group(user, group):
|
||||
|
@ -11,3 +12,16 @@ def can_manage_community_list_for_group(user, group):
|
|||
else:
|
||||
return False
|
||||
|
||||
def augment_docs_with_tracking_info(docs, user):
|
||||
"""Add attribute to each document with whether the document is tracked
|
||||
by the user or not."""
|
||||
|
||||
tracked = set()
|
||||
|
||||
if user and user.is_authenticated():
|
||||
clist = CommunityList.objects.filter(user=user).first()
|
||||
if clist:
|
||||
tracked.update(clist.get_documents().filter(pk__in=docs).values_list("pk", flat=True))
|
||||
|
||||
for d in docs:
|
||||
d.tracked_in_personal_community_list = d.pk in tracked
|
||||
|
|
|
@ -33,7 +33,6 @@
|
|||
import datetime, re
|
||||
|
||||
from django import forms
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
from django.core.urlresolvers import reverse as urlreverse
|
||||
from django.shortcuts import render
|
||||
from django.db.models import Q
|
||||
|
@ -41,7 +40,6 @@ from django.http import Http404, HttpResponseBadRequest, HttpResponse, HttpRespo
|
|||
|
||||
import debug # pyflakes:ignore
|
||||
|
||||
from ietf.community.models import CommunityList
|
||||
from ietf.doc.models import ( Document, DocHistory, DocAlias, State, RelatedDocument,
|
||||
DocEvent, LastCallDocEvent, TelechatDocEvent, IESG_SUBSTATE_TAGS )
|
||||
from ietf.doc.expire import expirable_draft
|
||||
|
@ -51,6 +49,7 @@ from ietf.idindex.index import active_drafts_index_by_group
|
|||
from ietf.name.models import DocTagName, DocTypeName, StreamName
|
||||
from ietf.person.models import Person
|
||||
from ietf.utils.draft_search import normalize_draftname
|
||||
from ietf.community.utils import augment_docs_with_tracking_info
|
||||
|
||||
|
||||
class SearchForm(forms.Form):
|
||||
|
@ -201,9 +200,9 @@ def fill_in_search_attributes(docs):
|
|||
l.sort()
|
||||
|
||||
|
||||
def retrieve_search_results(form, all_types=False):
|
||||
|
||||
def retrieve_search_results(form, request, all_types=False):
|
||||
"""Takes a validated SearchForm and return the results."""
|
||||
|
||||
if not form.is_valid():
|
||||
raise ValueError("SearchForm doesn't validate: %s" % form.errors)
|
||||
|
||||
|
@ -322,6 +321,8 @@ def retrieve_search_results(form, all_types=False):
|
|||
|
||||
results.sort(key=sort_key)
|
||||
|
||||
augment_docs_with_tracking_info(results, request.user)
|
||||
|
||||
# fill in a meta dict with some information for rendering the result table
|
||||
if len(results) == MAX:
|
||||
meta['max'] = MAX
|
||||
|
@ -345,21 +346,6 @@ def retrieve_search_results(form, all_types=False):
|
|||
return (results, meta)
|
||||
|
||||
|
||||
def get_doc_is_tracked(request, results):
|
||||
# Determine whether each document is being tracked or not, and remember
|
||||
# that so we can display the proper track/untrack option.
|
||||
doc_is_tracked = { }
|
||||
if request.user.is_authenticated():
|
||||
try:
|
||||
clist = CommunityList.objects.get(user=request.user)
|
||||
clist.update()
|
||||
except ObjectDoesNotExist:
|
||||
return doc_is_tracked
|
||||
for doc in results:
|
||||
if clist.get_documents().filter(name=doc.name).count() > 0:
|
||||
doc_is_tracked[doc.name] = True
|
||||
return doc_is_tracked
|
||||
|
||||
def search(request):
|
||||
if request.GET:
|
||||
# backwards compatibility
|
||||
|
@ -375,17 +361,15 @@ def search(request):
|
|||
if not form.is_valid():
|
||||
return HttpResponseBadRequest("form not valid: %s" % form.errors)
|
||||
|
||||
results, meta = retrieve_search_results(form)
|
||||
results, meta = retrieve_search_results(form, request)
|
||||
meta['searching'] = True
|
||||
else:
|
||||
form = SearchForm()
|
||||
results = []
|
||||
meta = { 'by': None, 'advanced': False, 'searching': False }
|
||||
|
||||
doc_is_tracked = get_doc_is_tracked(request, results)
|
||||
|
||||
return render(request, 'doc/search/search.html', {
|
||||
'form':form, 'docs':results, 'doc_is_tracked':doc_is_tracked, 'meta':meta, },
|
||||
'form':form, 'docs':results, 'meta':meta, },
|
||||
)
|
||||
|
||||
def frontpage(request):
|
||||
|
@ -550,7 +534,7 @@ def docs_for_ad(request, name):
|
|||
form = SearchForm({'by':'ad','ad': ad.id,
|
||||
'rfcs':'on', 'activedrafts':'on', 'olddrafts':'on',
|
||||
'sort': 'status'})
|
||||
results, meta = retrieve_search_results(form, all_types=True)
|
||||
results, meta = retrieve_search_results(form, request, all_types=True)
|
||||
results.sort(key=ad_dashboard_sort_key)
|
||||
del meta["headers"][-1]
|
||||
#
|
||||
|
@ -564,7 +548,7 @@ def docs_for_ad(request, name):
|
|||
def drafts_in_last_call(request):
|
||||
lc_state = State.objects.get(type="draft-iesg", slug="lc").pk
|
||||
form = SearchForm({'by':'state','state': lc_state, 'rfcs':'on', 'activedrafts':'on'})
|
||||
results, meta = retrieve_search_results(form)
|
||||
results, meta = retrieve_search_results(form, request)
|
||||
|
||||
return render(request, 'doc/drafts_in_last_call.html', {
|
||||
'form':form, 'docs':results, 'meta':meta
|
||||
|
|
|
@ -47,7 +47,7 @@ from django.views.decorators.cache import cache_page
|
|||
from django.db.models import Q
|
||||
from django.utils.safestring import mark_safe
|
||||
|
||||
from ietf.doc.views_search import SearchForm, retrieve_search_results, get_doc_is_tracked
|
||||
from ietf.doc.views_search import SearchForm, retrieve_search_results
|
||||
from ietf.doc.models import Document, State, DocAlias, RelatedDocument
|
||||
from ietf.doc.utils import get_chartering_type
|
||||
from ietf.doc.templatetags.ietf_filters import clean_whitespace
|
||||
|
@ -378,13 +378,13 @@ def construct_group_menu_context(request, group, selected, group_type, others):
|
|||
|
||||
return d
|
||||
|
||||
def search_for_group_documents(group):
|
||||
def search_for_group_documents(group, request):
|
||||
form = SearchForm({ 'by':'group', 'group': group.acronym or "", 'rfcs':'on', 'activedrafts': 'on' })
|
||||
docs, meta = retrieve_search_results(form)
|
||||
docs, meta = retrieve_search_results(form, request)
|
||||
|
||||
# get the related docs
|
||||
form_related = SearchForm({ 'by':'group', 'name': u'-%s-' % group.acronym, 'activedrafts': 'on' })
|
||||
raw_docs_related, meta_related = retrieve_search_results(form_related)
|
||||
raw_docs_related, meta_related = retrieve_search_results(form_related, request)
|
||||
|
||||
docs_related = []
|
||||
for d in raw_docs_related:
|
||||
|
@ -423,17 +423,13 @@ def group_documents(request, acronym, group_type=None):
|
|||
if not group.features.has_documents:
|
||||
raise Http404
|
||||
|
||||
docs, meta, docs_related, meta_related = search_for_group_documents(group)
|
||||
|
||||
doc_is_tracked = get_doc_is_tracked(request, docs)
|
||||
doc_is_tracked.update(get_doc_is_tracked(request, docs_related))
|
||||
docs, meta, docs_related, meta_related = search_for_group_documents(group, request)
|
||||
|
||||
context = construct_group_menu_context(request, group, "documents", group_type, {
|
||||
'docs': docs,
|
||||
'meta': meta,
|
||||
'docs_related': docs_related,
|
||||
'meta_related': meta_related,
|
||||
'doc_is_tracked': doc_is_tracked,
|
||||
})
|
||||
|
||||
return render(request, 'group/group_documents.html', context)
|
||||
|
@ -444,7 +440,7 @@ def group_documents_txt(request, acronym, group_type=None):
|
|||
if not group.features.has_documents:
|
||||
raise Http404
|
||||
|
||||
docs, meta, docs_related, meta_related = search_for_group_documents(group)
|
||||
docs, meta, docs_related, meta_related = search_for_group_documents(group, request)
|
||||
|
||||
for d in docs:
|
||||
d.prefix = d.get_state().name
|
||||
|
|
|
@ -29,7 +29,7 @@ def stream_documents(request, acronym):
|
|||
stream = StreamName.objects.get(slug=acronym)
|
||||
form = SearchForm({'by':'stream', 'stream':acronym,
|
||||
'rfcs':'on', 'activedrafts':'on'})
|
||||
docs, meta = retrieve_search_results(form)
|
||||
docs, meta = retrieve_search_results(form, request)
|
||||
return render_to_response('group/stream_documents.html', {'stream':stream, 'docs':docs, 'meta':meta, 'editable':editable }, context_instance=RequestContext(request))
|
||||
|
||||
class StreamEditForm(forms.Form):
|
||||
|
|
|
@ -13,7 +13,7 @@
|
|||
|
||||
<td>
|
||||
{% if user.is_authenticated %}
|
||||
{% if doc.name in doc_is_tracked %}
|
||||
{% if doc.tracked_in_personal_community_list %}
|
||||
<a href="{% url "community_personal_untrack_document" doc.name %}" class="community-list-add-remove-doc" title="Remove from your personal ID list">
|
||||
<span class="fa fa-bookmark"></span>
|
||||
</a>
|
||||
|
|
Loading…
Reference in a new issue