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

1""" 

2crate_anon/crateweb/userprofile/views.py 

3 

4=============================================================================== 

5 

6 Copyright (C) 2015, University of Cambridge, Department of Psychiatry. 

7 Created by Rudolf Cardinal (rnc1001@cam.ac.uk). 

8 

9 This file is part of CRATE. 

10 

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. 

15 

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. 

20 

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/>. 

23 

24=============================================================================== 

25 

26**View to edit an extended user profile.** 

27 

28""" 

29 

30from typing import TYPE_CHECKING 

31 

32from django.http.response import HttpResponse 

33from django.http.request import HttpRequest 

34from django.shortcuts import redirect, render 

35 

36from crate_anon.crateweb.config.constants import UrlNames 

37from crate_anon.crateweb.userprofile.forms import UserProfileForm 

38 

39if TYPE_CHECKING: 

40 from crate_anon.crateweb.userprofile.models import UserProfile 

41 

42 

43# ============================================================================= 

44# User profile settings 

45# ============================================================================= 

46# http://www.slideshare.net/pydanny/advanced-django-forms-usage 

47# ... e.g. slide 72 

48 

49 

50def edit_profile(request: HttpRequest) -> HttpResponse: 

51 """ 

52 View to edit an extended user profile. 

53 

54 Args: 

55 request: the :class:`django.http.request.HttpRequest` 

56 

57 Returns: 

58 a :class:`django.http.response.HttpResponse` 

59 

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 )