0.0.16
login routines
This commit is contained in:
@@ -9,6 +9,11 @@ from .models import *
|
||||
# from datetimepicker.widgets import DateTimePicker
|
||||
# from datetimepicker.helpers import js_loader_url
|
||||
|
||||
|
||||
class LoginForm(forms.Form):
|
||||
username = forms.CharField(required=True)
|
||||
password = forms.CharField(required=True)
|
||||
|
||||
class RegistrationForm(forms.Form):
|
||||
from .models import account_type_choices
|
||||
account_type = forms.ChoiceField(choices=account_type_choices, initial='sender', required=True)
|
||||
|
||||
@@ -7,4 +7,7 @@ from django.contrib.auth import views
|
||||
|
||||
urlpatterns = [
|
||||
path('registration/', registration_ajax, name='registration_ajax'),
|
||||
path('login/', login_ajax, name='login_ajax'),
|
||||
|
||||
path('new_route_view/', new_route_view_ajax, name='new_route_view_ajax'),
|
||||
]
|
||||
@@ -12,6 +12,69 @@ from datetime import datetime
|
||||
from django.template.loader import render_to_string
|
||||
from django.urls import reverse
|
||||
|
||||
|
||||
|
||||
def new_route_view_ajax(request):
|
||||
if request.method != 'POST':
|
||||
raise Http404
|
||||
|
||||
from RoutesApp.forms import RegistrationForm
|
||||
Dict = {
|
||||
'form': RegistrationForm()
|
||||
}
|
||||
|
||||
html = render_to_string('blocks/profile/b_new_route.html', Dict, request=request)
|
||||
return JsonResponse({'html': html}, status=200)
|
||||
|
||||
|
||||
def login_ajax(request):
|
||||
if request.method != 'POST':
|
||||
raise Http404
|
||||
|
||||
try:
|
||||
|
||||
data = request.POST
|
||||
|
||||
from .forms import LoginForm
|
||||
form = LoginForm(data)
|
||||
if not form.is_valid():
|
||||
Dict = {'form': form}
|
||||
html = render_to_string('forms/f_registration.html', Dict, request=request)
|
||||
return JsonResponse({'html': html}, status=400)
|
||||
|
||||
from django.contrib.auth import authenticate
|
||||
user = authenticate(username=form.data['username'], password=form.data['password'])
|
||||
if user is not None:
|
||||
auth.login(request, user)
|
||||
else:
|
||||
errors_Dict = {
|
||||
'errors': {
|
||||
'__all__': f'неверный логин и\или пароль'
|
||||
}
|
||||
}
|
||||
Dict = {'form': errors_Dict}
|
||||
html = render_to_string('forms/f_login.html', Dict)
|
||||
return JsonResponse({'html': html}, status=400)
|
||||
|
||||
|
||||
res_Dict = {
|
||||
'redirect_url': reverse('user_profile')
|
||||
}
|
||||
|
||||
return JsonResponse(res_Dict)
|
||||
|
||||
except Exception as e:
|
||||
|
||||
errors_Dict = {
|
||||
'errors': {
|
||||
'__all__': f'ошибка в запросе = {str(e)}'
|
||||
}
|
||||
}
|
||||
Dict = {'form': errors_Dict}
|
||||
html = render_to_string('forms/f_login.html', Dict)
|
||||
return JsonResponse({'html': html}, status=400)
|
||||
|
||||
|
||||
def registration_ajax(request):
|
||||
if request.method != 'POST':
|
||||
raise Http404
|
||||
|
||||
Reference in New Issue
Block a user