-
Notifications
You must be signed in to change notification settings - Fork 10
feat: add reusable CSV/XLS export utility and filtered download routes to journal page classes #1417
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
base: main
Are you sure you want to change the base?
feat: add reusable CSV/XLS export utility and filtered download routes to journal page classes #1417
Changes from 3 commits
554c1b8
8a96741
1c4933d
05eed2e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,11 @@ | |
| from wagtailcaptcha.models import WagtailCaptchaEmailForm | ||
|
|
||
| from collection.models import Collection | ||
| from core.home.utils.export_journals import ( | ||
| generate_csv_response, | ||
| generate_xls_response, | ||
| get_scielo_journals_data, | ||
| ) | ||
| from core.home.utils.get_social_networks import get_social_networks | ||
| from journal.choices import STUDY_AREA | ||
| from journal.models import OwnerHistory, SciELOJournal | ||
|
|
@@ -79,6 +84,18 @@ def _default_context(context): | |
| context["page_about"] = get_page_about() | ||
|
|
||
|
|
||
| class JournalDownloadMixin: | ||
| @re_path(r"^download-csv/$", name="download_csv") | ||
| def download_csv(self, request): | ||
| journals_data = get_scielo_journals_data() | ||
| return generate_csv_response(journals_data) | ||
|
Comment on lines
+91
to
+95
|
||
|
|
||
| @re_path(r"^download-xls/$", name="download_xls") | ||
| def download_xls(self, request): | ||
| journals_data = get_scielo_journals_data() | ||
| return generate_xls_response(journals_data) | ||
|
|
||
|
|
||
| def get_page_about(): | ||
| try: | ||
| locale = _get_current_locale() | ||
|
|
@@ -221,7 +238,7 @@ def get_context(self, request, *args, **kwargs): | |
| return context | ||
|
|
||
|
|
||
| class ListPageJournal(Page): | ||
| class ListPageJournal(JournalDownloadMixin, RoutablePageMixin, Page): | ||
| def get_context(self, request, *args, **kwargs): | ||
| context = super().get_context(request, *args, **kwargs) | ||
| search_term = request.GET.get("search_term", "") | ||
|
|
@@ -237,7 +254,7 @@ def get_context(self, request, *args, **kwargs): | |
| return context | ||
|
|
||
|
|
||
| class ListPageJournalByPublisher(Page): | ||
| class ListPageJournalByPublisher(JournalDownloadMixin, RoutablePageMixin, Page): | ||
| def get_context(self, request, *args, **kwargs): | ||
| context = super().get_context(request, *args, **kwargs) | ||
| search_term = request.GET.get("search_term", "") | ||
|
|
@@ -283,7 +300,7 @@ def get_context(self, request, *args, **kwargs): | |
| return context | ||
|
|
||
|
|
||
| class ListPageJournalByCategory(RoutablePageMixin, Page): | ||
| class ListPageJournalByCategory(JournalDownloadMixin, RoutablePageMixin, Page): | ||
| def get_context(self, request, *args, **kwargs): | ||
| context = super().get_context(request, *args, **kwargs) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,87 @@ | ||||||
| import csv | ||||||
| import logging | ||||||
|
|
||||||
| import xlwt | ||||||
| from django.http import HttpResponse | ||||||
| from django.utils import timezone | ||||||
|
|
||||||
| from journal.models import SciELOJournal | ||||||
|
|
||||||
| logger = logging.getLogger(__name__) | ||||||
|
|
||||||
| HEADERS = ["journals", "scielo_url", "publisher"] | ||||||
|
||||||
|
|
||||||
|
|
||||||
| def get_scielo_journals_data(): | ||||||
| try: | ||||||
| scielo_journals = SciELOJournal.objects.values( | ||||||
| "journal__title", | ||||||
| "collection__domain", | ||||||
| "journal__owner_history__institution__institution__institution_identification__name", | ||||||
| "issn_scielo", | ||||||
| ) | ||||||
|
||||||
| ) | |
| ).distinct() |
Copilot
AI
Mar 23, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The exported column headers are journals/publisher, but the internal data keys are title/owner. This mismatch makes the utility harder to reuse correctly and increases the chance of future mistakes. Consider aligning the dict keys to the export contract (e.g., use journals and publisher everywhere) and then read those same keys in both generate_csv_response and generate_xls_response.
Copilot
AI
Mar 23, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Logging the exception message without a traceback makes diagnosing production issues harder. Prefer logger.exception("Error fetching scielo journals data") (or logger.error(..., exc_info=True)) so the traceback is captured.
Copilot
AI
Mar 23, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The exported column headers are journals/publisher, but the internal data keys are title/owner. This mismatch makes the utility harder to reuse correctly and increases the chance of future mistakes. Consider aligning the dict keys to the export contract (e.g., use journals and publisher everywhere) and then read those same keys in both generate_csv_response and generate_xls_response.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,14 @@ | ||
| {% load wagtailroutable_tags %} | ||
| <th class="text-nowrap w-auto" scope="col"> | ||
| <button id="btnGroupDropDownload" type="submit" class="btn btn-sm btn-secondary scielo__btn-with-icon--only dropdown-toggle mb-0 float-end d-print-none" data-bs-toggle="dropdown" aria-expanded="false"> | ||
| <span class="material-icons-outlined">download</span> | ||
| </button> | ||
| <ul class="dropdown-menu dropdown-menu-end" aria-labelledby="btnGroupDropDownload" > | ||
| <li> | ||
| <a class="dropdown-item" href="{% url 'download_xls_journals_page_scielo_org' %}">XLS</a> | ||
| <a class="dropdown-item" href="{% routablepageurl page 'download_xls' %}">XLS</a> | ||
| </li> | ||
| <li> | ||
| <a class="dropdown-item" href="{% url 'download_csv_journals_page_scielo_org' %}">CSV</a> | ||
| <a class="dropdown-item" href="{% routablepageurl page 'download_csv' %}">CSV</a> | ||
| </li> | ||
| </ul> | ||
| </th> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot deve considerar que os itens foram filtrados usando os argumentos de request
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in
05eed2e. The download endpoints now respect request filters:get_scielo_journals_data(filters=None)accepts optional Q filtersJournalDownloadMixinuses a template methodget_export_filters(request)— each page class overrides it to build the same filters used inget_contextListPageJournalByPublisher.get_export_filtersincludes publisher name search (matching the page's own filter logic)request.GET.urlencode