Coverage for crateweb/userprofile/views.py: 50%
16 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-08-27 10:34 -0500
« prev ^ index » next coverage.py v7.8.0, created at 2025-08-27 10:34 -0500
1"""
2crate_anon/crateweb/userprofile/views.py
4===============================================================================
6 Copyright (C) 2015, University of Cambridge, Department of Psychiatry.
7 Created by Rudolf Cardinal (rnc1001@cam.ac.uk).
9 This file is part of CRATE.
11 CRATE is free software: you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
16 CRATE is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with CRATE. If not, see <https://www.gnu.org/licenses/>.
24===============================================================================
26**View to edit an extended user profile.**
28"""
30from typing import TYPE_CHECKING
32from django.http.response import HttpResponse
33from django.http.request import HttpRequest
34from django.shortcuts import redirect, render
36from crate_anon.crateweb.config.constants import UrlNames
37from crate_anon.crateweb.userprofile.forms import UserProfileForm
39if TYPE_CHECKING:
40 from crate_anon.crateweb.userprofile.models import UserProfile
43# =============================================================================
44# User profile settings
45# =============================================================================
46# http://www.slideshare.net/pydanny/advanced-django-forms-usage
47# ... e.g. slide 72
50def edit_profile(request: HttpRequest) -> HttpResponse:
51 """
52 View to edit an extended user profile.
54 Args:
55 request: the :class:`django.http.request.HttpRequest`
57 Returns:
58 a :class:`django.http.response.HttpResponse`
60 """
61 # noinspection PyUnresolvedReferences
62 profile = request.user.profile # type: UserProfile
63 form = UserProfileForm(
64 request.POST if request.method == "POST" else None, instance=profile
65 )
66 if form.is_valid():
67 profile = form.save()
68 profile.save()
69 return redirect(UrlNames.HOME)
70 return render(
71 request, "edit_profile.html", {"form": form, "profile": profile}
72 )