|
|
|
@ -6,7 +6,7 @@ import logging |
|
|
|
from django.contrib import auth |
|
|
|
from django.contrib import auth |
|
|
|
from django.conf import settings |
|
|
|
from django.conf import settings |
|
|
|
from django.contrib.auth import get_user_model |
|
|
|
from django.contrib.auth import get_user_model |
|
|
|
from django.core.mail import send_mail |
|
|
|
from django.core.mail import send_mail, EmailMessage |
|
|
|
from django.db.models import Q |
|
|
|
from django.db.models import Q |
|
|
|
from django.shortcuts import redirect |
|
|
|
from django.shortcuts import redirect |
|
|
|
|
|
|
|
|
|
|
|
@ -294,7 +294,6 @@ class MinUserView(APIView): |
|
|
|
|
|
|
|
|
|
|
|
class ManagementPassword(generics.GenericAPIView): |
|
|
|
class ManagementPassword(generics.GenericAPIView): |
|
|
|
permission_classes = (permissions.IsAuthenticated, permissions.IsAdminUser) |
|
|
|
permission_classes = (permissions.IsAuthenticated, permissions.IsAdminUser) |
|
|
|
serializer_class = UserEmailSerializer |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def post(self, request): |
|
|
|
def post(self, request): |
|
|
|
""" |
|
|
|
""" |
|
|
|
@ -302,20 +301,33 @@ class ManagementPassword(generics.GenericAPIView): |
|
|
|
--- |
|
|
|
--- |
|
|
|
Generate new password to the student and send email with new password |
|
|
|
Generate new password to the student and send email with new password |
|
|
|
""" |
|
|
|
""" |
|
|
|
serializer = self.get_serializer(data=request.JSON.dict()) |
|
|
|
email = request.JSON.get('email', None) |
|
|
|
serializer.is_valid(raise_exception=True) |
|
|
|
password = request.JSON.get('password', None) |
|
|
|
serializer.save() |
|
|
|
|
|
|
|
logger.info(f'set password: {serializer.password} to the ' |
|
|
|
if email is None: |
|
|
|
f'student with email: {serializer.user.email}') |
|
|
|
return Response('email not set', status=400) |
|
|
|
send_mail( |
|
|
|
|
|
|
|
|
|
|
|
if password is None: |
|
|
|
|
|
|
|
password = ''.join(random.choice(string.ascii_letters) for _x in range(8)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
|
|
|
|
|
user = get_user_model().objects.get(email=email) |
|
|
|
|
|
|
|
except get_user_model().DoesNotExist: |
|
|
|
|
|
|
|
return Response('user not found', status=404) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
user.set_password(password) |
|
|
|
|
|
|
|
user.save() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logger.info(f'set password: {password} to the ' |
|
|
|
|
|
|
|
f'student with email: {user.email}') |
|
|
|
|
|
|
|
EmailMessage( |
|
|
|
subject='Установлен новый пароль', |
|
|
|
subject='Установлен новый пароль', |
|
|
|
message=f'Ваш новый пароль {serializer.password} ' |
|
|
|
body=f'Ваш новый пароль {password} ' |
|
|
|
f'(в последствии вы сможите сменить его в личном кабинете).', |
|
|
|
f'(в последствии вы сможите сменить его в личном кабинете).', |
|
|
|
from_email='robo@skillbox.ru', |
|
|
|
from_email='robo@skillbox.ru', |
|
|
|
recipient_list=[serializer.user.email], |
|
|
|
to=[user.email], |
|
|
|
|
|
|
|
bcc=[request.user.email], |
|
|
|
) |
|
|
|
) |
|
|
|
logger.info(f'send email to {serializer.user.email} ' |
|
|
|
|
|
|
|
f'with new password') |
|
|
|
|
|
|
|
return Response( |
|
|
|
return Response( |
|
|
|
data={'message': 'Письмо с новым паролем отправлено на email студента.'}, |
|
|
|
data={'message': 'Письмо с новым паролем отправлено на email студента.'}, |
|
|
|
status=201 |
|
|
|
status=201 |
|
|
|
|