def post_loop(max_posts): posts = Post.objects.filter( status__title="Published").order_by('-post_date')[:max_posts] output = "" for x in range(0, len(posts)): post = posts[x] permalink = functions.permalink(post.category.slug, post.slug) summary = Truncator(post.content_html) properties = { 'TITLE': post.title, 'TITLE_LINK': functions.make_link(permalink, post.title), 'PERMALINK': permalink, 'BODY': mark_safe(summary.words(100, html=True)), 'CATEGORY': post.category, 'POST_DATE': post.post_date, 'CREATED_DATE': post.created, 'LAST_EDIT_DATE': post.last_edited, 'AUTHOR': post.author, 'LAST_EDITOR': post.last_editor, } output += render_to_string( functions.get_template_file_path("postloop"), properties) return output
def find_post_content(feed_obj, entry): """Find the correct content field for a post.""" try: content = entry["content"][0]["value"] except (IndexError, KeyError): content = entry.get("description") or entry.get("summary") or "" if '<img' not in content: # if there's no image and the we add an image to the feed def build_img(img_dict): try: # The tag is url instead of src... pain img = "<img src='%s'" % img_dict.get("url") except KeyError: return '' img_dict.pop('url') for attr in img_dict.items(): img += "%s='%s'" % (attr[0], attr[1]) img += ">" return img try: thumbnail = entry["media_thumbnail"][0] img = build_img(thumbnail) except (IndexError, KeyError): img = "" content = img + content try: truncator = Truncator(text=content) content =truncator.words(num=conf.DEFAULT_ENTRY_WORD_LIMIT, html=True) except UnicodeDecodeError: content = "" return feed_content_optimizer.optimize(content)
def offers(doc, categories): queryset = Product.objects.filter(is_visible=True, is_retail=True, quantity__gt=0, price__gt=0) doc.write('<offers>') for product in queryset: try: category_id = product.categories.filter(parent__isnull=False, id__in=categories).values_list('id', flat=True)[0] except IndexError: try: category_id = product.categories.filter(parent__isnull=True, id__in=categories).values_list('id', flat=True)[0] except IndexError: continue text = product.description.strip() if text: text = strip_tags(strip_spaces_between_tags(product.description)).strip() text = typograph(WHITESPACE_RE.sub(' ', text)) truncator = Truncator(text) text = truncator.chars(512) doc.write('<offer>') doc.write('<url>http://tomat-podarky.ru{}</url>'.format(product.get_absolute_url())) doc.write('<price>{}</price>'.format(product.price)) doc.write('<currencyId>RUR</currencyId>') doc.write('<categoryId>{}</categoryId>'.format(category_id)) doc.write('<delivery>true</delivery>') doc.write('<name>') doc.write(_(typograph(product.title))) doc.write('</name>') if text: doc.write('<description>') doc.write(_(text)) doc.write('</description>') doc.write('</offer>\n') doc.write('</offers>')
def save_plugin_data(self, request=None): """Save plugin data. We want to save the generated lorem ipsum text for later use. Thus, although we don't show it to the user, in case when ``generate_lipsum`` field is set to True, we silently generate the text and save it into the plugin data. """ if self.cleaned_data.get('generate_lipsum', None): lipsum_language = self.cleaned_data.get('lipsum_language', None) try: if lipsum_language in LANGUAGE_CHOICES_KEYS: if lipsum_language == 'en': gen = Generator() else: gen = TranslipsumGenerator( language_code=lipsum_language ) text = gen.generate_paragraph() truncator = Truncator(text) self.cleaned_data['text'] = truncator.chars( self.cleaned_data.get('lipsum_max_chars', DEFAULT_MAX_CHARS) ) except Exception as err: if DEBUG: logger.debug(err)
def tooltip_ellipsis(source, length=0): ''' return the plain text representation of markdown encoded text. That is the texted without any html tags. If ``length`` is 0 then it will not be truncated.''' try: length = int(length) except ValueError: # invalid literal for int() return source # Fail silently. ellipsis = '<a href rel="tooltip" title="{0}">...</a>'.format(source) truncated = Truncator(source).chars(length + 2, truncate='{...}') return mark_safe(truncated.replace('{...}', ellipsis))
def _get_teaser(self): """ Retrieve some part of the article or the article's description. """ if not self._teaser: if len(self.description.strip()): self._teaser = self.description else: truncator = Truncator(self.rendered_content) self._teaser = truncator.words(WORD_LIMIT, html=True) return self._teaser
def scrape_data(long_url): hdr = {"User-Agent": "Mozilla/5.0"} req = urllib2.Request(long_url, headers=hdr) response = urllib2.urlopen(req) soup = BeautifulSoup(response, "html.parser") title = soup.title.string meta_tags = soup.findAll("meta", {"property": "og:description"}) og_desc = meta_tags[0].get("content", "No description") description = Truncator(og_desc).chars(200) return {"title": title.encode("utf-8"), "description": description.encode("utf-8")}
def avoid_truncated_word(self, text): """Truncate in a way that text will be shorter than max_length and won't be cut in the middle of a word""" words = text.split() if not words: return text truncator = Truncator(text) last_word = text.split()[-1] text = truncator.chars(self.max_length, '') truncated_last_word = text.split()[-1] if truncated_last_word != last_word: # last word is cut. So, remove it num_words = len(text.split()) text = truncator.words(num_words - 1) return text
def popover_ellipsis(source, length=0): ''' return the plain text representation of markdown encoded text. That is the texted without any html tags. If ``length`` is 0 then it will not be truncated.''' try: length = int(length) except ValueError: # invalid literal for int() return source # Fail silently. ellipsis = ('<a href rel="popover" data-content="{0}" ' 'data-trigger="hover" data-container="body">...</a>').format(escape(source)) truncated = Truncator(strip_tags(source)).chars(length) nb_words = len(truncated.split(' ')) html_truncated = Truncator(source).words(nb_words, html=True, truncate='{...}') return mark_safe(html_truncated.replace('{...}', ellipsis))
def truncate_chars(data, maxlen): """ Truncate string to at most ``maxlen``, including elipsis. * uses django.utils.text Truncator class. :param data: string to truncate :param maxlen: length to truncate to :returns: string (truncated if necessary) """ from django.utils.text import Truncator if isinstance(data, six.text_type) and len(data) > maxlen: truncator = Truncator(data) data = truncator.chars(maxlen) return data
def describe_operation(operation, backwards): """Return a string that describes a migration operation for --plan.""" prefix = '' if hasattr(operation, 'code'): code = operation.reverse_code if backwards else operation.code action = code.__doc__ if code else '' elif hasattr(operation, 'sql'): action = operation.reverse_sql if backwards else operation.sql else: action = '' if backwards: prefix = 'Undo ' if action is None: action = 'IRREVERSIBLE' is_error = True else: action = str(action).replace('\n', '') is_error = False if action: action = ' -> ' + action truncated = Truncator(action) return prefix + operation.describe() + truncated.chars(40), is_error
def import_entry(self, title, content, item_node): """ Importing an entry but some data are missing like related entries, start_publication and end_publication. start_publication and creation_date will use the same value, wich is always in Wordpress $post->post_date. """ try: creation_date = datetime.strptime( item_node.find('{%s}post_date_gmt' % WP_NS).text, '%Y-%m-%d %H:%M:%S') except: creation_date = datetime.now() if settings.USE_TZ: creation_date = timezone.make_aware( creation_date, pytz.timezone('GMT')) excerpt = item_node.find('{%sexcerpt/}encoded' % WP_NS).text if excerpt: excerpt = strip_tags(excerpt) else: if self.auto_excerpt: excerpt = Truncator(strip_tags(content)).words(50) else: excerpt = '' # Prefer use this function than # item_node.find('{%s}post_name' % WP_NS).text # Because slug can be not well formated slug = slugify(title)[:255] or 'post-%s' % item_node.find( '{%s}post_id' % WP_NS).text entry_dict = { 'title': title, 'content': content.encode('ascii', 'xmlcharrefreplace'), 'excerpt': excerpt.encode('ascii', 'xmlcharrefreplace'), 'tags': ', '.join(self.get_entry_tags(item_node.findall( 'category')))[:255], 'status': self.REVERSE_STATUS[item_node.find( '{%s}status' % WP_NS).text], 'comment_enabled': item_node.find( '{%s}comment_status' % WP_NS).text == 'open', 'pingback_enabled': item_node.find( '{%s}ping_status' % WP_NS).text == 'open', 'featured': item_node.find('{%s}is_sticky' % WP_NS).text == '1', 'password': item_node.find('{%s}post_password' % WP_NS).text or '', 'login_required': item_node.find( '{%s}status' % WP_NS).text == 'private', 'last_update': timezone.now()} entry_dict['trackback_enabled'] = entry_dict['pingback_enabled'] entry, created = Entry.objects.get_or_create( slug=slug, creation_date=creation_date, defaults=entry_dict) if created: entry.categories.add(*self.get_entry_categories( item_node.findall('category'))) entry.authors.add(self.authors[item_node.find( '{http://purl.org/dc/elements/1.1/}creator').text]) entry.sites.add(self.SITE) return entry, created
def item_description(self, item): truncator = Truncator(item.summary) return truncator.words(75, html=True)
def __str__(self): truncated_message = Truncator(self.message) return truncated_message.chars(30) + " TS: " + str(self.time_stamp)
def visible_name(self): if self.name: return self.name length = getattr(settings, 'REPLY_TEMPLATE_VISIBLE_NAME_LENGTH', 15) return Truncator(self.content).chars(length)
def __str__(self): truncated_message = Truncator(self.message) return truncated_message.chars(30)
def truncContenu(self, commentaire): return Truncator(commentaire.contenu).chars(60, truncate='...')
def get_truncated_task(self): truncated_message = Truncator(self.task) return truncated_message.chars(25)
def truncated_message_id(self, instance): if instance.message_id: return Truncator(instance.message_id[1:-1]).chars(10) return str(instance.id)
def __str__(self): content = Truncator(strip_tags(self.code)).chars(30, html=True) return mark_safe(content)
def get_short_description(self, obj): t = Truncator(obj.post) return t.words(30, html=True)
def upload(self, data, owner): """Use cleaned form data to populate an unsaved upload record. Once, each upload was just one file, so all we had to do was attach its name and type to the upload, where we could display it. Since multifile and geopackage supported were added, the required behavior depends on what we find in the upload. That is what this method is for. For example, it looks at an upload and tries to come up with a reasonably mnemonic name, or else set name to None to mean there was no obvious option. """ from osgeo_importer.models import UploadedData # Do not save here, we want to leave control of that to the caller. upload = UploadedData.objects.create(user=owner) # Get a list of paths for files in this upload. paths = [item.name for item in data] # If there are multiple paths, see if we can boil them down to a # smaller number of representative paths (ideally, just one). if len(paths) > 1: # Group paths by common pre-extension prefixes groups = collections.defaultdict(list) for path in paths: group_name = os.path.splitext(path)[0] groups[group_name].append(path) # Check each group for "leaders" - a special filename like "a.shp" # which can be understood to represent other files in the group. # Map from each group name to a list of leaders in that group. leader_exts = ["shp"] group_leaders = {} for group_name, group in groups.items(): leaders = [ path for path in group if any( path.endswith(ext) for ext in leader_exts) ] if leaders: group_leaders[group_name] = leaders # Rebuild paths: leaders + paths without leaders to represent them leader_paths = [] for leaders in group_leaders.values(): leader_paths.extend(leaders) orphan_paths = [] for group_name, group in groups.items(): if group_name not in group_leaders: orphan_paths.extend(group) paths = leader_paths + orphan_paths max_length = UploadedData._meta.get_field('name').max_length # If no data, just return the instance. if not paths: name = None file_type = None # If we just have one path, that's a reasonable name for the upload. # We want this case to happen as frequently as reasonable, so that we # can set one reasonable name and file_type for the whole upload. elif len(paths) == 1: path = paths[0] basename = os.path.basename(path) name = Truncator(basename).chars(max_length) file_type = self.get_file_type(path) # Failing that, see if we can represent all the important paths within # the available space, making a meaningful mnemonic that isn't # misleading even though there isn't one obvious name. But if we can't # even do that, no use generating misleading or useless strings here, # just pass a None and let the frontend handle the lack of information. else: basenames = sorted(os.path.basename(path) for path in paths) name = ", ".join(basenames) if len(name) > max_length: logger.warning( "rejecting upload name for length: {0!r} {1} > {2}".format( name, len(name), max_length)) name = None file_type = None upload.name = name upload.file_type = file_type return upload
def truncate_words(s, num, end_text='...'): # truncate_words was removed in Django 1.5. truncate = end_text and ' %s' % end_text or '' return Truncator(s).words(num, truncate=truncate)
def result_item(self, obj, field_name, row): """ 返回某一对象某一列的数据, :class:`ResultItem` 实例. :param obj: Model 对象 :param field_name: 列的名字 :param row: :class:`ResultHeader` 实例 """ item = ResultItem(field_name, row) # 首先初始化 field_name_split = field_name.split('.') field_name = field_name_split[0] try: f, attr, value = lookup_field(field_name, obj, self) except (AttributeError, ObjectDoesNotExist): item.text = mark_safe("<span class='text-muted'>%s</span>" % EMPTY_CHANGELIST_VALUE) else: if f is None: # Model 属性或是 OptionClass 属性列 item.allow_tags = getattr(attr, 'allow_tags', False) boolean = getattr(attr, 'boolean', False) if boolean: item.allow_tags = True item.text = boolean_icon(value) else: item.text = smart_unicode(value) else: # 处理关联咧 if isinstance(f.rel, models.ManyToOneRel): field_val = getattr(obj, f.name) if field_val is None: item.text = mark_safe("<span class='text-muted'>%s</span>" % EMPTY_CHANGELIST_VALUE) else: if len(field_name_split)>1: item.text = getattr(field_val,field_name_split[1]) else: item.text = field_val else: item.text = display_for_field(value, f) if isinstance(f, models.DateField)\ or isinstance(f, models.TimeField)\ or isinstance(f, models.ForeignKey): item.classes.append('nowrap') item.field = f item.attr = attr item.value = value # 如果没有指定 ``list_display_links`` , 使用第一列作为内容连接列. if (item.row['is_display_first'] and not self.list_display_links) \ or field_name in self.list_display_links: item.row['is_display_first'] = False item.is_display_link = True if self.list_display_links_details: item_res_uri = self.model_admin_url("detail", getattr(obj, self.pk_name)) if item_res_uri: edit_url = self.model_admin_url("change", getattr(obj, self.pk_name)) item.wraps.append('<a data-res-uri="%s" data-edit-uri="%s" class="details-handler" rel="tooltip" title="%s">%%s</a>' % (item_res_uri, edit_url, _(u'Details of %s') % str(obj))) else: url = self.get_object_url(obj) if self.pop: if 's' in self.request.GET: show = getattr(obj, self.request.GET.get('s')) if callable(show):show = show() else: show = escape(Truncator(obj).words(14, truncate='...')) show = str(show).replace('%','%%').replace("\'","\\\'") pop = format_html(' class="for_multi_select" show="{0}" sid="{1}" ', show, getattr(obj, self.request.GET.get('t')) ) else: pop = '' item.wraps.append(u'<a href="%s" %s>%%s</a>' % (url, pop)) return item
def get_message_title(message, language): conversation = message.conversation author_name = message.author.display_name type = conversation.type() if message.is_thread_reply(): thread_start = Truncator(message.thread.content).chars(num=15) return '{} / {}'.format(thread_start, author_name) if type == 'group': return '{} / {}'.format(conversation.target.name, author_name) if type == 'place': return '{} / {}'.format(conversation.target.name, author_name) if type == 'pickup': pickup = conversation.target group_tz = pickup.place.group.timezone with timezone.override(group_tz): weekday = format_date( pickup.date.start.astimezone(timezone.get_current_timezone()), 'EEEE', locale=translation.to_locale(language), ) time = format_time( pickup.date.start, format='short', locale=translation.to_locale(language), tzinfo=timezone.get_current_timezone(), ) short_date = '{} {}'.format(weekday, time) short_name = _('Pickup %(date)s') % { 'date': short_date, } return '{} / {}'.format(short_name, author_name) if type == 'application': application = conversation.target applicant_name = application.user.display_name if applicant_name == '': applicant_name = '(?)' emoji = '❓' if application.status == ApplicationStatus.ACCEPTED.value: emoji = '✅' elif application.status == ApplicationStatus.DECLINED.value: emoji = '❌' elif application.status == ApplicationStatus.WITHDRAWN.value: emoji = '🗑️' application_title = '{} {}'.format(emoji, applicant_name) if message.author == application.user: return application_title else: return '{} / {}'.format(application_title, author_name) if type == 'issue': issue = conversation.target if message.author == issue.affected_user: return '☹️ {}'.format(author_name) return '☹️ {} / {}'.format(issue.affected_user, author_name) return author_name
def truncate_words(s, num): from django.utils.text import Truncator return Truncator(s).words(num, truncate=" ...")
def __str__(self): return Truncator(self.content).words(6, truncate="...")
def markup(text, truncate=None): if truncate is not None: t = Truncator(text) text = t.chars(truncate) return markdown(text, safe_mode=False)
def _render_label(self, name, value): return self.label_format % ( name, escape(Truncator(value).words(14, truncate='...')))
def __str__(self): return Truncator(self.code).words(20)
def label_for_value(self, other_model, rel_name, value): try: obj = other_model._default_manager.get(**{rel_name: value}) return '%s' % escape(Truncator(obj).words(14, truncate='...')) except (ValueError, other_model.DoesNotExist): return ""
def __str__(self): return Truncator(self.content).chars(89)
def description_shortened(self, instance): return Truncator(instance.description.split('\n')[0]).chars(200)
# -*- coding: utf-8 -*- from django.contrib import admin #from django.utils.text import truncate_words from django.core import urlresolvers from django.utils.html import escape try: from import_export.admin import ImportExportModelAdmin AdminClass = admin.ModelAdmin except: AdminClass = admin.ModelAdmin from django.utils.text import Truncator truncate_words = lambda value, length: Truncator(value).words(length, html=True) from chant.models import * def uni_tr_10(field_name): def func(obj): return truncate_words(unicode(getattr(obj, field_name)), 10) func.short_description = field_name func.admin_order_field = field_name return func def uni_fk_tr_10(field_name, order_field=None): fnparts = field_name.split('__')
def truncDescription(self, post): return Truncator(post.description).chars(100, truncate='...')
def __str__(self): return "{0}>{1}:{2}".format(self.obfuscated_sender, self.obfuscated_recipient, Truncator(self.subject).words(5))
def label_for_value(self, value): key = self.rel.get_related_field().name obj = self.rel.to._default_manager.get(**{key: value}) return Truncator(obj).words(14, truncate='...')
def apercu_contenu(self, task): raise Truncator(task.description).chars(40, truncate='...')
def title (self): truncator = Truncator(self.topics) return truncator.words(12)
def __str__(self): truncated_message = Truncator(self.text) return truncated_message.chars(20)
def __unicode__(self): truncator = Truncator(self.message) return u'%s: %s' % (self.id, truncator.chars(20))
def __unicode__(self): return Truncator(strip_tags(self.label).replace('­', '')).words( 5, truncate="...")
def test_string_formatting(self): """ Test the human representation of a message """ message = Message.objects.get(pk=1) truncated_body = Truncator.words(message.body, 10) self.failUnlessEqual(message.__unicode__(), truncated_body)
def log_name(self): # Required by task_log return Truncator(self.uri).chars(32)
def __unicode__(self): if self.wrapper: text = Truncator(u'%s: %s' % (self.wrapper, strip_tags(self.body))) else: text = Truncator(strip_tags(self.body)) return Truncator(text.words(7)).chars(38)
def title(self): truncator = Truncator(self.topics) return truncator.words(12)
def get_item_notification(notification): ''' This filter return info about one notification of one user ''' idobject = notification.idobject is_comment = notification.is_comment html = "" if is_comment: try: comment = Comment.objects.get(idcomment=idobject) forum = comment.topic.forum.name slug = comment.topic.slug idtopic = comment.topic.idtopic description = Truncator(comment.description).chars(100) username = comment.user.username url_topic = "/topic/" + forum + "/" + \ slug + "/" + str(idtopic) + "/" title = "<h5><a href='"+url_topic+"'><u>" + \ comment.topic.title+"</u></h5></a>" description = "<p>"+description+"</p>" # Get params for url profile try: params = "" params = get_params_url_profile(comment.user) except Exception: params = "" # Data profile profile = get_id_profile(comment.user.id) photo = get_photo_profile(profile) if photo: path_img = settings.MEDIA_URL + str(photo) else: path_img = static("img/profile.png") url_profile = URL_PROFILE if params: user = "******"+url_profile+params + \ "'><p>" + username + "</p></a>" else: user = "******" + username + "</a>" date = get_datetime_topic(notification.date) # Notificacion html += '<div class="list-group">' html += ' <div class="list-group-item">' html += ' <div class="row-action-primary">' html += ' <img src="'+path_img + \ '" width=30 height=30 class="img-circle" />' html += ' </div>' html += ' <div class="row-content">' html += ' <div class="least-content">'+date+'</div>' html += ' <h4 class="list-group-item-heading">' + \ title.encode('utf8')+'</h4>' html += ' <p class="list-group-item-text">' + \ description.encode('utf8')+'</p>' html += ' <p>'+user.encode('utf8')+'</p>' html += ' </div>' html += ' </div>' html += ' <div class="list-group-separator"></div>' html += '</div>' except Comment.DoesNotExist: html = "" else: html = "" return html
def __unicode__(self): """ Human representation, displaying first ten words of the body. """ truncator = Truncator(self.body) truncated_body = truncator.words(10) return "%(truncated_body)s" % {'truncated_body': truncated_body}
def __str__(self): return Truncator(strip_tags(self.body)).words(6, truncate="...")
def ellipse_text(text, max_chars): truncate = Truncator(text) return truncate.chars(max_chars, u"\u2026")
def truncate_words(s, num, end_text='...'): truncate = end_text and ' %s' % end_text or '' return Truncator(s).words(num, truncate=truncate)
def get_meta_description(self): about = Truncator(self.about).words(35) return f"{self.full_name} - {self.role}. {about}"
def label_for_value(self, value): key = self.rel.get_related_field().name obj = self.rel.to._default_manager.get(**{key: value}) trunc = Truncator(obj) return trunc.words(obj, 14)