From ae32c9d600c31002b224c75a86e0dbfbf5e3aea2 Mon Sep 17 00:00:00 2001 From: Deimos Date: Sat, 30 Nov 2019 13:34:49 -0700 Subject: [PATCH] Show content metadata on link topic comments pages Very basic version - should probably exclude some of the fields (like domain) and do some other changes in addition to this, but it's a reasonable start. --- tildes/scss/modules/_topic.scss | 29 ++++++++++++++++++++++++++++ tildes/tildes/enums.py | 5 +++++ tildes/tildes/models/topic/topic.py | 17 ++++++++++++++++ tildes/tildes/templates/topic.jinja2 | 17 ++++++++++++++++ 4 files changed, 68 insertions(+) diff --git a/tildes/scss/modules/_topic.scss b/tildes/scss/modules/_topic.scss index f2f71ca..ead1c12 100644 --- a/tildes/scss/modules/_topic.scss +++ b/tildes/scss/modules/_topic.scss @@ -268,6 +268,35 @@ font-size: 0.6rem; } +.topic-full-content-metadata { + margin: 1rem 0 1rem 1rem; + + h2 { + font-size: 0.8rem; + } + + dl { + display: grid; + grid-template-columns: max-content minmax(auto, $paragraph-max-width); + align-items: baseline; + + margin: 0; + } + + dt { + grid-column-start: 1; + padding: 0.2rem; + font-size: 0.6rem; + } + + dd { + grid-column-start: 2; + margin: 0; + padding: 0.2rem; + font-size: 0.6rem; + } +} + .topic-full-tags { margin-bottom: 0.4rem; font-size: 0.6rem; diff --git a/tildes/tildes/enums.py b/tildes/tildes/enums.py index bca8353..62c1eb4 100644 --- a/tildes/tildes/enums.py +++ b/tildes/tildes/enums.py @@ -100,6 +100,11 @@ class ContentMetadataFields(enum.Enum): """Return the key to store this field under.""" return self.name.lower() + @property + def display_name(self) -> str: + """Return the field's name in a format more suitable for display.""" + return self.name.replace("_", " ").capitalize() + @classmethod def detail_fields_for_content_type( cls, content_type: "TopicContentType", diff --git a/tildes/tildes/models/topic/topic.py b/tildes/tildes/models/topic/topic.py index 9c7a575..3921c7d 100644 --- a/tildes/tildes/models/topic/topic.py +++ b/tildes/tildes/models/topic/topic.py @@ -519,6 +519,23 @@ class Topic(DatabaseModel): return ", ".join(metadata_strings) + @property + def content_metadata_fields_for_display(self) -> Dict[str, str]: + """Return a dict of the metadata fields and values, suitable for display.""" + if not self.content_metadata: + return {} + + output_fields = {} + for field_name, value in self.content_metadata.items(): + try: + field = ContentMetadataFields[field_name.upper()] + except KeyError: + continue + + output_fields[field.display_name] = field.format_value(value) + + return output_fields + @property def content_excerpt(self) -> Optional[str]: """Return the topic's content excerpt (if it has one).""" diff --git a/tildes/tildes/templates/topic.jinja2 b/tildes/tildes/templates/topic.jinja2 index c6d9fe6..b8d8933 100644 --- a/tildes/tildes/templates/topic.jinja2 +++ b/tildes/tildes/templates/topic.jinja2 @@ -79,6 +79,10 @@ {% if topic.additional_content_html %}
{{ topic.additional_content_html|safe }}
{% endif %} + + {% if topic.content_metadata %} + {{ content_metadata(topic) }} + {% endif %} {% endif %} {% endif %} @@ -300,3 +304,16 @@ {% endif %} {% endblock %} + +{% macro content_metadata(topic) %} +
+

Link information

+

This data is scraped automatically and may be incorrect.

+
+ {% for field, value in topic.content_metadata_fields_for_display.items() %} +
{{ field }}
+
{{ value }}
+ {% endfor %} +
+
+{% endmacro %}