Coverage for accounts/views.py: 100.00%

29 statements  

« prev     ^ index     » next       coverage.py v7.16.1, created at 2026-09-23 10:02 -0700

1"""Views for kbowen-django-polls user accounts.""" 

2 

3from django.contrib.auth import get_user_model 1a

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

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

6from django.urls import reverse, reverse_lazy 1a

7from django.views.generic import ( 1a

8 CreateView, 

9 DetailView, 

10 RedirectView, 

11 UpdateView, 

12) 

13 

14from .forms import CustomUserCreationForm 1a

15 

16User = get_user_model() 1a

17 

18 

19class SignupPageView(CreateView): 1a

20 form_class = CustomUserCreationForm 1a

21 success_url = reverse_lazy("account_login") 1a

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

23 

24 

25class UserDetailView(LoginRequiredMixin, DetailView): 1a

26 model = User 1a

27 # Tell the view to index lookups by username 

28 slug_field = "username" 1a

29 slug_url_kwarg = "username" 1a

30 

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

32 

33 

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

35 fields = [ 1a

36 "name", 

37 "age", 

38 "bio", 

39 "country", 

40 "profile_pic", 

41 ] 

42 model = User 1a

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

44 # success_message = "%(name)s's profile was updated successfully." 

45 

46 # Send the user back to their own page after a successful update 

47 def get_success_url(self): 1a

48 return reverse( 1a

49 "user_detail", 

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

51 ) 

52 

53 def get_object(self): 1a

54 # Only get the user record for the user making the request 

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

56 

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

58 

59 

60class UserRedirectView(LoginRequiredMixin, RedirectView): 1a

61 permanent = False 1a

62 

63 def get_redirect_url(self): 1a

64 return reverse( 1a

65 "user_detail", 

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

67 )