Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blog pagination #346

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,23 @@
<div id="blog">
{% for entry in entries %}
<div class="message" id="{{ entry.id }}">
{# TODO These links won't work if entry is not on currently shown page. Fix by adding a separate view. #}
<a style="text-decoration: none;" href="#{{ entry.id }}"><h2>{{ entry.title }}</h2></a>
<time datetime="{{ entry.date|date:"Y-m-d\TH:i" }}" class="date">{{ entry.date|date:"d.m.Y H:i" }}</time>
<div class="content">{{ entry.text|safe }}</div>
</div>
{% endfor %}
{% if needs_pagination %}
<ul class="pagination">
{% for i in page_range %}
{% if i == page_number %}
<li class="active" aria-label="Nykyinen sivu: {{ i }}/{{ num_pages }}"><a>{{ i }}</a></li>
{% else %}
<li><a href="?p={{ i }}" aria-label="Siirry sivulle: {{ i }}/{{ num_pages }}">{{ i }}</a></li>
{% endif %}
{% endfor %}
</ul>
{% endif %}
</div>
{% else %}
<p>Blogissa ei ole entryjä!</p>
Expand Down
21 changes: 18 additions & 3 deletions backend/Instanssi/ext_blog/templatetags/blog_tags.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
from django import template
from django.core.paginator import Paginator

from Instanssi.ext_blog.models import BlogEntry

register = template.Library()


@register.inclusion_tag("ext_blog/blog_messages.html")
def render_blog(event_id: int, max_posts: int = 10) -> dict:
return {"entries": BlogEntry.get_latest().filter(event_id=event_id)[:max_posts]}
@register.inclusion_tag("ext_blog/blog_messages.html", takes_context=True)
def render_blog(context, event_id: int, max_posts: int = 10) -> dict:
request = context["request"]
page_number = request.GET.get('p', 1) # Default to page 1 if get parameter is missing
# Make sure the page number from the get parameter is a valid integer:
try:
page_number = int(page_number)
except ValueError:
page_number = 1
Copy link
Member

@katajakasa katajakasa Apr 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider splitting the request.GET and exception handler to a separate function, get_page_number() or somesuch.

full_entries_list = BlogEntry.get_latest().filter(event_id=event_id)
paginator = Paginator(full_entries_list, max_posts)
current_page = paginator.get_page(page_number)
entries = current_page.object_list
# We will pass this boolean as extra data to the template for simplicity
# It's used to determine whether to render pagination links or not:
needs_pagination = paginator.count > paginator.per_page
return {"entries": entries, "needs_pagination": needs_pagination, "page_range": paginator.page_range, "page_number": current_page.number, "num_pages": paginator.num_pages}
Copy link
Member

@katajakasa katajakasa Apr 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remember to run black & isort!

In backend/ directory, run:

poetry run black .
poetry run isort .

11 changes: 7 additions & 4 deletions backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ Installing stuff for development
1. Install the dependencies and clone this project.
2. Copy `settings.py-dist` to `settings.py`. Change as needed. `settings.py` should never be added to
git, as it may contain secrets.
3. Set up environment with poetry `poetry env use 3.11`.
3. Set up environment with poetry `poetry env use 3.11` (run inside the `backend/` directory).
4. Install packages with poetry `poetry install --no-root --sync`.
5. Make sure your database is set up and configured in settings.py, and then run database
5. Switch over to a poetry shell by running `poetry shell` to have the virtual
environment in use for the rest of the commands. Alternatively you can add
`poetry run` before every command.
6. Make sure your database is set up and configured in settings.py, and then run database
migrations to set up initial data `python manage.py migrate`.
6. Create a superuser so that you can access the admin `python manage.py createsuperuser`.
7. That's all. Now just start local dev server by running `python manage.py runserver`.
7. Create a superuser so that you can access the admin `python manage.py createsuperuser`.
8. That's all. Now just start local dev server by running `python manage.py runserver`.

Note that some background operations use celery. It can be started with following:
`python -m celery -A Instanssi worker -l info --autoscale 2,1`
Expand Down
Loading