Advertisement
Guest User

Untitled

a guest
Mar 15th, 2019
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. from django.contrib import admin
  2. from django.urls import path, include
  3. from django.conf.urls.static import static
  4. from django.conf import settings
  5. from users.views import login_view, register_view, logout_view
  6.  
  7. # from django.contrib.auth import views as auth_views
  8. # from users import views as user_views
  9. from posts.views import index,postDetail, categoryDetail, blog, search
  10.  
  11. urlpatterns = [
  12. path('admin/', admin.site.urls),
  13. path('', index, name="home"),
  14. path('blog/', blog, name="blog"),
  15. path('search/', search, name='search'),
  16. path('<slug>/', postDetail, name='post-detail'),
  17. path('category/<slug>/', categoryDetail, name='category-detail'),
  18. path('login/', login_view),
  19. path('logout/', logout_view),
  20. path('register/', register_view),
  21. # path('register/', user_views.register, name='register'),
  22. # path('login/', auth_views.LoginView.as_view(template_name='users/login.html'), name='login'),
  23. # path('logout/', auth_views.LogoutView.as_view(template_name='users/logout.html'), name='logout'),
  24. path('tinymce/', include('tinymce.urls'))
  25. ]
  26.  
  27. LOGIN_REDIRECT_URL = 'home'
  28. LOGIN_URL = 'login'
  29.  
  30. from django import forms
  31. from django.contrib.auth import (
  32. authenticate,
  33. get_user_model
  34. )
  35. User = get_user_model()
  36.  
  37. class UserLoginForm(forms.Form):
  38. username = forms.CharField()
  39. password = forms.CharField(widget=forms.PasswordInput)
  40.  
  41. def clean(self, *args, **kwargs):
  42. username = self.cleaned_data.get('username')
  43. password = self.cleaned_data.get('password')
  44.  
  45. if username and password:
  46. user = authenticate(username = username, password=password)
  47. if not user:
  48. raise forms.ValidationError('This user does not exits')
  49. if not user.check_password(password):
  50. raise forms.ValidationError('Incorrect Password')
  51. if not user.is_active:
  52. raise forms.ValidationError('This user is not active')
  53. return super(UserLoginForm, self).clean(*args, **kwargs)
  54.  
  55. class UserRegisterForm(forms.ModelForm):
  56. email = forms.EmailField(label='Email Address')
  57. email2 = forms.EmailField(label= 'Confirm Email')
  58. password = forms.CharField(widget=forms.PasswordInput)
  59.  
  60. class Meta:
  61. model = User
  62. fields =[
  63. 'username',
  64. 'email',
  65. 'email2',
  66. 'password'
  67. ]
  68.  
  69. def clean(self, *args, **kwargs):
  70. email = self.cleaned_data.get('email')
  71. email2 = self.cleaned_data.get('email2')
  72. if email != email2:
  73. raise forms.ValidationError('email must match')
  74. eamil_qs = User.objects.filter(email=email)
  75. if eamil_qs.exists():
  76. raise forms.ValidationError(
  77. "This email is already being used"
  78. )
  79. return super(UserRegisterForm, self).clean(*args, **kwargs)
  80.  
  81. # Create your views here.
  82. from django.shortcuts import render, redirect
  83. from django.contrib.auth import (
  84. authenticate,
  85. get_user_model,
  86. login,
  87. logout
  88. )
  89.  
  90. from .forms import UserLoginForm, UserRegisterForm
  91. # Create your views here.
  92. def login_view(request):
  93. next = request.GET.get('next')
  94. form = UserLoginForm(request.POST or None)
  95. if form.is_valid():
  96. username = form.cleaned_data.get('username')
  97. password = form.cleaned_data.get('password')
  98. user = authenticate(username= username, password=password)
  99. login(request, user)
  100. if next:
  101. return redirect(next)
  102. return redirect('/')
  103. context = {
  104. 'form': form
  105. }
  106. return render(request, 'login.html', context)
  107.  
  108. def register_view(request):
  109. next = request.GET.get('next')
  110. form = UserRegisterForm(request.POST or None)
  111. if form.is_valid():
  112. user = form.save(commit=False)
  113. password = form.cleaned_data.get('password')
  114. user.set_password(password)
  115. user.save()
  116. new_user = authenticate(username= user.username, password=password)
  117. login(request, new_user)
  118. if next:
  119. return redirect(next)
  120. return redirect('/')
  121. context = {
  122. 'form': form
  123. }
  124. return render(request, 'signup.html', context)
  125.  
  126. def logout_view(request):
  127. logout(request)
  128. return redirect('/login')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement