Paginate Gemini index page
This commit is contained in:
parent
b4b20eca12
commit
b19bbfe905
|
@ -1,4 +1,5 @@
|
||||||
import os
|
import os
|
||||||
|
from urllib.parse import parse_qs
|
||||||
|
|
||||||
os.environ["DJANGO_SETTINGS_MODULE"] = "whispermaphone.settings"
|
os.environ["DJANGO_SETTINGS_MODULE"] = "whispermaphone.settings"
|
||||||
|
|
||||||
|
@ -7,7 +8,9 @@ from django.template.loader import render_to_string
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
|
||||||
django.setup()
|
django.setup()
|
||||||
|
|
||||||
from thoughts.models import Thought
|
from thoughts.models import Thought
|
||||||
|
from thoughts.pagination import get_all_pages, get_page_slug, get_page
|
||||||
|
|
||||||
from jetforce import GeminiServer, JetforceApplication, Response, Status
|
from jetforce import GeminiServer, JetforceApplication, Response, Status
|
||||||
|
|
||||||
|
@ -20,8 +23,19 @@ app = JetforceApplication()
|
||||||
def index(request):
|
def index(request):
|
||||||
thoughts = Thought.objects.order_by("-posted")
|
thoughts = Thought.objects.order_by("-posted")
|
||||||
|
|
||||||
|
pages = get_all_pages()
|
||||||
|
|
||||||
|
[requested_slug] = parse_qs(request.query).get("page", [""])
|
||||||
|
print(requested_slug)
|
||||||
|
requested_page = get_page(requested_slug)
|
||||||
|
|
||||||
|
thoughts = requested_page.get_all_entries()
|
||||||
|
|
||||||
rendered_text = render_to_string("thoughts/index.gmi", {
|
rendered_text = render_to_string("thoughts/index.gmi", {
|
||||||
"thoughts": thoughts,
|
"thoughts": thoughts,
|
||||||
|
"first_page": requested_page == pages[0],
|
||||||
|
"pages": pages,
|
||||||
|
"current_page_slug": requested_page.slug,
|
||||||
})
|
})
|
||||||
|
|
||||||
return Response(Status.SUCCESS, "text/gemini", rendered_text)
|
return Response(Status.SUCCESS, "text/gemini", rendered_text)
|
||||||
|
|
|
@ -101,3 +101,31 @@ def get_all_pages():
|
||||||
# .formatted_name
|
# .formatted_name
|
||||||
# .get_all_entries
|
# .get_all_entries
|
||||||
# return [Pages]
|
# return [Pages]
|
||||||
|
|
||||||
|
|
||||||
|
def get_page(requested_slug, highlighted_uuid=None):
|
||||||
|
pages = get_all_pages()
|
||||||
|
|
||||||
|
# show=uuid takes priority over page
|
||||||
|
if highlighted_uuid:
|
||||||
|
try:
|
||||||
|
highlighted_thought = Thought.objects.get(uuid=highlighted_uuid)
|
||||||
|
requested_slug = get_page_slug(highlighted_thought)
|
||||||
|
except Thought.DoesNotExist:
|
||||||
|
pass
|
||||||
|
|
||||||
|
# When we get here, either:
|
||||||
|
# no slug was passed, requested_slug is ""
|
||||||
|
# a valid highlighted_uuid was passed, requested_slug is the slug of that page
|
||||||
|
# requested_slug is an invalid slug
|
||||||
|
# requested_slug is a valid slug
|
||||||
|
# First item in pages should be listed first
|
||||||
|
requested_page = pages[0]
|
||||||
|
for p in pages:
|
||||||
|
if p.slug == requested_slug:
|
||||||
|
requested_page = p
|
||||||
|
|
||||||
|
return requested_page
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,12 +1,22 @@
|
||||||
=> /about About
|
=> /about About
|
||||||
# Thoughts
|
# Thoughts
|
||||||
{% for thought in thoughts %}{{ thought.text|safe }}
|
|
||||||
|
{% if not first_page %}{% for page in pages %}{% if page.slug == current_page_slug %}
|
||||||
|
## {{ page.formatted_name }}{% else %}
|
||||||
|
=> /?page={{ page.slug }} {{ page.formatted_name }}{% endif %}{% endfor %}
|
||||||
|
=> /search Search{% endif %}
|
||||||
|
|
||||||
|
{% load tz %}
|
||||||
|
{% for thought in thoughts %}### {% timezone thought.get_timezone %}{{ thought.posted|time:"g:i a" }} {{ thought.posted|date:"M d, Y" }}, UTC{{ thought.get_offset_hours }}{% endtimezone %}
|
||||||
|
{{ thought.text|safe }}
|
||||||
{% if thought.extended_text %}
|
{% if thought.extended_text %}
|
||||||
{{ thought.extended_text|safe }}
|
{{ thought.extended_text|safe }}
|
||||||
{% endif %}{% if thought.media %}
|
{% endif %}{% if thought.media %}
|
||||||
=> gemini://thoughts.learnerpages.com{{ thought.media.url }}{% endif %}{% load tz %}
|
=> gemini://thoughts.learnerpages.com{{ thought.media.url }}{% endif %}
|
||||||
```
|
|
||||||
{% timezone thought.get_timezone %}{{ thought.posted|time:"g:i a" }} {{ thought.posted|date:"M d, Y" }}, UTC{{ thought.get_offset_hours }}{% endtimezone %}
|
|
||||||
```
|
|
||||||
▔▔▔
|
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
||||||
|
{% if not first_page %}{% for page in pages %}{% if page.slug == current_page_slug %}
|
||||||
|
### {{ page.formatted_name }}{% else %}
|
||||||
|
=> /?page={{ page.slug }} {{ page.formatted_name }}{% endif %}{% endfor %}
|
||||||
|
=> /search Search{% endif %}
|
||||||
|
|
|
@ -15,7 +15,7 @@ from haystack.forms import SearchForm
|
||||||
|
|
||||||
from whispermaphone import settings
|
from whispermaphone import settings
|
||||||
from .models import Thought, ThoughtForm, ALLOWED_MEDIA_TYPES
|
from .models import Thought, ThoughtForm, ALLOWED_MEDIA_TYPES
|
||||||
from .pagination import get_all_pages, get_page_slug
|
from .pagination import get_all_pages, get_page_slug, get_page
|
||||||
|
|
||||||
def check_authenticated(request):
|
def check_authenticated(request):
|
||||||
authenticated = False
|
authenticated = False
|
||||||
|
@ -39,25 +39,7 @@ def index(request):
|
||||||
pages = get_all_pages()
|
pages = get_all_pages()
|
||||||
|
|
||||||
requested_slug = request.GET.get("page", default="")
|
requested_slug = request.GET.get("page", default="")
|
||||||
|
requested_page = get_page(requested_slug, highlighted_uuid)
|
||||||
# show=uuid takes priority over page
|
|
||||||
if highlighted_uuid:
|
|
||||||
try:
|
|
||||||
highlighted_thought = Thought.objects.get(uuid=highlighted_uuid)
|
|
||||||
requested_slug = get_page_slug(highlighted_thought)
|
|
||||||
except Thought.DoesNotExist:
|
|
||||||
pass
|
|
||||||
|
|
||||||
# When we get here, either:
|
|
||||||
# no slug was passed, requested_slug is ""
|
|
||||||
# a valid highlighted_uuid was passed, requested_slug is the slug of that page
|
|
||||||
# requested_slug is an invalid slug
|
|
||||||
# requested_slug is a valid slug
|
|
||||||
# First item in pages should be listed first
|
|
||||||
requested_page = pages[0]
|
|
||||||
for p in pages:
|
|
||||||
if p.slug == requested_slug:
|
|
||||||
requested_page = p
|
|
||||||
|
|
||||||
thoughts = requested_page.get_all_entries()
|
thoughts = requested_page.get_all_entries()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue