From 1fd4a4d1a7541f2b92ee794e7adab3576bbd52ac Mon Sep 17 00:00:00 2001
From: Henrik Levkowetz <henrik@levkowetz.com>
Date: Wed, 14 Mar 2018 19:16:50 +0000
Subject: [PATCH] Added session ical links to document lists (WG docs, search
 results, etc.) for documents on upcoming meeting agendas.  Reduced the query
 and rendering times of document lists some more through additional
 prefetch_related().  - Legacy-Id: 14799

---
 ietf/doc/utils_search.py                      | 26 ++++++++++++++++---
 .../doc/search/search_result_row.html         |  8 ++++++
 2 files changed, 30 insertions(+), 4 deletions(-)

diff --git a/ietf/doc/utils_search.py b/ietf/doc/utils_search.py
index c3e44ca97..9fd224c23 100644
--- a/ietf/doc/utils_search.py
+++ b/ietf/doc/utils_search.py
@@ -1,10 +1,17 @@
+# Copyright The IETF Trust 2016, All Rights Reserved
+
+import datetime
+import debug                            # pyflakes:ignore
+
+from ietf.community.utils import augment_docs_with_tracking_info
 from ietf.doc.models import Document, DocAlias, RelatedDocument, DocEvent, TelechatDocEvent
 from ietf.doc.expire import expirable_draft
-from ietf.community.utils import augment_docs_with_tracking_info
+from ietf.meeting.models import SessionPresentation
 
 def wrap_value(v):
     return lambda: v
 
+
 def fill_in_document_table_attributes(docs):
     # fill in some attributes for the document table results to save
     # some hairy template code and avoid repeated SQL queries
@@ -35,6 +42,13 @@ def fill_in_document_table_attributes(docs):
             d.telechat_date = wrap_value(d.telechat_date(e))
             seen.add(e.doc_id)
 
+    # on agenda in upcoming meetings
+    presentations = SessionPresentation.objects.filter(session__meeting__date__gte=datetime.date.today()-datetime.timedelta(days=15)).select_related('session', 'document')
+    session_list = [ (p.document, p.session) for p in presentations ]
+    sessions = dict( (d, []) for (d, s) in session_list )
+    for (d, s) in session_list:
+        sessions[d].append(s)
+
     # misc
     for d in docs:
         # emulate canonical name which is used by a lot of the utils
@@ -64,6 +78,7 @@ def fill_in_document_table_attributes(docs):
 
             d.reviewed_by_teams = sorted(set(r.team for r in d.reviewrequest_set.filter(state__in=["requested","accepted","part-completed","completed"])), key=lambda g: g.acronym)
 
+        d.sessions = sessions[d] if d in sessions else []
 
     # RFCs
 
@@ -101,10 +116,13 @@ def prepare_document_table(request, docs, query=None, max_results=500):
     if not isinstance(docs, list):
         # evaluate and fill in attribute results immediately to decrease
         # the number of queries
-        docs = docs.select_related("ad", "std_level", "intended_std_level", "group", "stream")
-        docs = docs.prefetch_related("states__type", "tags", "groupmilestone_set__group", "reviewrequest_set__team")
+        docs = docs.select_related("ad", "std_level", "intended_std_level", "group", "stream", "shepherd", )
+        docs = docs.prefetch_related("states__type", "tags", "groupmilestone_set__group", "reviewrequest_set__team",
+                                     "submission_set__checks", "ad__email_set")
 
-    docs = list(docs[:max_results])
+    if docs.count() > max_results:
+        docs = docs[:max_results]
+    docs = list(docs)
 
     fill_in_document_table_attributes(docs)
     augment_docs_with_tracking_info(docs, request.user)
diff --git a/ietf/templates/doc/search/search_result_row.html b/ietf/templates/doc/search/search_result_row.html
index f2adcb93f..c191242bc 100644
--- a/ietf/templates/doc/search/search_result_row.html
+++ b/ietf/templates/doc/search/search_result_row.html
@@ -20,6 +20,14 @@
         <span class="fa fa-bookmark-o"></span>
       </a>
     {% endif %}
+    {% for session in doc.sessions %}
+      <br><small>
+      <a href="{% url 'ietf.meeting.views.ical_agenda' num=session.meeting.number session_id=session.id %}"
+	title="Calendar entry: document is on the agenda for {{ session.group.acronym }}@{{ session.meeting }}">
+	<span class="fa fa-calendar"></span>
+      </a>
+      </small>
+    {% endfor %}
   </td>
 
   <td class="doc">