Coverage for accounts/views.py: 100.00%

28 statements  

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

1"""Views for djapi-library 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.urls import reverse, reverse_lazy 1a

8from django.views.generic import ( 1a

9 CreateView, 

10 DetailView, 

11 RedirectView, 

12 UpdateView, 

13) 

14 

15from .forms import CustomUserCreationForm 1a

16 

17User = get_user_model() 1a

18 

19 

20class SignupPageView(CreateView): 1a

21 form_class = CustomUserCreationForm 1a

22 success_url = reverse_lazy("account_login") 1a

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

24 

25 

26class UserDetailView(LoginRequiredMixin, DetailView): 1a

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

28 

29 model = User 1a

30 slug_field = "username" 1a

31 slug_url_kwarg = "username" 1a

32 

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

34 

35 

36class UserUpdateView(LoginRequiredMixin, UpdateView): 1a

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

38 "name", 

39 "bio", 

40 "country", 

41 "profile_pic", 

42 ] 

43 model = User 1a

44 

45 def get_success_url(self): 1a

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

47 return reverse( 1a

48 "user_detail", 

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

50 ) 

51 

52 def get_object(self): 1a

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

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

55 

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

57 

58 

59class UserRedirectView(LoginRequiredMixin, RedirectView): 1a

60 permanent = False 1a

61 

62 def get_redirect_url(self): 1a

63 return reverse( 1a

64 "user_detail", 

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

66 )