1.7.0 password recovery

This commit is contained in:
SDE
2024-09-20 16:43:33 +03:00
parent 19721cf5da
commit 7dc25532a3
6 changed files with 150 additions and 8 deletions

View File

@@ -23,6 +23,8 @@ urlpatterns = [
path('support_tickets/', support_tickets_ajax, name='support_tickets_ajax'),
path('password_recovery/', password_recovery_ajax, name='password_recovery_ajax'),
path('password_recovery_confirm/', password_recovery_confirm_ajax, name='password_recovery_confirm_ajax'),
path('change_profile/', change_profile_ajax, name='change_profile_ajax'),
path('change_profile_confirm/', change_profile_confirm_ajax, name='change_profile_confirm_ajax'),

View File

@@ -31,6 +31,120 @@ from GeneralApp.funcs import get_and_set_lang
# html = render_to_string('blocks/profile/b_subscribe.html', Dict, request=request)
# return JsonResponse({'html': html}, status=200)
def password_recovery_confirm_ajax(request):
if request.method != 'POST':
raise Http404
if not request.POST['pass'] or not request.POST['pass_confirm'] or not request.POST['user_id']:
raise Http404
lang = get_and_set_lang(request)
try:
if request.POST['pass'] != request.POST['pass_confirm']:
return JsonResponse({
'status': 'error',
'error': _('Пароли не совпадают')
}, status=400)
user = User.objects.get(id=request.POST['user_id'])
user.set_password(request.POST['pass'])
return JsonResponse({
'status': 'success',
'message': _('Пароль был успешно изменен')
})
except Exception as e:
return JsonResponse({
'status': 'error',
'error': str(e)
}, status=400)
def password_recovery_ajax(request):
if request.method != 'POST':
raise Http404
lang = get_and_set_lang(request)
try:
email = request.POST['email']
try:
user = User.objects.get(email=email)
except User.DoesNotExist:
return JsonResponse({
'status': 'error',
'error': _('Пользователь с указанным email не зарегистрирован на сайте')
}, status=400)
user.user_profile.authMailCode = uuid1().hex
user.user_profile.save(update_fields=['authMailCode'])
from GeneralApp.funcs_options import get_options_by_opt_types, get_mail_send_options
sets = get_options_by_opt_types(['domain', 'project_name'], only_vals=True)
subject = _('Восстановление пароля учетной записи на сайте tripwb.com')
mail_txt = _('Вы получили это письмо потому что '
'был произведен запрос на восстановление пароля '
'для данного email на сайте tripwb.com.<br>'
'<br>'
'Если Вы не выполняли запрос - просто проигнорируйте это письмо.<br><br>'
'Если же это были Вы и Вам требуется восстановить пароль от учетной записи - '
'перейдите по ссылке, указанной ниже.<br><br>')
link = sets["domain"] + f'reset_password/{str(user.id)}/{user.user_profile.authMailCode}/<br><br>'
sign_txt = _('Спасибо за то, что вы с нами!<br>'
'С уважением,<br>'
'Команда Trip With Bonus.<br>')
Dict = {
'logo': f'{sets["domain"]}/static/img/svg/LogoMobile.svg',
'project_name': sets['project_name'],
'message_title': subject,
'message_text': f'<p><b>{_("ДАННЫЕ ЗАПРОСА")}</b></p>'
f'<p style="padding-left: 20px; line-height: 30px;">'
f'{mail_txt}'
f'{link}'
f'{sign_txt}'
f'</p>'
}
html = render_to_string('mail/m_request_offer.html', Dict, request)
from BaseModels.mailSender import admin_send_mail_by_SMTPlib
mail_sets = get_mail_send_options()
to = [email]
res = admin_send_mail_by_SMTPlib(
mail_sets,
subject=subject,
from_email=mail_sets['sender_email'], to=to,
html_content=html
)
to = ['web@syncsystems.net']
res = admin_send_mail_by_SMTPlib(
mail_sets,
subject=subject,
from_email=mail_sets['sender_email'], to=to,
html_content=html
)
return JsonResponse({
'status': 'sended',
'message': _('На email') + ' ' + email + ' '
+ _('отправлено письмо с инструкциями для восстановления пароля')
})
except Exception as e:
return JsonResponse({
'status': 'error',
'error': str(e)
}, status=400)
def mailing_subscribe_ajax(request):
if request.method != 'POST':

View File

@@ -8,6 +8,9 @@ from django.contrib.auth import views
urlpatterns = [
path('registration/', registration_View, name='registration_page'),
path('reset_password/<int:user_id>/<str:token>/',
recovery_password_page_View, name='recovery_password_page'),
# path('', user_profile_View, name='user_profile'),
# path('page/chat/<int:user_id>/', chat_w_user_View, name='chat_w_user'),
# path('page/chat/', chat_w_user_View, name='chat_w_user_wo_user_id'),

View File

@@ -278,12 +278,19 @@ def decode_get_param(data):
def recovery_password_user(request, uidb64=None, token=None):
from django.contrib.auth.views import PasswordResetConfirmView
def recovery_password_page_View(request, user_id, token):
try:
user = User.objects.get(id=user_id, user_profile__authMailCode=token)
except User.DoesNotExist:
raise Http404
return PasswordResetConfirmView(request=request, uidb64=uidb64, token=token
)
Dict = {
'user': user
}
t = loader.get_template('pages/profile/p_user_profile.html')
response = get_inter_http_response(t, Dict, request)
return response