Round up reading time and simplify implementation
This commit is contained in:
parent
7a4aaca82c
commit
83f5c3234b
1 changed files with 19 additions and 7 deletions
|
@ -1,15 +1,15 @@
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
from math import ceil
|
||||||
from typing import Any, Optional
|
from typing import Any, Optional
|
||||||
from urllib.parse import urlencode
|
from urllib.parse import urlencode
|
||||||
|
|
||||||
from django.contrib.humanize.templatetags.humanize import NaturalTimeFormatter
|
|
||||||
from django.core.paginator import EmptyPage, Paginator
|
from django.core.paginator import EmptyPage, Paginator
|
||||||
from django.core.paginator import Page as PaginatorPage
|
from django.core.paginator import Page as PaginatorPage
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.http.request import HttpRequest
|
from django.http.request import HttpRequest
|
||||||
from django.http.response import Http404, HttpResponse, HttpResponseBadRequest
|
from django.http.response import Http404, HttpResponse, HttpResponseBadRequest
|
||||||
from django.shortcuts import redirect
|
from django.shortcuts import redirect
|
||||||
from django.utils import timezone
|
from django.template.defaultfilters import pluralize
|
||||||
from django.utils.functional import cached_property, classproperty
|
from django.utils.functional import cached_property, classproperty
|
||||||
from django.utils.text import slugify
|
from django.utils.text import slugify
|
||||||
from wagtail.admin.panels import FieldPanel, MultiFieldPanel
|
from wagtail.admin.panels import FieldPanel, MultiFieldPanel
|
||||||
|
@ -116,16 +116,28 @@ class BaseContentPage(BasePage, MetadataMixin):
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def reading_time_display(self) -> str:
|
def reading_time_display(self) -> str:
|
||||||
return NaturalTimeFormatter.string_for(
|
reading_time_seconds = ceil(self.reading_time.total_seconds())
|
||||||
timezone.now() - self.reading_time
|
|
||||||
).removesuffix(" ago")
|
# Show nothing if under a minute. Probably won't be shown anyway
|
||||||
|
if reading_time_seconds < 60:
|
||||||
|
return ""
|
||||||
|
|
||||||
|
# If under an hour, show minutes
|
||||||
|
if reading_time_seconds < 3600:
|
||||||
|
minutes = ceil(reading_time_seconds / 60)
|
||||||
|
|
||||||
|
return f"{minutes} minute{pluralize(minutes)}"
|
||||||
|
|
||||||
|
# After that, show hours
|
||||||
|
hours = ceil(reading_time_seconds / 60 / 60)
|
||||||
|
return f"{hours} hour{pluralize(hours)}"
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def show_reading_time(self) -> bool:
|
def show_reading_time(self) -> bool:
|
||||||
"""
|
"""
|
||||||
Only show reading time if it's longer than 2 minutes
|
Only show reading time if it's longer than 2 minutes (rounded)
|
||||||
"""
|
"""
|
||||||
return self.reading_time.total_seconds() >= 120
|
return ceil(self.reading_time.total_seconds() / 60) >= 2
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def word_count(self) -> int:
|
def word_count(self) -> int:
|
||||||
|
|
Loading…
Reference in a new issue