ellipsize at model level to avoid escape clusterfuck (#49189)

This commit is contained in:
Emmanuel Cazenave 2020-12-02 15:20:55 +01:00
parent ca7a948c2b
commit e495a58638
3 changed files with 15 additions and 9 deletions

View File

@ -2,6 +2,7 @@ import os
import datetime as dt
import random
import hashlib
import html
import re
from collections import defaultdict
import time
@ -15,6 +16,7 @@ from django.template.defaultfilters import slugify
from django.utils.translation import ugettext_lazy as _, pgettext_lazy
from django.urls import reverse
from picklefield.fields import PickledObjectField
from django.utils.html import strip_tags
from django.utils.timezone import now, utc
from django.forms import ValidationError
from django.utils.encoding import force_text, python_2_unicode_compatible
@ -32,6 +34,14 @@ DOCBOW_APP2 = _('Docbow_app')
# fixup User ordering
User._meta.ordering = [ 'username' ]
def ellipsize(text, length=50):
text = html.unescape(strip_tags(text))
if len(text) < length:
return text
return text[:(length-10)] + '...'
class GetByNameManager(Manager):
'''Manager providing a get_by_natural_key() method to retrieve object by
their name. The name field MUST be unique.'''
@ -496,6 +506,7 @@ class AutomaticForwarding(Model):
verbose_name_plural = _('Automatic forwarding rules')
@python_2_unicode_compatible
class AttachedFile(Model):
'''Uploaded file attached to a Document'''
@ -522,6 +533,9 @@ class AttachedFile(Model):
return filename
def ellipsed_name(self):
return ellipsize(self.name)
def link(self):
'''Returns the link for downloading the attached file in the admin
application.

View File

@ -22,7 +22,7 @@
<a title="{{file.name}}" href="{{file.pk}}/{{file.name}}">
{% trans "Download" %}
<span class="file-size">({{file.content.size|frfilesizeformat}})</span>
<div class="filename">{{file.name|escape|ellipsizedfilename:50|safe}}</div>
<div class="filename">{{file.ellipsed_name}}</div>
</a>
</h4>
{% endfor %}

View File

@ -116,14 +116,6 @@ def nonbreakinghyphen(value):
register.filter(nonbreakinghyphen)
def ellipsizedfilename(value, length=25):
if len(value) > length:
value = value[:length] + '&#8230;'
return value
register.filter(ellipsizedfilename)
@register.filter_function
def order_by(queryset, args):
args = [x.strip() for x in args.split(',')]