From ef0db4c727b2748ce68283c2fdd42c84b564d491 Mon Sep 17 00:00:00 2001 From: Tasos Katsoulas Date: Fri, 13 Sep 2024 13:44:54 +0300 Subject: [PATCH] Display different date formats based on locale --- .../products/jinja2/products/documents.html | 4 +-- kitsune/products/views.py | 3 -- kitsune/settings.py | 2 +- kitsune/sumo/templatetags/jinja_helpers.py | 29 ++++++++++--------- kitsune/wiki/jinja2/wiki/document.html | 11 +++++-- .../jinja2/wiki/includes/document_macros.html | 18 +++++++----- kitsune/wiki/views.py | 13 ++++----- 7 files changed, 44 insertions(+), 36 deletions(-) diff --git a/kitsune/products/jinja2/products/documents.html b/kitsune/products/jinja2/products/documents.html index 7ef338dc9..4354b2806 100644 --- a/kitsune/products/jinja2/products/documents.html +++ b/kitsune/products/jinja2/products/documents.html @@ -2,7 +2,7 @@ {% from 'products/includes/topic_macros.html' import topic_sidebar, topic_tabs %} {% from 'products/includes/product_macros.html' import product_dropdown %} {% from 'includes/common_macros.html' import download_firefox, sumo_banner %} -{% from 'wiki/includes/document_macros.html' import document_metadata %} +{% from 'wiki/includes/document_macros.html' import document_metadata with context %} {% from "questions/includes/aaq_macros.html" import aaq_widget %} {% if product %} @@ -72,7 +72,7 @@

{{ document['document_summary'] }}

- {{ document_metadata(document['created'], document['is_past_thirty_days'], document['is_first_revision'], document['product_titles'], helpful_votes=None, metadata_type="list") }} + {{ document_metadata(document['created'], document['is_first_revision'], document['product_titles'], helpful_votes=None, metadata_type="list") }} {% endfor %} diff --git a/kitsune/products/views.py b/kitsune/products/views.py index 89eda61ce..bbce691f1 100644 --- a/kitsune/products/views.py +++ b/kitsune/products/views.py @@ -1,5 +1,4 @@ import json -from datetime import datetime, timedelta from django.conf import settings from django.db.models import Exists, OuterRef, Q @@ -152,9 +151,7 @@ def document_listing(request, topic_slug, product_slug=None, subtopic_slug=None) documents, fallback_documents = documents_for(request.user, **doc_kw) - thirty_days_ago = datetime.now() - timedelta(days=30) for document in documents: - document["is_past_thirty_days"] = document["created"] < thirty_days_ago document["is_first_revision"] = ( Revision.objects.filter(document=document["id"], is_approved=True).count() == 1 ) diff --git a/kitsune/settings.py b/kitsune/settings.py index cae8db032..e5413c50f 100644 --- a/kitsune/settings.py +++ b/kitsune/settings.py @@ -642,7 +642,7 @@ INSTALLED_APPS: tuple[str, ...] = ( "django.contrib.sites", "django.contrib.messages", "django.contrib.staticfiles", - "django_jinja.contrib._humanize", + "django_jinja", "graphene_django", "mozilla_django_oidc", "corsheaders", diff --git a/kitsune/sumo/templatetags/jinja_helpers.py b/kitsune/sumo/templatetags/jinja_helpers.py index 9d51daa5f..f82a12c23 100644 --- a/kitsune/sumo/templatetags/jinja_helpers.py +++ b/kitsune/sumo/templatetags/jinja_helpers.py @@ -8,16 +8,17 @@ from zoneinfo import ZoneInfo import bleach import jinja2 import wikimarkup.parser -from babel import localedata from babel.dates import format_date, format_datetime, format_time from babel.numbers import format_decimal from django.conf import settings +from django.contrib.humanize.templatetags.humanize import naturaltime from django.http import QueryDict from django.template.loader import render_to_string from django.templatetags.static import static as django_static from django.utils.encoding import smart_bytes, smart_str from django.utils.http import urlencode from django.utils.timezone import get_default_timezone, is_aware, is_naive +from django.utils.timezone import now as django_now from django.utils.translation import gettext as _ from django.utils.translation import gettext_lazy as _lazy from django.utils.translation import ngettext @@ -231,15 +232,12 @@ def _babel_locale(locale): def _contextual_locale(context): """Return locale from the context, falling back to a default if invalid.""" request = context.get("request") - locale = request.LANGUAGE_CODE - if not localedata.exists(locale): - locale = settings.LANGUAGE_CODE - return locale + return request.LANGUAGE_CODE if request else settings.LANGUAGE_CODE @jinja2.pass_context @library.global_function -def datetimeformat(context, value, format="shortdatetime"): +def datetimeformat(context, value, format="shortdatetime", use_naturaltime=False): """ Returns a formatted date/time using Babel's locale settings. Uses the timezone from settings.py, if the user has not been authenticated. @@ -274,18 +272,21 @@ def datetimeformat(context, value, format="shortdatetime"): convert_value = new_value.astimezone(convert_tzinfo) locale = _babel_locale(_contextual_locale(context)) - # If within a day, 24 * 60 * 60 = 86400s - if format == "shortdatetime": + if use_naturaltime and (django_now().astimezone(convert_tzinfo) - convert_value).days < 30: + return naturaltime(convert_value) + + if format == "shortdatetime" or format == "shortdate": # Check if the date is today today = datetime.datetime.now(tz=convert_tzinfo).toordinal() + kwargs = {"format": "short", "tzinfo": convert_tzinfo, "locale": locale} if convert_value.toordinal() == today: - formatted = _lazy("Today at %s") % format_time( - convert_value, format="short", tzinfo=convert_tzinfo, locale=locale - ) + formatted = _lazy("Today at %s") % format_time(convert_value, **kwargs) else: - formatted = format_datetime( - convert_value, format="short", tzinfo=convert_tzinfo, locale=locale - ) + if format == "shortdatetime": + formatted = format_datetime(convert_value, **kwargs) + else: + del kwargs["tzinfo"] + formatted = format_date(convert_value, **kwargs) elif format == "longdatetime": formatted = format_datetime( convert_value, format="long", tzinfo=convert_tzinfo, locale=locale diff --git a/kitsune/wiki/jinja2/wiki/document.html b/kitsune/wiki/jinja2/wiki/document.html index d6a7a461f..9c471cdf9 100644 --- a/kitsune/wiki/jinja2/wiki/document.html +++ b/kitsune/wiki/jinja2/wiki/document.html @@ -1,7 +1,14 @@ {% extends "wiki/base.html" %} {% from "wiki/includes/sidebar_modules.html" import document_tools, document_notifications, related_products_sidebar with context %} -{% from "wiki/includes/document_macros.html" import contributor_list, document_title, document_messages, document_metadata, document_content, inpage_contact_cta, topic_list, related_documents %} +{% from "wiki/includes/document_macros.html" import contributor_list, + document_title, + document_messages, + document_content, + inpage_contact_cta, + topic_list, + related_documents, + document_metadata with context %} {% from "wiki/includes/document_macros.html" import vote_form with context %} {% from "includes/common_macros.html" import download_firefox, sumo_banner %} {% from "questions/includes/aaq_macros.html" import aaq_widget %} @@ -87,7 +94,7 @@
{{ document_messages(document, redirected_from) }} {% if not document.is_archived %} - {{ document_metadata(document.current_revision.created, is_past_thirty_days, is_first_revision, product_titles, helpful_votes, metadata_type="document") }} + {{ document_metadata(document.current_revision.created, is_first_revision, product_titles, helpful_votes, metadata_type="document") }} {% endif %} {{ document_content(document, fallback_reason, request, settings, document_css_class, any_localizable_revision, full_locale_name) }} diff --git a/kitsune/wiki/jinja2/wiki/includes/document_macros.html b/kitsune/wiki/jinja2/wiki/includes/document_macros.html index 093a402f8..c8016e2bf 100644 --- a/kitsune/wiki/jinja2/wiki/includes/document_macros.html +++ b/kitsune/wiki/jinja2/wiki/includes/document_macros.html @@ -12,7 +12,7 @@

{{ document.title }}

{%- endmacro %} -{% macro document_metadata(created_date, is_past_thirty_days, is_first_revision, product_titles, helpful_votes, metadata_type) -%} +{% macro document_metadata(created_date, is_first_revision, product_titles, helpful_votes, metadata_type) -%} {% set truncate_length = 25 if helpful_votes else 50 %} {%- endmacro %} diff --git a/kitsune/wiki/views.py b/kitsune/wiki/views.py index 036192f7f..2a1bcb01b 100644 --- a/kitsune/wiki/views.py +++ b/kitsune/wiki/views.py @@ -3,7 +3,6 @@ import logging import time from datetime import datetime from datetime import time as datetime_time -from datetime import timedelta from functools import wraps from django.conf import settings @@ -15,7 +14,12 @@ from django.core.exceptions import PermissionDenied from django.db.models import Count, Exists, OuterRef, Q from django.db.models.functions import Now, TruncDate from django.forms.utils import ErrorList -from django.http import Http404, HttpResponse, HttpResponseBadRequest, HttpResponseRedirect +from django.http import ( + Http404, + HttpResponse, + HttpResponseBadRequest, + HttpResponseRedirect, +) from django.shortcuts import get_object_or_404, redirect, render from django.template.loader import render_to_string from django.utils.cache import patch_vary_headers @@ -294,15 +298,10 @@ def document(request, document_slug, document=None): else 0 ) - is_past_thirty_days = False - if doc.current_revision: - is_past_thirty_days = doc.current_revision.created < (datetime.now() - timedelta(days=30)) - is_first_revision = doc.revisions.filter(is_approved=True).count() == 1 data = { "document": doc, - "is_past_thirty_days": is_past_thirty_days, "is_first_revision": is_first_revision, "redirected_from": redirected_from, "contributors": contributors,