diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2016-09-05 19:10:39 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2016-09-05 19:28:48 +0200 |
commit | 9186af3a28bfb99eebf8b468a66af00c61da54c1 (patch) | |
tree | 2039c5911319a7c2930584ec0e2b5c6977c2bdeb | |
parent | f751e22887e8940d0e3ed3c81eee0fb92e8d7820 (diff) | |
download | Ishtar-9186af3a28bfb99eebf8b468a66af00c61da54c1.tar.bz2 Ishtar-9186af3a28bfb99eebf8b468a66af00c61da54c1.zip |
Random image feature (refs #2958)
-rw-r--r-- | ishtar_common/models.py | 3 | ||||
-rw-r--r-- | ishtar_common/static/media/style.css | 5 | ||||
-rw-r--r-- | ishtar_common/templates/welcome.html | 1 | ||||
-rw-r--r-- | ishtar_common/utils.py | 53 | ||||
-rw-r--r-- | ishtar_common/views.py | 6 |
5 files changed, 66 insertions, 2 deletions
diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 4e46693de..087e772e7 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -954,7 +954,8 @@ class IshtarSiteProfile(models.Model, Cached): homepage = models.TextField( _(u"Home page"), null=True, blank=True, help_text=_(u"Homepage of Ishtar - if not defined a default homepage " - u"will appear. Use the markdown syntax.")) + u"will appear. Use the markdown syntax. {random_image} " + u"can be used to display a random image.")) file_external_id = models.TextField( _(u"File external id"), default="{settings__ISHTAR_LOCAL_PREFIX}{year}-{numeric_reference}", diff --git a/ishtar_common/static/media/style.css b/ishtar_common/static/media/style.css index 7dd424974..c6a151dc8 100644 --- a/ishtar_common/static/media/style.css +++ b/ishtar_common/static/media/style.css @@ -920,6 +920,11 @@ a.photo{ margin-right:auto; } +.welcome-image{ + padding: 1em; + text-align: center; +} + .dashboard table.resume th{ text-align:center; padding:0.5em; diff --git a/ishtar_common/templates/welcome.html b/ishtar_common/templates/welcome.html index 42935d8f9..eb9de475e 100644 --- a/ishtar_common/templates/welcome.html +++ b/ishtar_common/templates/welcome.html @@ -2,6 +2,7 @@ {% load url from future %} <h2>{% trans "Welcome in Ishtar, open source software for management and inventory of archaeological data" %}</h2> +{{random_image}} <p>{% trans "Some useful links:" %}</p> <ul> <li><a href='https://ishtar-archeo.net' target="_blank">{% trans "Presentation site and blog" %}</a>{% trans ":"%} {% trans "stay tuned with Ishtar news!" %}</li> diff --git a/ishtar_common/utils.py b/ishtar_common/utils.py index d4973012e..cb45d32e1 100644 --- a/ishtar_common/utils.py +++ b/ishtar_common/utils.py @@ -17,9 +17,13 @@ # See the file COPYING for details. +import random + from django.conf import settings from django.core.cache import cache -from django.utils.translation import ugettext +from django.core.urlresolvers import reverse +from django.utils.safestring import mark_safe +from django.utils.translation import ugettext_lazy as _, ugettext from django.template.defaultfilters import slugify @@ -61,3 +65,50 @@ def shortify(lbl, number=20): def mode(array): most = max(list(map(array.count, array))) return list(set(filter(lambda x: array.count(x) == most, array))) + + +def _get_image_link(item): + return mark_safe(u""" + <div class="welcome-image"> + <img src="{}"/><br/> + <em>{} - {}</em> + <a href="#" onclick="load_window(\'{}\')"> + <i class="fa fa-info-circle" aria-hidden="true"></i> + </a> + <a href="." title="{}"> + <i class="fa fa-random" aria-hidden="true"></i> + </a><br/> + </div>""".format( + item.thumbnail.url, + unicode(item.__class__._meta.verbose_name), + unicode(item), + reverse(item.SHOW_URL, args=[item.pk, '']), + unicode(_(u"Load another random image?")))) + + +def get_random_item_image_link(): + from archaeological_operations.models import Operation + from archaeological_context_records.models import ContextRecord + from archaeological_finds.models import Find + ope_image_nb = Operation.objects.filter(image__isnull=False).count() + cr_image_nb = ContextRecord.objects.filter(image__isnull=False).count() + find_image_nb = Find.objects.filter(image__isnull=False).count() + + image_total = ope_image_nb + cr_image_nb + find_image_nb + if not image_total: + return '' + + image_nb = random.randint(0, image_total - 1) + if image_nb >= 0 and image_nb < ope_image_nb: + return _get_image_link( + Operation.objects.filter(image__isnull=False).all()[image_nb]) + if image_nb >= ope_image_nb and image_nb < (cr_image_nb + ope_image_nb): + return _get_image_link( + ContextRecord.objects.filter(image__isnull=False).all()[ + image_nb - ope_image_nb]) + if image_nb >= (cr_image_nb + ope_image_nb): + return _get_image_link( + Find.objects.filter(image__isnull=False).all()[ + image_nb - ope_image_nb - cr_image_nb]) + # should never happen except in case of deletion during the excution + return '' diff --git a/ishtar_common/views.py b/ishtar_common/views.py index 79d3054b3..ea2eda462 100644 --- a/ishtar_common/views.py +++ b/ishtar_common/views.py @@ -62,6 +62,7 @@ from archaeological_operations.forms import DashboardForm as DashboardFormOpe from archaeological_files.forms import DashboardForm as DashboardFormFile from ishtar_common.forms import FinalForm, FinalDeleteForm +from ishtar_common.utils import get_random_item_image_link from ishtar_common import forms_common as forms from ishtar_common import wizards from ishtar_common.models import HistoryError, PRIVATE_FIELDS, \ @@ -81,6 +82,11 @@ def index(request): profile = get_current_profile() if hasattr(profile, 'homepage') and profile.homepage: dct['homepage'] = markdown(profile.homepage) + if '{random_image}' in dct['homepage']: + dct['homepage'] = dct['homepage'].replace( + '{random_image}', get_random_item_image_link()) + else: + dct['random_image'] = get_random_item_image_link() try: return render_to_response('index.html', dct, context_instance=RequestContext(request)) |