-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathviews.py
67 lines (51 loc) · 2.24 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.views import LoginView, PasswordChangeView
from django.core.exceptions import PermissionDenied
from django.http import HttpRequest, HttpResponse, request
from django.shortcuts import render
from django.urls import reverse_lazy
from django.views.generic import CreateView, UpdateView
from snippets.models import Snippet
from tickets.models import Comment, Ticket
from .forms import (CustomPasswordChangeForm, CustomUserCreationForm,
CustomUserUpdateForm)
from .models import User
class SignUpView(CreateView):
form_class = CustomUserCreationForm
success_url = reverse_lazy('login')
template_name = 'registration/signup.html'
def get(self, request: HttpRequest, *args: str, **kwargs) -> HttpResponse:
if request.user.is_authenticated:
raise PermissionDenied()
else:
return super().get(request, *args, **kwargs)
class ProfileView(LoginRequiredMixin, UpdateView):
model = User
form_class = CustomUserUpdateForm
template_name = "profile.html"
success_url = reverse_lazy('profile')
def get_object(self, *args, **kwargs):
user = User.objects.get(username=self.request.user)
return user
def get_context_data(self, *args, **kwargs):
context = super(ProfileView, self).get_context_data(*args, **kwargs)
context['snippets'] = Snippet.objects.filter(
created_by=self.request.user.id
).order_by('-created_on')
context['tickets'] = Ticket.objects.filter(
created_by=self.request.user.id
).order_by('-created_on')
context['comments'] = Comment.objects.filter(
created_by=self.request.user.id
).order_by('-created_on')
return context
class CustomPasswordChangeView(LoginRequiredMixin, PasswordChangeView):
template_name = 'registration/change_password.html'
success_url = reverse_lazy('home')
form_class = CustomPasswordChangeForm
class CustomLoginView(LoginView):
def get(self, request: HttpRequest, *args: str, **kwargs):
if request.user.is_authenticated:
raise PermissionDenied()
else:
return super().get(request, *args, **kwargs)