Coverage for accounts/views.py: 100.00%

30 statements  

« prev     ^ index     » next       coverage.py v7.17.2, created at 2026-11-05 13:07 -0800

1"""Views for django-start user accounts.""" 

2 

3from typing import ClassVar 1a

4 

5from django.contrib.auth import get_user_model 1a

6from django.contrib.auth.mixins import LoginRequiredMixin 1a

7from django.contrib.messages.views import SuccessMessageMixin 1a

8from django.urls import reverse, reverse_lazy 1a

9from django.views.generic import ( 1a

10 CreateView, 

11 DetailView, 

12 RedirectView, 

13 UpdateView, 

14) 

15 

16from .forms import CustomUserCreationForm 1a

17 

18User = get_user_model() 1a

19 

20 

21class SignupPageView(CreateView): 1a

22 form_class = CustomUserCreationForm 1a

23 success_url = reverse_lazy("account_login") 1a

24 template_name = "account/signup.html" 1a

25 

26 

27class UserDetailView(LoginRequiredMixin, DetailView): 1a

28 """ 

29 Tell the view to index lookups by username. 

30 Display an individual :model:`accounts.CustomUser` 

31 

32 **Context** 

33 

34 ``accounts`` 

35 An instance of :model:`accounts.CustomUser` 

36 

37 **Template** 

38 

39 :template:`account/user_detail.html` 

40 """ 

41 

42 model = User 1a

43 slug_field = "username" 1a

44 slug_url_kwarg = "username" 1a

45 

46 template_name = "account/user_detail.html" 1a

47 

48 

49class UserUpdateView(LoginRequiredMixin, SuccessMessageMixin, UpdateView): 1a

50 """ 

51 Permit user to edit own account information. 

52 Display an individual :model:`accounts.CustomUser` 

53 

54 **Context** 

55 

56 ``accounts`` 

57 An instance of :model:`accounts.CustomUser` 

58 

59 **Template** 

60 

61 :template:`account/user_form.html` 

62 """ 

63 

64 fields: ClassVar[list[str]] = [ 1a

65 "name", 

66 "age", 

67 "bio", 

68 "country", 

69 "profile_pic", 

70 ] 

71 model = User 1a

72 success_message = "Your profile was updated successfully." 1a

73 # success_message = "%(name)s's profile has been successfully updated." 

74 

75 def get_success_url(self): 1a

76 """Send the user back to their own page after a successful update.""" 

77 return reverse( 1a

78 "user_detail", 

79 kwargs={"username": self.request.user.username}, 

80 ) 

81 

82 def get_object(self): 1a

83 """Only get the user record for the user making the request.""" 

84 return User.objects.get(username=self.request.user.username) 1a

85 

86 template_name = "account/user_form.html" 1a

87 

88 

89class UserRedirectView(LoginRequiredMixin, RedirectView): 1a

90 permanent = False 1a

91 

92 def get_redirect_url(self): 1a

93 return reverse( 1a

94 "user_detail", 

95 kwargs={"username": self.request.user.username}, 

96 )