[profiles] Actually make the form work

This commit is contained in:
2025-02-25 01:17:12 -05:00
parent 8c600d6b4b
commit 71874510a4
4 changed files with 33 additions and 11 deletions

View File

@ -4,6 +4,17 @@ from profiles.models import UserProfile
class UserProfileForm(forms.ModelForm): class UserProfileForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.request = kwargs.pop("request")
self.profile = UserProfile.objects.filter(
user=self.request.user
).first()
if not self.profile:
raise Exception
super(UserProfileForm, self).__init__(
*args, **kwargs, instance=self.profile
)
class Meta: class Meta:
model = UserProfile model = UserProfile
fields = [ fields = [
@ -26,3 +37,8 @@ class UserProfileForm(forms.ModelForm):
"ntfy_enabled", "ntfy_enabled",
"redirect_to_webpage", "redirect_to_webpage",
] ]
widgets = {
"lastfm_password": forms.PasswordInput(render_value=True),
"archivebox_password": forms.PasswordInput(render_value=True),
"webdav_pass": forms.PasswordInput(render_value=True),
}

View File

@ -6,6 +6,6 @@ app_name = "profiles"
urlpatterns = [ urlpatterns = [
path( path(
"settings/", views.ProfileDetailView.as_view(), name="profile_settings" "settings/", views.ProfileFormView.as_view(), name="profile_settings"
), ),
] ]

View File

@ -5,13 +5,20 @@ from django.views.generic import FormView
from profiles.forms import UserProfileForm from profiles.forms import UserProfileForm
class ProfileDetailView(FormView): class ProfileFormView(FormView):
form_class = UserProfileForm form_class = UserProfileForm
template_name = "profiles/settings_form.html" template_name = "profiles/settings_form.html"
success_url = reverse_lazy("profiles:profile_settings") success_url = reverse_lazy("profiles:profile_settings")
def get_context_data(self, **kwargs): def get_form_kwargs(self):
context_data = super().get_context_data(**kwargs) """Passes the request object to the form class.
if self.request.user.is_anonymous: This is necessary to only display members that belong to a given user"""
return HttpResponseBadRequest
return context_data kwargs = super(ProfileFormView, self).get_form_kwargs()
kwargs["request"] = self.request
return kwargs
def form_valid(self, form):
form.save()
return super(ProfileFormView, self).form_valid(form)

View File

@ -46,9 +46,8 @@ input:focus {
{% block title %}Settings{% endblock %} {% block title %}Settings{% endblock %}
{% block details %} {% block details %}
<form action="." method="post"> <form method="post">{% csrf_token %}
{% csrf_token %} {{form.as_p}}
{{form.as_p}} <input type="submit" value="Save">
<input type="submit" value="Save">
</form> </form>
{% endblock %} {% endblock %}