Skip to content

Commit 56b5b61

Browse files
committed
add non editable credentials accounts, for creating demo accounts
1 parent 084c062 commit 56b5b61

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

src/users/forms.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,15 @@ class Meta:
106106
class UserUpdateForm(forms.ModelForm):
107107
"""Custom form for updating username."""
108108

109+
def clean(self: "PasswordChangeForm") -> dict:
110+
"""Check if the user is editable before changing the password."""
111+
112+
cleaned_data = super().clean()
113+
if not self.instance.editable:
114+
msg = "Changing the username is not allowed for this account."
115+
self.add_error("username", msg)
116+
return cleaned_data
117+
109118
def __init__(self: "UserUpdateForm", *args: dict, **kwargs: dict) -> None:
110119
"""Add crispy form helper to add submit button."""
111120
super().__init__(*args, **kwargs)
@@ -124,6 +133,15 @@ class Meta:
124133
class PasswordChangeForm(PasswordChangeForm):
125134
"""Custom form for changing password."""
126135

136+
def clean(self: "PasswordChangeForm") -> dict:
137+
"""Check if the user is editable before changing the password."""
138+
139+
cleaned_data = super().clean()
140+
if not self.user.editable:
141+
msg = "Changing the password is not allowed for this account."
142+
self.add_error("new_password2", msg)
143+
return cleaned_data
144+
127145
def __init__(self: "PasswordChangeForm", *args: dict, **kwargs: dict) -> None:
128146
"""Remove autofocus from password change form."""
129147

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Generated by Django 5.0.2 on 2024-02-19 15:05
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
dependencies = [
8+
("users", "0003_user_default_layout"),
9+
]
10+
11+
operations = [
12+
migrations.AddField(
13+
model_name="user",
14+
name="editable",
15+
field=models.BooleanField(default=True),
16+
),
17+
]

src/users/models.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ def get_default_layout() -> dict:
1616
class User(AbstractUser):
1717
"""Custom user model that saves the last media search type."""
1818

19+
editable = models.BooleanField(default=True)
20+
1921
last_search_type = models.CharField(
2022
max_length=10,
2123
default="tv",

src/users/views.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ def profile(request: HttpRequest) -> HttpResponse:
9494
password = password_form.save()
9595
update_session_auth_hash(request, password)
9696
messages.success(request, "Your password has been updated!")
97-
logger.info("Successful password change for: %s", request.user.username)
97+
logger.info(
98+
"Successful password change for: %s",
99+
request.user.username,
100+
)
98101
else:
99102
logger.error(
100103
"Failed password change for %s: %s",

0 commit comments

Comments
 (0)