From 7b0ed67b7b15e1cf7a91da65e04a2b7c53c52817 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Mon, 22 Oct 2018 14:55:24 +0200 Subject: [PATCH] add an html_value method to attribut kinds --- src/authentic2/attribute_kinds.py | 5 +++-- .../templates/authentic2/accounts.html | 13 ++++++++++++- .../authentic2/accounts_attribute.html | 18 ------------------ src/authentic2/utils.py | 13 +++++++++++++ src/authentic2/views.py | 2 ++ 5 files changed, 30 insertions(+), 21 deletions(-) delete mode 100644 src/authentic2/templates/authentic2/accounts_attribute.html diff --git a/src/authentic2/attribute_kinds.py b/src/authentic2/attribute_kinds.py index a31995fa..8f213dfd 100644 --- a/src/authentic2/attribute_kinds.py +++ b/src/authentic2/attribute_kinds.py @@ -17,7 +17,7 @@ from .decorators import to_iter from .plugins import collect_from_plugins from . import app_settings from .forms import widgets -from .utils import profile_image_serialize, profile_image_deserialize +from .utils import profile_image_serialize, profile_image_deserialize, profile_image_html_value capfirst = allow_lazy(capfirst, unicode) @@ -172,7 +172,8 @@ DEFAULT_ATTRIBUTE_KINDS = [ 'deserialize': profile_image_deserialize, 'rest_framework_field_kwargs': { 'read_only': True, - }, + }, + 'html_value': profile_image_html_value, }, ] diff --git a/src/authentic2/templates/authentic2/accounts.html b/src/authentic2/templates/authentic2/accounts.html index 0e212ad2..4e9f979f 100644 --- a/src/authentic2/templates/authentic2/accounts.html +++ b/src/authentic2/templates/authentic2/accounts.html @@ -16,7 +16,18 @@ {% if attributes %}
{% for attribute in attributes %} - {% include "authentic2/accounts_attribute.html" %} +
{{ attribute.attribute.label|capfirst }} :
+
+ {% if attribute.values|length == 1 %} + {{ attribute.values.0 }} + {% else %} +
    + {% for value in attribute.values %} +
  • {{ value }}
  • + {% endfor %} +
+ {% endif %} +
{% endfor %}
{% endif %} diff --git a/src/authentic2/templates/authentic2/accounts_attribute.html b/src/authentic2/templates/authentic2/accounts_attribute.html deleted file mode 100644 index 75b3bba1..00000000 --- a/src/authentic2/templates/authentic2/accounts_attribute.html +++ /dev/null @@ -1,18 +0,0 @@ -{% load authentic2 %} -
{{ attribute.attribute.label|capfirst }} :
-
- {% if attribute.attribute.kind == 'profile_image' %} - {% thumbnail attribute.values.0 as thumb_im %} - - {% else %} - {% if attribute.values|length == 1 %} - {{ attribute.values.0 }} - {% else %} - - {% endif %} - {% endif %} -
diff --git a/src/authentic2/utils.py b/src/authentic2/utils.py index 83c0d35a..fd416403 100644 --- a/src/authentic2/utils.py +++ b/src/authentic2/utils.py @@ -1119,3 +1119,16 @@ def get_image_thumbnail(request, img): logger.error("Couldn't generate thumbnail for image %s" % img) else: return thumb + + +def profile_image_html_value(attribute, value): + from sorl.thumbnail import get_thumbnail + + try: + thumb = get_thumbnail(value, dimensions, crop=crop, quality=quality) + except: + return '' + + fragment = u'' % ( + settings.MEDIA_URL, value, attribute.name, thumb.url) + return html.mark_safe(fragment) diff --git a/src/authentic2/views.py b/src/authentic2/views.py index c9d8d9a7..d39419b4 100644 --- a/src/authentic2/views.py +++ b/src/authentic2/views.py @@ -444,11 +444,13 @@ class ProfileView(cbv.TemplateNamesMixin, TemplateView): if attribute: if not attribute.user_visible: continue + html_value = attribute.get_kind().get('html_value', lambda a, b: b) qs = models.AttributeValue.objects.with_owner(request.user) qs = qs.filter(attribute=attribute) qs = qs.select_related() value = [at_value.to_python() for at_value in qs] value = filter(None, value) + value = map(html_value, value) if not title: title = unicode(attribute) else: -- 2.18.0