def post(self, request): serializer = EmailSerializer(data=request.data) serializer.is_valid(raise_exception=True) email = serializer.data.get("email") user, created = CustomUser.objects.get_or_create(email=email) if created: user.username = email user.save() confirmation_code = default_token_generator.make_token(user) subject = "Регистрация в YaMDB" body = (f"Для продолжения регистрации {user.email} в YaMDB и\n" f"получения токена отправьте запрос на\n" f"{EMAIL_AUTH_URL} с\n" f"параметрами email и confirmation_code.\n\n" f"Ваш confirmation_code: {confirmation_code}\n") send_mail( subject, body, EMAIL_FROM_DEFOULT, [ user.email, ], fail_silently=False, ) return Response(serializer.data, status=200)
def post(self, request): serializer = EmailSerializer(data=request.data) if serializer.is_valid(): users = User.objects.filter(email=serializer['email']) if len(users) > 1: pass elif len(users) == 1: users[0] else: pass
def get_confirmation_code(request): serializer = EmailSerializer(data=request.data) serializer.is_valid(raise_exception=True) user = User.objects.get_or_create(email=serializer.data['email'], defaults={'username': uuid.uuid4(), 'email': serializer.data[ 'email']}) user = user[0] user.confirmation_code = default_token_generator.make_token(user) user.save() send_email(user) return Response(serializer.data, status=status.HTTP_200_OK)
def send_confirmation_code(self, request): serializer = EmailSerializer(data=request.data) serializer.is_valid(raise_exception=True) user, create = User.objects.get_or_create( username=serializer.data['username'], email=serializer.data['email'], role=serializer.data['role'], ) token = default_token_generator.make_token(user) mail_subject = 'Confirmation code' message = f'Используйте этот код для получения доступа: {token}' send_mail(mail_subject, message, NOREPLY_FREELANCE1_EMAIL, [user.email] ) return Response(serializer.data, status=status.HTTP_200_OK)
def put(self, request): data = request.data user = request.user email = data.get('email') serializer = EmailSerializer(instance=user, data=data) serializer.is_valid(raise_exception=True) serializer.save() # 发送邮件163授权码#RJUNSTDKWDMYWRLB # from django.core.mail import send_mail # subject = '美多商城邮箱验证' # message = '' # from_mail = '美多商城<*****@*****.**>' # verify_url = generate_verify_url(user.id, email) # html_massage = '<p>尊敬的用户您好!</p>' \ # '<p>感谢您使用美多商城。</p>' \ # '<p>您的邮箱为:%s 。请点击此链接激活您的邮箱:</p>' \ # '<p><a href="%s">%s<a></p>' % (email, verify_url, verify_url) # send_mail(subject=subject, # message=message, # from_email=from_mail, # recipient_list=[email], # html_message=html_massage) # 使用celery_tasks异步发送邮件 from celery_tasks.email.tasks import send_verify_email send_verify_email(user.id, email) return Response(serializer.data)
def post(self, request, format=None): serializer = EmailSerializer(data=request.data) if not serializer.is_valid(): return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) try: user = User.objects.get(email=serializer.validated_data["email"]) except User.DoesNotExist: pass else: send_mail( region=self.request.region, subject="Password reset", recipient_list=[user.email], template="mails/users/password_reset.html", context={ "domain": Site.objects.get_current().domain, "url": f"password/reset/{user.generate_uid()}/{user.generate_token()}", }, ) return Response(status=status.HTTP_204_NO_CONTENT)
def put(self, request): """ 登录用户的邮箱设置: 1. 获取登录用户 2. 获取email参数并进行校验(email必传,email格式) 3. 设置登录用户的邮箱并给邮箱发送验证邮件 4. 返回应答,邮箱设置成功 """ # 1. 获取登录用户 user = request.user # 2. 获取email参数并进行校验(email必传,email格式) serializer = EmailSerializer(user, data=request.data) serializer.is_valid(raise_exception=True) # 3. 设置登录用户的邮箱并给邮箱发送验证邮件(update) serializer.save() # 4. 返回应答,邮箱设置成功 return Response(serializer.data)
def get_serializer(self, *args, **kwargs): return EmailSerializer(self.request.user, data=self.request.data)