コード例 #1
0
ファイル: topic.py プロジェクト: Sanyam07/tildes
    def content_metadata_for_display(self) -> List[str]:
        """Return a list of the content's metadata strings, suitable for display."""
        if not self.content_type:
            return []

        metadata_strings = []

        fields = ContentMetadataFields.detail_fields_for_content_type(
            self.content_type)

        for field in fields:
            value = self.get_content_metadata(field.key)
            if not value:
                continue

            # only show published date if it's more than 3 days before the topic
            if field is ContentMetadataFields.PUBLISHED:
                published = utc_from_timestamp(value)
                if self.created_time - published < timedelta(days=3):
                    continue

            formatted_value = field.format_value(value)

            if field is ContentMetadataFields.PUBLISHED:
                formatted_value = f"published {formatted_value}"

            metadata_strings.append(formatted_value)

        return metadata_strings
コード例 #2
0
    def format_value(self, value: Any) -> str:
        """Format a value stored in this field into a string for display."""
        if self.name == "AUTHORS":
            return ", ".join(value)

        if self.name == "DURATION":
            delta = timedelta(seconds=value)

            # When converted to str, timedelta always includes hours and minutes,
            # so we want to strip off all the excess zeros and/or colons. However,
            # if it's less than a minute we'll need to add one back.
            duration_str = str(delta).lstrip("0:")
            if value < 60:
                duration_str = f"0:{duration_str}"

            return duration_str

        if self.name == "PUBLISHED":
            published = utc_from_timestamp(value)
            return published.strftime("%b %-d %Y")

        if self.name == "WORD_COUNT":
            if value == 1:
                return "1 word"

            if value >= 10_000:
                # dirty way of using non-breaking thin spaces as thousands separators
                word_count = f"{value:,}".replace(",", "\u202F")
            else:
                word_count = str(value)

            return f"{word_count} words"

        return str(value)
コード例 #3
0
ファイル: topic.py プロジェクト: talhadar90/bawajee
    def content_metadata_for_display(self) -> str:
        """Return a string of the content's metadata, suitable for display."""
        # pylint: disable=too-many-branches
        metadata_strings = []

        # display word count (if we have it) with either type of topic
        word_count = self.get_content_metadata("word_count")
        if word_count is not None:
            if word_count == 1:
                metadata_strings.append("1 word")
            else:
                metadata_strings.append(f"{word_count} words")

        if self.is_link_type:
            # display the duration if we have it
            duration = self.get_content_metadata("duration")
            if duration:
                duration_delta = timedelta(seconds=duration)

                # When converted to str, timedelta always includes hours and minutes,
                # so we want to strip off all the excess zeros and/or colons. However,
                # if it's less than a minute we'll need to add one back.
                duration_str = str(duration_delta).lstrip("0:")
                if duration < 60:
                    duration_str = f"0:{duration_str}"

                metadata_strings.append(duration_str)

            # display the published date if it's more than 3 days before the topic
            published_timestamp = self.get_content_metadata("published")
            if published_timestamp:
                published = utc_from_timestamp(published_timestamp)
                if self.created_time - published > timedelta(days=3):
                    metadata_strings.append(published.strftime("%b %-d %Y"))

        return ", ".join(metadata_strings)