def send_email(self, type, user, token, current_site): if type == 'register': # subject for mail self.subject = "Activate Your ChatApp Account" uid = urlsafe_base64_encode(user.pk) s = get_short_url(uid) print(s.short_id) # message body for mail self.message = render( request, 'account_activate.html', { 'user': user, 'domain': current_site.domain, 'uid': urlsafe_base64_encode(force_bytes(user.pk)), 'token': token, 's': s }) # emails of sender and receiver # from_email = settings.EMAIL_HOST_USER self.to_send = [user.email] # send mail method # send_mail(subject, message, from_email, to_send, fail_silently=False) # for type reset password elif type == 'reset_password': # subject for mail self.subject = "RESET YOUR PASSWORD" # message body for mail self.message = render_to_string( 'password_reset.html', { 'user': user, 'domain': "localhost:3000", 'uid': urlsafe_base64_encode(force_bytes(user.pk)), 'token': token }) # emails of sender and receiver # from_email = settings.EMAIL_HOST_USER self.to_send = [user.email] email_data = { 'subject': self.subject, 'message': self.message, 'email': self.to_send } Message = json.dumps(email_data) self.channel.basic_publish( exchange='', routing_key='mailQueue', body=Message, properties=pika.BasicProperties(delivery_mode=2)) self.connection.close()
def post(self, request, *args, **kwargs): """ :param request: here is post request por set password :return: in this function we take email from user and send toaken for verification """ try: if not 'email' in request.data: raise KeyError('email is missing') email = request.data['email'] print(email) if email == "": raise KeyError('email field not be blank') if not validate_email(email): raise ValueError user = User.objects.get(email=email) if user: payload = { 'username': user.username, 'email': user.email, } token = Jwt().register_token(payload) long_url = reverse('reset_password', args=[token]) short_url = get_short_url(long_url) # Url object message = render_to_string( 'users/email_template.html', { 'name': user.username, 'domain': get_current_site(request).domain, 'url': short_url.short_id }) recipient_list = [ user.email, ] email_event.emit("reset_password_event", message, recipient_list) smd = Smd_Response( True, 'you"re email is verified for reset password check you"re email', status_code=200) return smd else: smd = Smd_Response(False, 'you are not valid user register first', []) logger.warning( 'not valid user warning from users.views.Reset_password_api' ) except ObjectDoesNotExist as e: logger.warning('email not registered' + str(e)) smd = Smd_Response(False, 'this email id not registered', []) except ValueError as e: smd = Smd_Response(False, 'please provide valid email address', []) logger.warning('not valid email address' + str(e)) except KeyError as error: smd = Smd_Response(False, str(error), []) logger.warning('input is blank ' + str(error)) except Exception as e: logger.warning('something is wrong ' + str(e)) smd = Smd_Response() return smd
def post(self, request, *args, **kwargs): """ :purpose: in this function we register a new user via sending jwt token on email :param request: here we get post request :return:in this function we take user input for registration and sent mail to email id """ try: # pdb.set_trace() serializer = UserSerializer(data=request.data) if serializer.is_valid(): user = serializer.save() user.is_active = False user.save() if user: payload = { 'username': self.request.data['username'], 'email': self.request.data['email'], } token = Jwt().register_token(payload) long_url = reverse('activate', args=[token]) short_url = get_short_url(long_url) # Url object message = render_to_string( 'users/token.html', { 'name': user.username, 'domain': get_current_site(request).domain, 'url': short_url.short_id }) recipient_list = [ self.request.data['email'], ] response = Smd_Response( True, 'you registered successfully for activate your account please check ' 'your email', status_code=200) email_event.emit("account_activate_event", message, recipient_list) return response response = Smd_Response(False, 'you are not validated try again', []) return response logger.warning( 'not valid input warning from users.views.register_api') return Smd_Response(message=serializer.errors) except Exception as e: logger.warning('something was wrong ' + str(e)) smd = Smd_Response() return smd
def get_short_link(request): full_url = request.POST.get('full_url', '') if not full_url: return JsonResponse({ "error": "full_url is empty", "data": "" }, status=400) if len(full_url) > Url._meta.get_field("url").max_length: return JsonResponse({ "error": "full_url is too long", "data": "" }, status=400) url = get_short_url(full_url) return JsonResponse({ "error": "", "data": { "short_id": url.short_id, "short_url_path": settings.SHORT_URL_PATH } })
def sendmail(self, type, user, token, current_site): """ :param type: type of mail to be sent :param user: user the mail will be sent to :param token: token to be sent in url in message :param current_site: current site link in mail will redirect to :return: """ # for type register if type == 'register': # subject for mail subject = "Activate Your ChatApp Account" uid = urlsafe_base64_encode(user.pk) s = get_short_url(uid) print(s.short_id) # message body for mail message = render( request, 'account_activate.html', { 'user': user, 'domain': current_site.domain, 'uid': urlsafe_base64_encode(force_bytes(user.pk)), 'token': token, 's': s }) # emails of sender and receiver from_email = settings.EMAIL_HOST_USER to_send = [user.email] # send mail method send_mail(subject, message, from_email, to_send, fail_silently=False) # for type reset password elif type == 'reset_password': # subject for mail subject = "RESET YOUR PASSWORD" # message body for mail message = render_to_string( 'password_reset.html', { 'user': user, 'domain': "localhost:3000", 'uid': urlsafe_base64_encode(force_bytes(user.pk)), 'token': token }) # emails of sender and receiver from_email = settings.EMAIL_HOST_USER to_send = [user.email] # send mail method send_mail(subject, message, from_email, to_send, fail_silently=False) else: return None
def test_get_full_url(self): url = get_short_url(self.site) site = get_full_url(url.short_id) self.assertEqual(url.url, site.url)
def test_invalidate_url(self): url = get_short_url(self.site) self.assertEqual(url.is_expired, False) invalidate_url(url.short_id) self.assertEqual( Url.objects.get(short_id=url.short_id).is_expired, True)
def test_get_short_url_cache(self): url1 = get_short_url(self.site) url2 = get_short_url(self.site) self.assertEqual(url1.short_id, url2.short_id)
def test_get_short_url(self): url = get_short_url(self.site) self.assertEqual(url.url, self.site)