other optimisation
authorcavaliet
Wed, 30 Oct 2013 10:48:44 +0100
changeset 162 e849690649d4
parent 161 adafb3d13a19
child 163 bd4fac229345
other optimisation
src/jocondelab/views/front_office.py
--- a/src/jocondelab/views/front_office.py	Tue Oct 29 13:55:08 2013 +0100
+++ b/src/jocondelab/views/front_office.py	Wed Oct 30 10:48:44 2013 +0100
@@ -77,7 +77,7 @@
                 else:
                     self.template_name = "jocondelab/front_search.html"
         
-        qs = Notice.objects.filter(image=True)
+        qs = Notice.objects.prefetch_related("images").filter(image=True)
         
         if emptysearch:
             context["count"] = qs.count()
@@ -132,16 +132,21 @@
             logger.debug("COUCOU 0")
             termsbythesaurus = get_terms_by_thesaurus(n, lang)
             logger.debug("COUCOU 1")
+            # select first image with "_p.jpg"
+            selected_image = None
+            for i in n.images.all():
+                if i.relative_url.endswith('p.jpg'):
+                    selected_image = settings.JOCONDE_IMG_BASE_URL + i.relative_url
+                    break
             noticedict = {
                 "id": n.id,
                 "title": n.titr,
                 "designation": " | ".join([ v for v in [ n.deno, n.appl] if v ]),
-                "image": (settings.JOCONDE_IMG_BASE_URL + n.images.exclude(relative_url__endswith='v.jpg').order_by('id')[0].relative_url) if n.images.exists() else "",
+                "image": selected_image,
                 "author": n.autr,
                 "terms_by_thesaurus": termsbythesaurus,
                 "datation": termsbythesaurus.get("PERI",{}).get("translated",[]) + termsbythesaurus.get("EPOQ",{}).get("translated",[])
             }
-            logger.debug("COUCOU 2")
             noticedict['image_title'] = noticedict['title'] if noticedict['title'] else noticedict['designation']
             if show_years and n.years.exists():
                 noticedict["years"] = [n.years.all()[0].start_year, n.years.all()[0].end_year]