Coverage for accounts/views.py: 100.00%

25 statements  

« prev     ^ index     » next       coverage.py v7.17.0, created at 2026-10-23 15:55 -0700

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

2from typing import ClassVar 1a

3 

4from django.contrib.auth import get_user_model 1a

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

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

7from django.urls import reverse 1a

8from django.views.generic import ( 1a

9 DetailView, 

10 RedirectView, 

11 UpdateView, 

12) 

13 

14User = get_user_model() 1a

15 

16 

17class UserDetailView(LoginRequiredMixin, DetailView): 1a

18 """Tell the view to index lookups by username.""" 

19 

20 model = User 1a

21 slug_field = "username" 1a

22 slug_url_kwarg = "username" 1a

23 

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

25 

26 

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

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

29 "name", 

30 "age", 

31 "bio", 

32 "country", 

33 "profile_pic", 

34 ] 

35 model = User 1a

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

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

38 

39 def get_success_url(self): 1a

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

41 return reverse( 1a

42 "user_detail", 

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

44 ) 

45 

46 def get_object(self): 1a

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

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

49 

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

51 

52 

53class UserRedirectView(LoginRequiredMixin, RedirectView): 1a

54 permanent = False 1a

55 

56 def get_redirect_url(self): 1a

57 return reverse( 1a

58 "user_detail", 

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

60 )