diff --git a/vrobbler/apps/profiles/forms.py b/vrobbler/apps/profiles/forms.py index 89ef44b..84235e5 100644 --- a/vrobbler/apps/profiles/forms.py +++ b/vrobbler/apps/profiles/forms.py @@ -4,6 +4,17 @@ from profiles.models import UserProfile 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: model = UserProfile fields = [ @@ -26,3 +37,8 @@ class UserProfileForm(forms.ModelForm): "ntfy_enabled", "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), + } diff --git a/vrobbler/apps/profiles/urls.py b/vrobbler/apps/profiles/urls.py index 7df3a2d..490f5b4 100644 --- a/vrobbler/apps/profiles/urls.py +++ b/vrobbler/apps/profiles/urls.py @@ -6,6 +6,6 @@ app_name = "profiles" urlpatterns = [ path( - "settings/", views.ProfileDetailView.as_view(), name="profile_settings" + "settings/", views.ProfileFormView.as_view(), name="profile_settings" ), ] diff --git a/vrobbler/apps/profiles/views.py b/vrobbler/apps/profiles/views.py index 70d7e8b..c5484f2 100644 --- a/vrobbler/apps/profiles/views.py +++ b/vrobbler/apps/profiles/views.py @@ -5,13 +5,20 @@ from django.views.generic import FormView from profiles.forms import UserProfileForm -class ProfileDetailView(FormView): +class ProfileFormView(FormView): form_class = UserProfileForm template_name = "profiles/settings_form.html" success_url = reverse_lazy("profiles:profile_settings") - def get_context_data(self, **kwargs): - context_data = super().get_context_data(**kwargs) - if self.request.user.is_anonymous: - return HttpResponseBadRequest - return context_data + def get_form_kwargs(self): + """Passes the request object to the form class. + This is necessary to only display members that belong to a given user""" + + 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) diff --git a/vrobbler/templates/profiles/settings_form.html b/vrobbler/templates/profiles/settings_form.html index 4153be1..c2aad1a 100644 --- a/vrobbler/templates/profiles/settings_form.html +++ b/vrobbler/templates/profiles/settings_form.html @@ -46,9 +46,8 @@ input:focus { {% block title %}Settings{% endblock %} {% block details %} -
{% endblock %}