Skip to content
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
4 changes: 4 additions & 0 deletions accounts/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ class Meta:
"email",
"username",
)

def get_email_prefix_chars(self, n):
user = self.instance
return user.get_email_prefix_chars(n)
22 changes: 21 additions & 1 deletion accounts/models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
from django.contrib.auth.models import AbstractUser
from django.db import models


class CustomUser(AbstractUser):
pass

def __str__(self):
return self.email
return self.email

def get_email_prefix_chars(self, n):
"""Return the first n characters of the email as a list."""
if not isinstance(n, int) or n < 0:
raise ValueError("n must be a non-negative integer")
prefix = self.email
if len(prefix) == 0:
return []
chars = []
count = 0
i = 0
while i <= n:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The loop condition while i <= n: will return n+1 characters instead of n as intended. This creates an off-by-one error. Changing to while i < n: would fix this issue and ensure exactly n characters are returned.

Suggested change
while i <= n:
while i < n:

Spotted by Diamond

Is this helpful? React 👍 or 👎 to let us know.

if i < len(prefix):
chars.append(prefix[i])
count += 1
else:
break
i += 1
return chars
1 change: 1 addition & 0 deletions pages/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.views.generic import TemplateView
from django.shortcuts import render


class HomePageView(TemplateView):
Expand Down