diff --git a/.gitignore b/.gitignore index b705f60..99b4a04 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,6 @@ node_modules **/dist/ /staticfiles/ .coverage +/hot_osm/locale +/media +/locale \ No newline at end of file diff --git a/app/news/migrations/0020_newsownerpage_category_select_and_more.py b/app/news/migrations/0020_newsownerpage_category_select_and_more.py new file mode 100644 index 0000000..2f0bd60 --- /dev/null +++ b/app/news/migrations/0020_newsownerpage_category_select_and_more.py @@ -0,0 +1,63 @@ +# Generated by Django 4.2.7 on 2024-06-25 22:20 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('news', '0019_newsownerpage_and_more'), + ] + + operations = [ + migrations.AddField( + model_name='newsownerpage', + name='category_select', + field=models.CharField(default='Select Categories'), + ), + migrations.AddField( + model_name='newsownerpage', + name='enter_tag_hint', + field=models.CharField(default='Enter tag'), + ), + migrations.AddField( + model_name='newsownerpage', + name='keyword_search_hint', + field=models.CharField(default='Search by keyword'), + ), + migrations.AddField( + model_name='newsownerpage', + name='remove_filters_text', + field=models.CharField(default='Remove All Filters'), + ), + migrations.AddField( + model_name='newsownerpage', + name='results_text', + field=models.CharField(default='Results'), + ), + migrations.AddField( + model_name='newsownerpage', + name='search_button_text', + field=models.CharField(default='Search'), + ), + migrations.AddField( + model_name='newsownerpage', + name='sort_by_new', + field=models.CharField(default='Sort by New'), + ), + migrations.AddField( + model_name='newsownerpage', + name='sort_by_old', + field=models.CharField(default='Sort by Old'), + ), + migrations.AddField( + model_name='newsownerpage', + name='sort_by_titlea', + field=models.CharField(default='Sort by Title Alphabetical'), + ), + migrations.AddField( + model_name='newsownerpage', + name='sort_by_titlez', + field=models.CharField(default='Sort by Title Reverse Alphabetical'), + ), + ] diff --git a/app/news/models.py b/app/news/models.py index d2c3b1e..279dda1 100644 --- a/app/news/models.py +++ b/app/news/models.py @@ -1,5 +1,7 @@ from django import forms from django.db import models +from django.db.models import Q +from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger from modelcluster.fields import ParentalKey, ParentalManyToManyField from modelcluster.contrib.taggit import ClusterTaggableManager @@ -10,9 +12,56 @@ from wagtail.admin.panels import FieldPanel, MultiFieldPanel, InlinePanel from wagtail.blocks import CharBlock, StreamBlock, StructBlock, URLBlock, RichTextBlock, PageChooserBlock from wagtail.snippets.models import register_snippet +from wagtail.search import index class NewsOwnerPage(Page): + def get_context(self, request, *args, **kwargs): + context = super().get_context(request, *args, **kwargs) + + keyword = request.GET.get('keyword', '') + news_list = IndividualNewsPage.objects.live().filter(locale=context['page'].locale) + + categories = NewsCategory.objects.all() + tags = [x[4:] for x in request.GET.keys() if x.startswith("tag.")] + query = Q() + for category in categories: + if request.GET.get(str(category), ''): + query = query | Q(categories=category) + for tag in tags: + query = query | Q(tags__name=tag) + news_list = news_list.filter(query).distinct() + + match request.GET.get('sort', ''): + case 'sort.new': + news_list = news_list.order_by('date') + case 'sort.old': + news_list = news_list.order_by('-date') + case 'sort.titlea': + news_list = news_list.order_by('title') + case 'sort.titlez': + news_list = news_list.order_by('-title') + case _: + news_list = news_list.order_by('date') + + if keyword: + news_list = news_list.search(keyword) + + page = request.GET.get('page', 1) + paginator = Paginator(news_list, 6) # if you want more/less items per page (i.e., per load), change the number here to something else + try: + news = paginator.page(page) + except PageNotAnInteger: + news = paginator.page(1) + except EmptyPage: + news = paginator.page(paginator.num_pages) + + context['news'] = news + context['news_paginator'] = paginator + context['current_page'] = int(page) + context['categories'] = categories + return context + max_count = 1 authors_posted_by_text = models.CharField(default="Posted by", help_text="The text which appears prior to the authors names; with 'posted by', the text displays as 'posted by [author]'.") @@ -25,20 +74,45 @@ class NewsOwnerPage(Page): categories_title = models.CharField(default="Categories") tags_title = models.CharField(default="Tags") + keyword_search_hint = models.CharField(default="Search by keyword") + category_select = models.CharField(default="Select Categories") + sort_by_new = models.CharField(default="Sort by New") + sort_by_old = models.CharField(default="Sort by Old") + sort_by_titlea = models.CharField(default="Sort by Title Alphabetical") + sort_by_titlez = models.CharField(default="Sort by Title Reverse Alphabetical") + enter_tag_hint = models.CharField(default="Enter tag") + search_button_text = models.CharField(default="Search") + remove_filters_text = models.CharField(default="Remove All Filters") + results_text = models.CharField(default="Results") + content_panels = Page.content_panels + [ MultiFieldPanel([ - FieldPanel("authors_posted_by_text"), - FieldPanel("authors_posted_on_text"), - ], heading="Info"), + FieldPanel('keyword_search_hint'), + FieldPanel('category_select'), + FieldPanel('sort_by_new'), + FieldPanel('sort_by_old'), + FieldPanel('sort_by_titlea'), + FieldPanel('sort_by_titlez'), + FieldPanel('enter_tag_hint'), + FieldPanel('search_button_text'), + FieldPanel('remove_filters_text'), + FieldPanel('results_text'), + ], heading="News Search Page"), MultiFieldPanel([ - FieldPanel('related_projects_title'), - FieldPanel('related_news_title'), - FieldPanel('view_all_news_text'), - FieldPanel('view_all_news_url'), - FieldPanel('news_read_more_text'), - FieldPanel('categories_title'), - FieldPanel('tags_title'), - ], heading="Sidebar"), + MultiFieldPanel([ + FieldPanel("authors_posted_by_text"), + FieldPanel("authors_posted_on_text"), + ], heading="Info"), + MultiFieldPanel([ + FieldPanel('related_projects_title'), + FieldPanel('related_news_title'), + FieldPanel('view_all_news_text'), + FieldPanel('view_all_news_url'), + FieldPanel('news_read_more_text'), + FieldPanel('categories_title'), + FieldPanel('tags_title'), + ], heading="Sidebar"), + ], heading="Individual Project Page"), ] @@ -103,6 +177,13 @@ class IndividualNewsPage(Page): tags = ClusterTaggableManager(through=NewsTag, blank=True) + search_fields = Page.search_fields + [ + index.SearchField('title'), + index.SearchField('intro'), + index.FilterField('newscategory_id'), # the console warns you about this but if you don't have this then category search doesn't work + index.FilterField('name'), + ] + content_panels = Page.content_panels + [ MultiFieldPanel([ FieldPanel("authors"), diff --git a/app/news/templates/news/components/NewsSortOption.html b/app/news/templates/news/components/NewsSortOption.html new file mode 100644 index 0000000..8b30d23 --- /dev/null +++ b/app/news/templates/news/components/NewsSortOption.html @@ -0,0 +1,4 @@ +
+ {% include "ui/components/icon_svgs/LinkCaret.html" with class="rotate-180" %} +
+ ++ {{forloop.counter}} +
+ + {% endif %} + {% if forloop.counter|add:2 == current_page or forloop.counter|add:'-2' == current_page and not forloop.last %} + {% comment %} + for reasons that i can't begin to comprehend, the if statement + below CANNOT be part of the if statement above. it just does + not work + {% endcomment %} + {% if not forloop.first %} +...
+ {% endif %} + {% endif %} + {% endfor %} + {% endwith %} ++ {% include "ui/components/icon_svgs/LinkCaret.html" %} +
+ +