Revert "Thoughts are sorted and displayed by UUID, not date"

This reverts commit 608c8cdcd2.
This commit is contained in:
Matthias Portzel 2023-07-01 12:54:16 -07:00
parent 608c8cdcd2
commit 7ee0e3980d
2 changed files with 82 additions and 65 deletions

View File

@ -3,8 +3,6 @@ import datetime
from django.utils.text import slugify
from django.utils.timezone import make_aware
from django.db.models import Q
from .models import Thought
class Page:
@ -16,71 +14,90 @@ class Page:
pass
# Okay, we're going to have some fun with this
# 8 pages, 01, 23, 45, 67, 89, AB, CD, EF
# Okay we're going to use some wack seasons here hold on
# Let's go meteorological seasons
# 1 - jan -> 1, 0
# 2 - feb -> 2, 0
# 3 - mar -> 3, 1, spring
# 4 - apl -> 4, 1
# 5 - may -> 5, 1
# 6 - jun -> 6, 2, summer
# 7 - jul -> 7, 2
# 8 - aug -> 8, 2
# 9 - sep -> 9, 3, fall
# 10 - oct -> 10, 3
# 11 - nov -> 11, 3
# 12 - dec -> 0, 0, winter
def season_for_date(date):
return (date.month % 12) // 3
def season_year_for_date(date):
return season_for_date(date), date.year - (1 if date.month in [1, 2] else 0)
def formatted_name_for_season_year(current_season, current_year):
return ["Winter", "Spring", "Summer", "Fall"][current_season] + " " + str(current_year)
# Exported
def get_page_slug(thought):
return ({
"0": "0-1",
"1": "0-1",
"2": "2-3",
"3": "2-3",
"4": "4-5",
"5": "4-5",
"6": "6-7",
"7": "6-7",
"8": "8-9",
"9": "8-9",
"a": "a-b",
"b": "a-b",
"c": "c-d",
"d": "c-d",
"e": "e-f",
"f": "e-f",
})[str(thought.uuid)[0]]
return slugify(formatted_name_for_season_year(*season_year_for_date(thought.posted)))
def formatted_name_for_slug(slug):
return ({
"0-1": "0-1",
"2-3": "2-3",
"4-5": "4-5",
"6-7": "6-7",
"8-9": "8-9",
"a-b": "A-B",
"c-d": "C-D",
"e-f": "E-F",
})[slug]
class RandomPage(Page):
def __init__(self, slug):
super().__init__(formatted_name_for_slug(slug))
self.slug = slug
class SeasonPage(Page):
def __init__(self, current_season, current_year):
super().__init__(formatted_name_for_season_year(current_season, current_year))
self.first_day_of_season = make_aware(datetime.datetime(
current_year,
12 if current_season == 0 else current_season * 3,
1
))
# If the current season is winter, then the next season starts on the next year
# This is actually the first day of the next season but that's hard to type out and it's 2am
self.last_day_of_season = make_aware(datetime.datetime(
current_year + (1 if current_season == 0 else 0),
12 if current_season == 3 else (current_season + 1) * 3,
1
))
def get_all_entries(self):
return Thought.objects.filter(
Q(uuid__startswith=self.slug[0]) |
Q(uuid__startswith=self.slug[-1])
).order_by("uuid")
return Thought.objects.order_by("-posted").filter(
posted__gte=self.first_day_of_season, # First month of this season
posted__lt=self.last_day_of_season # First month of next season
)
# Need to loop over all thoughts? and yield a new Page for each season
# Assume that if you're using this generator, you have at least 1 thought each
# season between the first and last
# We don't take into account timezone here. Because local time isn't monotonic increasing,
# it's ill-defined how to split up a list of items ordered by server time. This is annoying
# But the alternative is to call get_season for every thought and sort them into buckets
def season_pages():
ordered_thoughts = Thought.objects.order_by("posted")
first_thought = ordered_thoughts.first()
last_thought = ordered_thoughts.last()
current_season, current_year = season_year_for_date(first_thought.posted)
last_season, last_year = season_year_for_date(last_thought.posted)
while current_year < last_year or current_season != last_season:
yield SeasonPage(current_season, current_year)
if current_season == 0:
current_year += 1
current_season += 1
current_season = current_season % 4
yield SeasonPage(current_season, current_year)
# Exported
# Returns a list of `Page`s
# Where a Page has:
# .slug
# .formatted_name
# .get_all_entries
def get_all_pages():
return [
RandomPage("0-1"),
RandomPage("2-3"),
RandomPage("4-5"),
RandomPage("6-7"),
RandomPage("8-9"),
RandomPage("a-b"),
RandomPage("c-d"),
RandomPage("e-f"),
]
pages = list(season_pages())
pages.reverse()
return pages
# Where a Page has:
# .slug
# .formatted_name
# .get_all_entries
# return [Pages]

View File

@ -55,13 +55,13 @@
{% block main %}
<nav class="nav top" aria-label="History Navigation">
{% if not is_first_page %}
<span class="nav-item"><a href="?page={{ previous_page_slug }}">Previous</a></span>
<span class="nav-item"><a href="?page={{ previous_page_slug }}">Newer</a></span>
{% endif %}
<span class="nav-item"><span class="current-page">{{ page.formatted_name }}</span></span>
{% if not is_last_page %}
<span class="nav-item"><a href="?page={{ next_page_slug }}">Next</a></span>
<span class="nav-item"><a href="?page={{ next_page_slug }}">Older</a></span>
{% endif %}
</nav>
@ -81,13 +81,13 @@
<nav class="nav bottom" aria-label="History Navigation">
{% if not is_first_page %}
<span class="nav-item"><a href="?page={{ previous_page_slug }}">Previous</a></span>
<span class="nav-item"><a href="?page={{ previous_page_slug }}">Newer</a></span>
{% endif %}
<span class="nav-item"><span class="current-page">{{ page.formatted_name }}</span></span>
{% if not is_last_page %}
<span class="nav-item"><a href="?page={{ next_page_slug }}">Next</a></span>
<span class="nav-item"><a href="?page={{ next_page_slug }}">Older</a></span>
{% endif %}
</nav>
{% endblock %}