Skip to content

Don't 500 on the voter email page for an election with no voters - #567

Merged
benadida merged 1 commit into
masterfrom
fix/voters-email-no-voters
Nov 18, 2026
Merged

benadida merged 1 commit into
masterfrom
fix/voters-email-no-voters

Conversation

@benadida

@benadida benadida commented Nov 18, 2026 •

Copy link
Copy Markdown
Owner

Error

GET /helios/elections/<uuid>/voters/email returned 500:

  File "/app/helios/views.py", line 1821, in voters_email
    'voter_type' : election.voter_set.all()[0].voter_type,
IndexError: list index out of range

Frequency: once in the past 24h (Nov 17, 20:02 UTC), on a frozen election that had no voters. Not frequent, but any admin who opens "email voters" before there are voters hits it.

Root cause

The email preview takes voter_type from the election's first voter via voter_set.all()[0], which raises IndexError when the election has no voters yet (e.g. a frozen open-registration election before anyone has voted).

Fix

Use the targeted voter if one was given, otherwise election.voter_set.first(), and pass None as the voter type if there are no voters. The preview then just shows the generic login line.

Tests

Added VoterEmailCutoffViewTests.test_voters_email_accessible_with_no_voters; it fails with IndexError without the fix. Full suite: uv run python manage.py test — 335 tests OK (1 skipped).

Datadog

service:helios-main "election.voter_set.all()[0]"

🤖 Generated with Claude Code

https://claude.ai/code/session_01QuQvo1opV1c3G9Hf22qGjf

The email preview read election.voter_set.all()[0].voter_type, which raises
IndexError when the election has no voters (e.g. open registration before
anyone has voted). Use the targeted voter, or the first voter if any, and
fall back to no voter type.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QuQvo1opV1c3G9Hf22qGjf

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Devin Review found 2 potential issues.

Devin Review

Comment thread helios/views.py
'voter_login_id': '<VOTER_LOGIN_ID>',
'voter_password': '<VOTER_PASSWORD>',
'voter_type' : election.voter_set.all()[0].voter_type,
'voter_type' : sample_voter.voter_type if sample_voter else None,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Empty-election preview names a None account

With no voters, sample_voter supplies None to the voting email preview. The login instructions render “Log in with your None account,” misleading admins about the message.

Learn more

The preview renders an email template with a placeholder voter. Voting and simple emails interpolate that voter's authentication type in their login instructions. On an election without voters, the new fallback passes None, which renders as None rather than a usable login method. The actual email task later uses each real voter, so only the preview is incorrect. See render_text_template and the simple login instructions.

Example: A frozen open-registration election has zero voters. Its admin opens the default voting email preview and reads “Log in with your None account,” although a future recipient will see their actual login method.

Recommended fix: Give the empty-election preview a distinct generic login instruction in both voting email templates, or pass a valid representative auth system selected from election.voter_auth_systems(). Add a preview-content assertion to the empty-voter test.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Confirmed: with no voters, the vote/simple preview's {{voter.voter_type}} renders as "None", so the preview line reads "Log in with your None account." It only affects the admin's preview; the real emails use each voter's own type. Since this is marked optional, I haven't pushed for it. If wanted, the follow-up is to guard that line in vote_body.txt/simple_body.txt with {% if voter.voter_type %} and fall back to a generic "Log in to vote." line, plus an assertion in the empty-voter test.


Generated by Claude Code

Comment thread helios/tests.py
Comment on lines +3175 to +3181
def test_voters_email_accessible_with_no_voters(self):
"""An election with no voters yet used to 500 building the preview (IndexError)"""
self.voter.delete()
self.setup_login()
response = self.client.get(f'/helios/elections/{self.election.uuid}/voters/email')
self.assertStatusCode(response, 200)
self.assertContains(response, 'Email')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 New test breaks required Python indentation

The new test_voters_email_accessible_with_no_voters method uses four-space indentation, contrary to the repository's required two-space Python indentation.

Learn more

The repository's indentation rule requires two spaces throughout Python files. The new test uses four spaces per indentation level, matching the surrounding tests but not that explicit rule. Changing only this method to two spaces would leave its class inconsistent.

Example: The method declaration starts with four spaces and its statements start with eight, where the stated convention calls for two and four spaces.

Recommended fix: Reconcile the mandatory indentation rule with the existing test-file convention before applying the change consistently; do not reindent only the new method if it makes the class harder to read.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

helios/tests.py uses 4-space indentation throughout, so the new test matches the class it sits in. Reindenting just this method would make the class inconsistent, and reindenting the whole file is out of scope for this fix. Leaving it as is.


Generated by Claude Code

@benadida
benadida merged commit d6aad04 into master Nov 18, 2026
2 checks passed
@benadida
benadida deleted the fix/voters-email-no-voters branch November 18, 2026 13:01
vndmtrx added a commit to helios-ptbr/helios-server that referenced this pull request Nov 18, 2026
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.

2 participants