def post(self, request): user = request.data serializer = self.serializer_class(data=user) serializer.is_valid(raise_exception=True) serializer.save() user_data = serializer.data user = User.objects.get(email=user_data['email']) #get token token = RefreshToken.for_user(user).access_token ########### SEND TO VERIFY EMAIL ################# current_site = get_current_site(request).domain relativeLink = reverse('email-verify') absurl = 'http://' + current_site + relativeLink + "?token=" + str( token) #email data email_body = 'Hi '+user.username + \ ' Use the link below to verify your email \n' + absurl data = { 'email_body': email_body, 'to_email': user.email, 'email_subject': 'Verify your email' } Util.send_email(data) ############################################3 return Response(user_data, status=status.HTTP_201_CREATED)
def post(self, request): serializer = self.serializer_class(data=request.data) email = request.data.get('email', '') if User.objects.filter(email=email).exists(): user = User.objects.get(email=email) uidb64 = urlsafe_base64_encode(smart_bytes(user.id)) token = PasswordResetTokenGenerator().make_token(user) current_site = get_current_site(request=request).domain relativeLink = reverse('password-reset-confirm', kwargs={ 'uidb64': uidb64, 'token': token }) absurl = 'http://' + current_site + relativeLink email_body = 'Hello, \n Use link below to reset your password \n' + absurl data = { 'email_body': email_body, 'to_email': user.email, 'email_subject': 'Reset your passsword' } Util.send_email(data) return Response( {'success': 'We have sent you a link to reset your password'}, status=status.HTTP_200_OK)
def create(self, request): try: serializer = UserSerializer(data=request.data, context={"request": request}) serializer.is_valid() serializer.save() user_data = serializer.data user = CustomUser.objects.get(email=user_data['email']) token = RefreshToken.for_user(user).access_token current_site = get_current_site(request) relativeLink = reverse('email-verify') absurl = 'http://'+str(current_site) + \ relativeLink+'?token='+str(token) email_body = 'Hello '+user.name + \ ' Use below link to verify your email \n' + absurl data = { 'email_body': email_body, 'to_email': user.email, 'email_subject': 'Verify your Email!' } Util.send_email(data) return Response(user_data, status=status.HTTP_201_CREATED) except Exception as e: return Response({"error": str(e)}, status=HTTP_500_INTERNAL_SERVER_ERROR)
def post(self, request): """ Generate jwt token ,create verification url and send it to user email """ user = request.data serializer = self.serializer_class(data=user) serializer.is_valid(raise_exception=True) serializer.save() user_data = serializer.data user = User.objects.get(email=user_data['email']) # generating token using user information payload = jwt_payload_handler(user) token = jwt.encode(payload, settings.SECRET_KEY).decode('UTF-8') user_data['token'] = token # creating email verification link current_site = get_current_site(request).domain relative_link = reverse('verify-email') absurl = 'http://' + current_site + relative_link + '?token=' + str( token) # shortening of verification link shortener = pyshorteners.Shortener() verification_link = shortener.tinyurl.short(absurl) email_body = 'Hii \n' + user.username + ' Use this below to verify your email \n' + verification_link data = { 'email_body': email_body, 'to_email': user.email, 'email_subject': 'Verify you email' } Util.send_email(data) return Response(user_data, status=status.HTTP_201_CREATED)
def post(self, request): """ Get user email and generate jwt token and send it to the user by email """ serializer = self.serializer_class(data=request.data) serializer.is_valid(raise_exception=True) user_data = serializer.data user = User.objects.get(email=user_data['email']) current_site = get_current_site(request).domain reverse_link = reverse('new-pass') payload = jwt_payload_handler(user) token = jwt.encode(payload, settings.SECRET_KEY).decode('UTF-8') user_data['token'] = token shortener = pyshorteners.Shortener() reset_link = shortener.tinyurl.short('http://' + current_site + reverse_link + '?token=' + token) email_body = "hii \n" + user.username + "Use this link to reset password: \n" + reset_link data = { 'email_body': email_body, 'to_email': user.email, 'email_subject': "Reset password Link" } Util.send_email(data) return Response(user_data, status=status.HTTP_200_OK)
def retrieve(self, request, *args, **kwargs): instance = self.get_object() serializer = self.get_serializer(instance) data = serializer.data ########## DATA MANIPULATION ######### #page size 595.27,841.89 # Create a file-like buffer to receive PDF data. buffer = io.BytesIO() # Create the PDF object, using the buffer as its "file." p = canvas.Canvas(buffer) #Title orderNum = str(17*data['id']) p.setFont("Helvetica", 20) p.setFillColorRGB(0,0,1) p.setStrokeColorRGB(0.2,0.5,0.3) p.drawString(170, 800, ("Sabanci Thrift Shop Invoice #" + orderNum ))#Multiplied with 17 so users won't know their actual id's p.setFont("Helvetica", 14) p.setFillColorRGB(0,0,0) p.setStrokeColorRGB(0.2,0.5,0.3) yVal = 720 buyerInfo = User.objects.get(id=data['buyer']) #get the user info with buyer's id buyerName = buyerInfo.first_name + " " + buyerInfo.last_name p.drawString(100, 740, ("Buyer Name: " + str(buyerName))) #buyer = str(data['buyer']) #p.drawString(100, yVal, buyer) #yVal -= 20 totalPrice = 0.0 dateSold = str(data['dateSold']) p.drawString(100, yVal, dateSold) for prod in data['products']: productName = str(prod['productName']) yVal -= 40 p.drawString(100, yVal, productName) yVal -= 20 ownerInfo = User.objects.get(id=prod['owner']) #get the user info with buyer's id ownerName = ownerInfo.first_name + " " + ownerInfo.last_name #owner = str(prod['owner']) p.drawString(100, yVal, ("Owner: " + ownerName)) yVal -= 20 totalPrice += float(prod['price']) price = str(prod['price']) p.drawString(100, yVal, price) p.drawString(400, 60, ("Total Price: " + str(totalPrice))) #Draw Lines p.line(50,800,50,50) p.line(545,800,545,50) # Close the PDF object cleanly, and we're done. p.showPage() p.save() # FileResponse sets the Content-Disposition header so that browsers # present the option to save the file. buffer.seek(0) user_data = serializer.data user = User.objects.get(email=user_data['email']) email_data={'email_body':p,'to_email':user.email,'email_subject': 'Order invoice','attachment':p} Util.attachement_email(email_data) return FileResponse(buffer, as_attachment=True, filename='Invoice.pdf')