Corrected image load bug and added describe prev/next
authorveltr
Tue, 08 Oct 2013 16:34:00 +0200
changeset 140 752062bdeb10
parent 137 e2043b49682d
child 141 93500ff3b697
Corrected image load bug and added describe prev/next
src/jocondelab/management/commands/populate_notice_years.py
src/jocondelab/static/jocondelab/css/front-notice.css
src/jocondelab/static/jocondelab/img/default-image.png
src/jocondelab/static/jocondelab/js/front-common.js
src/jocondelab/static/jocondelab/js/front-notice.js
src/jocondelab/templates/jocondelab/front_base.html
src/jocondelab/templates/jocondelab/front_describe.html
src/jocondelab/templates/jocondelab/partial/notice_list.html
src/jocondelab/templates/jocondelab/partial/wrapped_notice_list.html
src/jocondelab/urls.py
src/jocondelab/views/front_office.py
--- a/src/jocondelab/management/commands/populate_notice_years.py	Mon Oct 07 13:32:09 2013 +0200
+++ b/src/jocondelab/management/commands/populate_notice_years.py	Tue Oct 08 16:34:00 2013 +0200
@@ -1,12 +1,13 @@
 # -*- coding: utf-8 -*-
-from django.core.management.base import BaseCommand
+from django.core.management.base import NoArgsCommand
 from core.models import (Notice)
 from core.utils import show_progress
 from jocondelab.models import NoticeYears
 from django.db import reset_queries, transaction
+from optparse import make_option
 import re
 
-class Command(BaseCommand):
+class Command(NoArgsCommand):
     
     help = "Extract Years from MILL field"
     
--- a/src/jocondelab/static/jocondelab/css/front-notice.css	Mon Oct 07 13:32:09 2013 +0200
+++ b/src/jocondelab/static/jocondelab/css/front-notice.css	Tue Oct 08 16:34:00 2013 +0200
@@ -15,11 +15,23 @@
 }
 
 .describe-mode .notice-images li {
-    margin: 0; box-shadow: 0 0 3px #999999; display: inline-block; max-width: 100%;
+    margin: 0; display: inline-block; max-width: 80%;
+}
+
+.describe-mode .notice-images li:nth-child(n+2) {
+    float: right; clear: right; width: 20%;
 }
 
 .describe-mode .notice-images img {
-    max-height: 500px;
+    max-height: 480px;
+}
+
+.describe-mode .notice-images li:nth-child(n+2) img {
+    max-height: 160px;
+}
+
+.describe-title {
+    font-size: 18px; margin: 5px 0; clear: both; font-weight: bold;
 }
 
 .notice-colright-wrapper {
Binary file src/jocondelab/static/jocondelab/img/default-image.png has changed
--- a/src/jocondelab/static/jocondelab/js/front-common.js	Mon Oct 07 13:32:09 2013 +0200
+++ b/src/jocondelab/static/jocondelab/js/front-common.js	Tue Oct 08 16:34:00 2013 +0200
@@ -1,3 +1,24 @@
+/* Image load events may be triggered before jQuery is loaded.
+ * Here we bind temporary functions to the Window namespace */
+
+(function() {
+    
+    function tmpBind(property) {
+        window[property] = function() {
+            var arglist = Array.prototype.slice.call(arguments);
+            setTimeout(function() {
+                window[property].apply(window, arglist);
+            }, 500);
+        };
+    }
+    
+    var propsToBind = [ "onWikiImageLoad", "onWikiImageError", "onResultImageError" ];
+    for (var i = 0; i < propsToBind.length; i++) {
+        tmpBind(propsToBind[i]);
+    }
+    
+})();
+
 $(function() {
     
     /* SEARCH TAG-IT */
@@ -332,6 +353,11 @@
         adaptGrid();
     };
     
+    window.onResultImageError = function(img) {
+        img.src = urls.img_if_404;
+        throttledCheckSizes();
+    };
+    
     /* END NOTICE LIST MANAGEMENT */
     
     /* AJAX SCROLL LOAD */
@@ -398,6 +424,7 @@
     window.loadSearchResults = function(query) {
         $(".hide-on-search").hide();
         $win.off("scroll.ajaxload");
+        $(".wiki-info img").off("load error");
         $results.empty();
         $(".loading-please-wait").show();
         $.ajax({
@@ -443,11 +470,17 @@
         }
     }
     
-    /* */
-    
     resizeWikiInfo();
     
-    $(".wiki-info img").on("load", resizeWikiInfo);
+    window.onWikiImageLoad = function() {
+        resizeWikiInfo(true);
+    };
+    window.onWikiImageError = function(img) {
+        $(img).hide();
+        resizeWikiInfo(true);
+    };
+    
+    /* */
     
     $win.resize(function() {
         adaptGrid();
--- a/src/jocondelab/static/jocondelab/js/front-notice.js	Mon Oct 07 13:32:09 2013 +0200
+++ b/src/jocondelab/static/jocondelab/js/front-notice.js	Tue Oct 08 16:34:00 2013 +0200
@@ -291,4 +291,5 @@
         });
         return false;
     });
+        
 });
--- a/src/jocondelab/templates/jocondelab/front_base.html	Mon Oct 07 13:32:09 2013 +0200
+++ b/src/jocondelab/templates/jocondelab/front_base.html	Tue Oct 08 16:34:00 2013 +0200
@@ -31,7 +31,8 @@
         <script type="text/javascript">
             var urls = {
                 ajax_terms: "{% url 'ajax_terms' %}",
-                ajax_search: "{% url 'front_search' %}"
+                ajax_search: "{% url 'front_search' %}",
+                img_if_404: "{{STATIC_URL}}jocondelab/img/default-image.png"
             };
         </script>
         <script type="text/javascript" src="{{STATIC_URL}}jocondelab/js/front-common.js"></script>
--- a/src/jocondelab/templates/jocondelab/front_describe.html	Mon Oct 07 13:32:09 2013 +0200
+++ b/src/jocondelab/templates/jocondelab/front_describe.html	Tue Oct 08 16:34:00 2013 +0200
@@ -2,13 +2,12 @@
 {% load i18n %}
 
 {% block title %}JocondeLab &raquo; {% trans "Contribuer" %}{% endblock %}
-
-{% block breadcrumbs %}<a href="{% url 'random_describe' %}">{% trans "Contribuer" %}</a>{% endblock %}
+{% url 'front_about' as url %}
+{% block breadcrumbs %}<a href="{{url}}">{% trans "Contribuer" %}</a>{% endblock %}
 {% block main %}
 <div class="describe-mode">
-    {{object.id}}
     {% block images %}{{block.super}}{% endblock %}
-    <h2 style="font-size: 18px; margin: 5px 0; font-weight: bold;">{{object.titr}}</h2>
+    <h2 class="describe-title">{{object.titr}}</h2>
     {% block contribution %}
     <form class="contribution-frame description-frame">
         <h2>{% trans "Décrivez le sujet de cette œuvre" %}</h2>
@@ -16,7 +15,8 @@
         <input class="notice-contribution-field" data-thesaurus-label="REPR" type="text" size="60" placeholder="{% trans 'Que voyez-vous dans cette oeuvre ?' %}" />
         <ul class="notice-term-list notice-contribution-list contribution-novote"></ul>
         <p class="button-links">
-            <a href="{% url 'random_describe' %}">{% trans 'Œuvre suivante' %}</a>
+            {% if prev_notice %}<a href="{% url 'front_describe' prev_notice %}">{% trans 'Œuvre précédente' %}</a>{% endif %}
+            <a href="{% if next_notice %}{% url 'front_describe' next_notice  %}{% else %}{% url 'random_describe' %}{% endif %}">{% trans 'Œuvre suivante' %}</a>
         </p>
     </form>
     {% endblock %}
--- a/src/jocondelab/templates/jocondelab/partial/notice_list.html	Mon Oct 07 13:32:09 2013 +0200
+++ b/src/jocondelab/templates/jocondelab/partial/notice_list.html	Tue Oct 08 16:34:00 2013 +0200
@@ -4,7 +4,7 @@
     <li class="notice-item" data-notice-id="{{notice.id}}">
         <div class="notice-contents">
             <a href="{% url 'front_notice' notice.id %}" title="{{notice.imagetitle}}">
-                <img class="notice-image" alt="{{notice.imagetitle}}" src="{{ notice.image }}" />
+                <img class="notice-image" alt="{{notice.imagetitle}}" src="{{notice.image}}" onerror="window.onResultImageError(this);" />
             </a>
             <div class="notice-metadata">
                 {% if notice.title %}<h2><a href="{% url 'front_notice' notice.id %}">{% trans 'TITR' context 'Thesaurus label' %}{% trans ':' %} <span class="notice-title" dir="ltr">{{notice.title}}</span></a></h2>{% endif %}
--- a/src/jocondelab/templates/jocondelab/partial/wrapped_notice_list.html	Mon Oct 07 13:32:09 2013 +0200
+++ b/src/jocondelab/templates/jocondelab/partial/wrapped_notice_list.html	Tue Oct 08 16:34:00 2013 +0200
@@ -8,7 +8,7 @@
     <li class="wiki-info" data-dbpedia-uri="{{wkinfo.dbpedia_uri}}">
         {% if wkinfo.thumbnail %}
         <a href="{{wkinfo.label|wikiurl:lang}}" target="_blank">
-            <img class="wiki-info-image" src="{{wkinfo.thumbnail}}" alt="{{wkinfo.label}}" />
+            <img class="wiki-info-image" src="{{wkinfo.thumbnail}}" alt="{{wkinfo.label}}" onload="window.onWikiImageLoad();" onerror="window.onWikiImageError(this);" />
         </a>
         {% endif %}
         <h3 class="wiki-info-title">
--- a/src/jocondelab/urls.py	Mon Oct 07 13:32:09 2013 +0200
+++ b/src/jocondelab/urls.py	Tue Oct 08 16:34:00 2013 +0200
@@ -39,7 +39,8 @@
     url(r'^timeline/$', TemplateView.as_view(template_name="jocondelab/front_timeline.html"), name='front_timeline'),
     url(r'^about/$', TemplateView.as_view(template_name="jocondelab/front_about.html"), name='front_about'),
     url(r'^notice/(?P<pk>\d+)/$', NoticeView.as_view(), name='front_notice'),
-    url(r'^describe/$', NoticeView.as_view(template_name="jocondelab/front_describe.html", show_contributions=False), name='random_describe'),
+    url(r'^describe/(?P<pk>\d+)/$', NoticeView.as_view(template_name="jocondelab/front_describe.html", show_contributions=False), name='front_describe'),
+    url(r'^describe/$', 'jocondelab.views.front_office.describe_view', name='random_describe'),
     url(r'^ajax/terms/$', 'jocondelab.views.ajax.terms', name='ajax_terms'),
     url(r'^ajax/years/$', 'jocondelab.views.ajax.years', name='ajax_years'),
     url(r'^ajax/geocoords/$', 'jocondelab.views.ajax.geo_coords', name='ajax_geo_coords'),
--- a/src/jocondelab/views/front_office.py	Mon Oct 07 13:32:09 2013 +0200
+++ b/src/jocondelab/views/front_office.py	Tue Oct 08 16:34:00 2013 +0200
@@ -13,6 +13,7 @@
 from django.views.generic import DetailView, TemplateView
 from django.db.models import Sum, Count
 import random
+from django.shortcuts import redirect
 
 class SearchView(TemplateView):
     
@@ -94,9 +95,8 @@
             } for t in Term.objects.filter(noticeterm__notice=n,dbpedia_fields__language_code=lang).distinct()]
             termsbythesaurus = {}
             for term in terms:
-                if not term["thesaurus"] in termsbythesaurus:
-                    termsbythesaurus[term["thesaurus"]] = []
-                termsbythesaurus[term["thesaurus"]].append(term)
+                th = termsbythesaurus.setdefault(term["thesaurus"], [])
+                th.append(term)
             noticedict = {
                 "id": n.id,
                 "imagetitle": n.titr if n.titr else n.deno,
@@ -147,12 +147,8 @@
                 "translated": False,
                 "label": nt.label
             }
-            if not term["thesaurus"] in termsbythesaurus:
-                termsbythesaurus[term["thesaurus"]] = {
-                   "translated": [],
-                   "untranslated": []
-                   }
-            termsbythesaurus[term["thesaurus"]]["untranslated"].append(term)
+            th = termsbythesaurus.setdefault(term["thesaurus"], { "translated": [], "untranslated": [] })
+            th["untranslated"].append(term)
         for nt in Term.objects.filter(noticeterm__notice=self.object, dbpedia_fields__language_code=lang).select_related('thesaurus,dbpedia_fields').order_by('dbpedia_fields__label').distinct():
             term = {
                 "thesaurus": nt.thesaurus.label,
@@ -160,12 +156,8 @@
                 "translated": True,
                 "label": nt.dbpedia_fields.get(language_code = lang).label
             }
-            if not term["thesaurus"] in termsbythesaurus:
-                termsbythesaurus[term["thesaurus"]] = {
-                   "translated": [],
-                   "untranslated": []
-                   }
-            termsbythesaurus[term["thesaurus"]]["translated"].append(term)
+            th = termsbythesaurus.setdefault(term["thesaurus"], { "translated": [], "untranslated": [] })
+            th["translated"].append(term)
         context["terms_by_thesaurus"] = termsbythesaurus
         
         if self.show_contributions:
@@ -183,14 +175,26 @@
         context['wikipedia_urls'] = json.dumps(settings.WIKIPEDIA_URLS)
         context['JOCONDE_NOTICE_BASE_URL'] = settings.JOCONDE_NOTICE_BASE_URL
         
+        notice_history = self.request.session.get('notice_history',None)            
+        if self.object.id in notice_history:
+            p = notice_history.index(self.object.id)
+            if p < len(notice_history) - 1:
+                context['next_notice'] = notice_history[p + 1]
+            if p > 0:
+                context['prev_notice'] = notice_history[p - 1]
+        
         return context
-    
-    def get_object(self):
-        if self.pk_url_kwarg in self.kwargs:
-            return super(NoticeView, self).get_object()
-        else:
-            queryset = self.get_queryset().filter(image=True, repr='', peri__contains='4e quart 19e siècle', noticeterm__term__id__in=ContributableTerm.objects.values('term__id'))
-            return queryset[random.randint(0, queryset.count() - 1)]
+
+def describe_view(request):
+    notice_history = request.session.get('notice_history',[])
+    queryset = Notice.objects.filter(image=True, repr='', peri__contains='4e quart 19e siècle', noticeterm__term__id__in=ContributableTerm.objects.values('term__id'))
+    for i in range(10):
+        id = queryset[random.randint(0, queryset.count() - 1)].id
+        if id not in notice_history:
+            break
+    notice_history.append(id)
+    request.session['notice_history'] = notice_history
+    return redirect('front_describe', pk=id)
 
 class FrontTermListView(TemplateView):