Skip to content

Fix BuildError when last_issue.url_segment is None on journal detail page#429

Open
Copilot wants to merge 3 commits intomasterfrom
copilot/fix-url-segment-error
Open

Fix BuildError when last_issue.url_segment is None on journal detail page#429
Copilot wants to merge 3 commits intomasterfrom
copilot/fix-url-segment-error

Conversation

Copy link
Contributor

Copilot AI commented Mar 11, 2026

O que esse PR faz?

Corrige o BuildError recorrente em /j/<acron>/ quando last_issue existe mas url_segment é None — situação que ocorre no fluxo SPF onde LastIssue.url_segment não é preenchido ao registrar issues.

Duas correções:

  • opac/webapp/main/views.py: Usa utils.fix_journal_last_issue(journal) (já existia para admin views) em journal_detail() e about_journal() para construir url_segment a partir de year/volume/number quando ausente
  • opac/webapp/templates/journal/detail.html: Guarda defensiva {% if last_issue and last_issue.url_segment %} em ambos os blocos, e corrige referências a latest_issue.cover_url (variável inexistente no contexto) para last_issue.cover_url

Onde a revisão poderia começar?

opac/webapp/main/views.py linha 512 — a chamada a fix_journal_last_issue().

Como este poderia ser testado manualmente?

  1. Criar um journal com last_issue cujo url_segment seja None
  2. Acessar /j/<acron>/
  3. Verificar que a página renderiza sem BuildError
  4. Verificar que quando url_segment existe, os links para o último número continuam funcionando

Algum cenário de contexto que queira dar?

A função fix_journal_last_issue() em webapp/utils/utils.py já tratava exatamente este caso e já era usada em admin/views.py:1004, mas não era chamada nas views públicas. O docstring da própria função documenta que o SPF não preenche url_segment.

Screenshots

N/A — correção de erro server-side.

Quais são tickets relevantes?

Erro recorrente relacionado com url_segment.

Referências

  • opac/webapp/utils/utils.py:610-627fix_journal_last_issue()
  • opac/webapp/admin/views.py:1004 — uso existente da mesma função

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • api.twitter.com
    • Triggering command: /home/REDACTED/.local/bin/flask flask --app opac.app test rese�� ng/Offset.o (dns block)
    • Triggering command: /home/REDACTED/.local/bin/flask flask --app opac.app test rev-�� (dns block)
    • Triggering command: /home/REDACTED/.local/bin/flask flask --app opac.app test --pid-file /run/containerd/io.containerd.runtime.v2.task/moby/cd1c72e168274290c2e76f74295e1618a5a8f39cd3d0a620118ac01a901b8e87/init.pid cd1c72e168274290c2e76f74295e1618a5a8f39cd3d0a620118ac01a901b8e87 u/13/cc1 unset hon3.12 k/_temp/copilot---noprofile u/13/cc1 --li�� ng/QuantPngQuant.o --keep-system-libs de in.so hon3.12 (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>Erro recorrente relaciondo com url_segment</issue_title>
<issue_description>### Descrição do problema
Recentemente está ocorrendo um erro relacionado com o último issue do periódico.

Passos para reproduzir o problema

2026-02-27 12:11:50 +0000] [9] [ERROR] Error handling request /j/abcd/
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/gunicorn/workers/base_async.py", line 55, in handle
    self.handle_request(listener_name, req, client, addr)
  File "/usr/local/lib/python3.11/site-packages/gunicorn/workers/ggevent.py", line 127, in handle_request
    super().handle_request(listener_name, req, sock, addr)
  File "/usr/local/lib/python3.11/site-packages/gunicorn/workers/base_async.py", line 108, in handle_request
    respiter = self.wsgi(environ, resp.start_response)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/flask/app.py", line 2548, in __call__
    return self.wsgi_app(environ, start_response)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/werkzeug/middleware/proxy_fix.py", line 187, in __call__
    return self.app(environ, start_response)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/flask/app.py", line 2528, in wsgi_app
    response = self.handle_exception(e)
               ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/flask/app.py", line 2525, in wsgi_app
    response = self.full_dispatch_request()
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/flask/app.py", line 1822, in full_dispatch_request
    rv = self.handle_user_exception(e)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/flask/app.py", line 1820, in full_dispatch_request
    rv = self.dispatch_request()
         ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/flask_debugtoolbar/__init__.py", line 142, in dispatch_request
    return view_func(**req.view_args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/flask_caching/__init__.py", line 404, in decorated_function
    rv = self._call_fn(f, *args, **kwargs)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/flask_caching/__init__.py", line 180, in _call_fn
    return ensure_sync(fn)(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/opac/webapp/main/views.py", line 544, in journal_detail
    return render_template("journal/detail.html", **context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/flask/templating.py", line 147, in render_template
    return _render(app, template, context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/flask/templating.py", line 130, in _render
    rv = template.render(context)
         ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/jinja2/environment.py", line 1289, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.11/site-packages/jinja2/environment.py", line 924, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "/app/opac/webapp/templates/journal/detail.html", line 1, in <module>
    {% extends "journal/base.html" %}
    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/opac/webapp/templates/journal/base.html", line 1, in <module>
    {% extends "base.html" %}
  File "/app/opac/webapp/templates/base.html", line 51, in <module>
    {% block content %}{% endblock %}
    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/opac/webapp/templates/journal/base.html", line 19, in <module>
    {% block main_content %}{% endblock %}
    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/opac/webapp/templates/journal/detail.html", line 75, in <module>
    <a href="{{ url_for('.issue_toc', url_seg=journal.url_segment, url_seg_issue=last_issue.url_segment) }}">
    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/flask/app.py", line 2031, in url_for
    return self.handle_url_build_error(error, endpoint, values)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/flask/app.py", line 2020, in url_for
    rv = url_adapter.build(  # type: ignore[union-attr]
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/werkzeug/routing/map.py", line 917, in build
    raise BuildError(endpoint, values, method, self)
werkzeug.routing.exceptions.BuildError: Could not build url for endpoint 'main.issue_toc' with values ['url_seg']. Did you forget to specify values ['url_seg_issue']?

Comportamento esperado

ausência de erro

Screenshots ou vídeos

n/a

Ambiente utilizado

n/a
</i...


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits March 11, 2026 17:16
Use fix_journal_last_issue() to populate url_segment in journal_detail
and about_journal views. Add defensive template guards to check
last_issue.url_segment before building URLs.

Co-authored-by: robertatakenaka <505143+robertatakenaka@users.noreply.github.com>
Co-authored-by: robertatakenaka <505143+robertatakenaka@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix recurring error related to url_segment Fix BuildError when last_issue.url_segment is None on journal detail page Mar 11, 2026
@robertatakenaka robertatakenaka requested a review from Copilot March 11, 2026 17:45
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes a recurring Flask/Werkzeug BuildError on the public journal detail page (/j/<acron>/) when journal.last_issue exists but its url_segment is None (not populated in the SPF flow), ensuring links to the latest issue can be built safely.

Changes:

  • Public views now call utils.fix_journal_last_issue(journal) to derive/fill last_issue.url_segment when missing.
  • journal/detail.html now guards rendering of “last issue” blocks on last_issue.url_segment and fixes an invalid template variable (latest_issue.cover_urllast_issue.cover_url).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
opac/webapp/main/views.py Backfills last_issue.url_segment for public journal pages by reusing existing helper logic.
opac/webapp/templates/journal/detail.html Prevents url_for from being called without url_seg_issue and fixes cover image binding to the correct context variable.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +512 to 515
latest_issue = utils.fix_journal_last_issue(journal)

if latest_issue:
latest_issue_legend = descriptive_short_format(
Copy link

Copilot AI Mar 11, 2026

Choose a reason for hiding this comment

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

Add a regression test for the scenario this change fixes: a Journal with last_issue present but last_issue.url_segment is None should render /j/<acron>/ (and /journal/<acron>/about/) without raising BuildError, and should generate a valid issue_toc link using the derived segment. There are already integration tests for main.journal_detail, so covering this edge case will prevent the error from reappearing.

Copilot uses AI. Check for mistakes.
@robertatakenaka robertatakenaka marked this pull request as ready for review March 11, 2026 20:26
@robertatakenaka robertatakenaka requested a review from Copilot March 11, 2026 20:26
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Erro recorrente relaciondo com url_segment

3 participants