Projet

Général

Profil

0001-add-an-html_value-method-to-attribut-kinds.patch

Benjamin Dauvergne, 22 octobre 2018 15:09

Télécharger (4,71 ko)

Voir les différences:

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
src/authentic2/attribute_kinds.py
17 17
from .plugins import collect_from_plugins
18 18
from . import app_settings
19 19
from .forms import widgets
20
from .utils import profile_image_serialize, profile_image_deserialize
20
from .utils import profile_image_serialize, profile_image_deserialize, profile_image_html_value
21 21

  
22 22
capfirst = allow_lazy(capfirst, unicode)
23 23

  
......
172 172
        'deserialize': profile_image_deserialize,
173 173
        'rest_framework_field_kwargs': {
174 174
            'read_only': True,
175
            },
175
        },
176
        'html_value': profile_image_html_value,
176 177
    },
177 178
]
178 179

  
src/authentic2/templates/authentic2/accounts.html
16 16
      {% if attributes %}
17 17
        <dl>
18 18
          {% for attribute in attributes %}
19
            {% include "authentic2/accounts_attribute.html" %}
19
            <dt>{{ attribute.attribute.label|capfirst }}&nbsp;:</dt>
20
            <dd>
21
              {% if attribute.values|length == 1 %}
22
                {{ attribute.values.0 }}
23
              {% else %}
24
                <ul>
25
                  {% for value in attribute.values %}
26
                    <li>{{ value }}</li>
27
                  {% endfor %}
28
                </ul>
29
              {% endif %}
30
            </dd>
20 31
          {% endfor %}
21 32
        </dl>
22 33
      {% endif %}
src/authentic2/templates/authentic2/accounts_attribute.html
1
{% load authentic2 %}
2
<dt>{{ attribute.attribute.label|capfirst }}&nbsp;:</dt>
3
<dd>
4
  {% if attribute.attribute.kind == 'profile_image' %}
5
      {% thumbnail attribute.values.0 as thumb_im %}
6
      <img class="{{ attribute.attribute.name }}" src="{{ thumb_im.url }}">
7
  {% else %}
8
    {% if attribute.values|length == 1 %}
9
      {{ attribute.values.0 }}
10
    {% else %}
11
      <ul>
12
        {% for value in attribute.values %}
13
          <li>{{ value }}</li>
14
        {% endfor %}
15
      </ul>
16
    {% endif %}
17
  {% endif %}
18
</dd>
src/authentic2/utils.py
1119 1119
        logger.error("Couldn't generate thumbnail for image %s" % img)
1120 1120
    else:
1121 1121
        return thumb
1122

  
1123

  
1124
def profile_image_html_value(attribute, value):
1125
    from sorl.thumbnail import get_thumbnail
1126

  
1127
    try:
1128
        thumb = get_thumbnail(value, dimensions, crop=crop, quality=quality)
1129
    except:
1130
        return ''
1131

  
1132
    fragment = u'<a href="%s/%s"><img class="%s" src="%s"/></a>' % (
1133
        settings.MEDIA_URL, value, attribute.name, thumb.url)
1134
    return html.mark_safe(fragment)
src/authentic2/views.py
444 444
            if attribute:
445 445
                if not attribute.user_visible:
446 446
                    continue
447
                html_value = attribute.get_kind().get('html_value', lambda a, b: b)
447 448
                qs = models.AttributeValue.objects.with_owner(request.user)
448 449
                qs = qs.filter(attribute=attribute)
449 450
                qs = qs.select_related()
450 451
                value = [at_value.to_python() for at_value in qs]
451 452
                value = filter(None, value)
453
                value = map(html_value, value)
452 454
                if not title:
453 455
                    title = unicode(attribute)
454 456
            else:
455
-