Ejemplo n.º 1
0
    def send_verify_email(self, register_id):

        try:
            register_user = Register.objects.get(pk=register_id)
            result = 'Retrieved register id:{0}'.format(register_id)
            logger.debug(result)
        except ObjectDoesNotExist:
            result = 'Failed getting register id:{0}'.format(register_id)
            logger.critical(result)
            return ReturnResponse.Response(1, __name__, 'failed', result).return_json()

        try:
            email_template = EmailTemplate.objects.get(pk=EMAIL_TEMPLATE['VERIFY'])
            result = 'Getting EMAIL_TEMPLATE VERIFY :{0} from DB:'.format(email_template.HTML_Filename)
            logger.debug(result)
        except ObjectDoesNotExist:
            result = 'Failed getting EMAIL_TEMPLATE record #:{0} from DB:'.format(EMAIL_TEMPLATE['VERIFY'])
            logger.critical(result)
            return ReturnResponse.Response(1, __name__, 'failed', result).return_json()

        notification = Notification()
        notification.To_Id = register_user.pk
        notification.Message_Id = email_template.pk
        notification.Type_Id = NotificationType(pk=NOTIFICATION_TYPE['EMAIL'])

        try:
            self.load_template(email_template.HTML_Filename.name)
            result = 'read email template file:{0}'.format(email_template.HTML_Filename.name)
            logger.debug(result)
        except:
            result = 'Failed reading email template:{0}'.format(email_template.HTML_Filename.name)
            logger.critical(result)
            return ReturnResponse.Response(1, __name__, 'failed', result).return_json()

        self.subject = email_template.Subject
        self.fromEmail = email_template.From + '@' + EMAIL_FROM_DOMAIN
        self.toEmail = register_user.Email
        self.replace_string_in_template('EMAIL_VERIFY_TRACK_URL',
                                        EMAIL_VERIFY_TRACK_URL + register_user.Authorization_Code)
        self.replace_string_in_template('EMAIL_VERIFY_ACCOUNT_URL',
                                        EMAIL_VERIFY_ACCOUNT_URL + register_user.Authorization_Code)
        self.replace_string_in_template('FIRST_NAME', register_user.First_Name + " ")
        self.replace_string_in_template('LAST_NAME', register_user.Last_Name)

        if self.send() == 1:
            notification.Status_Id = NotificationStatus.objects.get(pk=NOTIFICATION_STATUS['SENT'])
            register_user.Status_Id = RegisterStatus.objects.get(pk=REGISTER_STATUS['SENT'])
            register_user.save()
            result = 'Registered User and sent email to user ID:{0}'.format(register_user.Status_Id)
            logger.debug(result)
        else:
            notification.Status_Id = NotificationStatus.objects.get(pk=NOTIFICATION_STATUS['FAILED'])
            result = 'Failed sending email:{0} to user ID:{1}'.format(email_template.HTML_Filename.name,
                                                                      register_user.Status_Id)
            logger.error(result)
            return ReturnResponse.Response(1, __name__, 'failed', result).return_json()
        notification.save()
        result = 'Verify email sent to:{}'.format(self.toEmail)
        return ReturnResponse.Response(0, __name__, 'success', result).return_json()
Ejemplo n.º 2
0
    def load_template(self, template_filename):

        filename = join(EMAIL_TEMPLATE_DIR, template_filename)
        with open(filename, "rt") as template:
            self.body = template.read()

        if len(self.body) > 10:
            result = 'Read Email Template:{0}'.format(filename)
            logger.debug(result)
            return ReturnResponse.Response(0, __name__, 'success', result).return_json()
        else:
            result = 'Failed Reading Email Template:{0}'.format(filename)
            logger.critical(result)
            return ReturnResponse.Response(1, __name__, 'failed', result).return_json()
Ejemplo n.º 3
0
    def get(self, request, *args, **kwargs):
        authorization_code = kwargs.get('Authorization_Code', 0)

        my_email = MyEmail('Send New Password')
        result = my_email.send_reset_password_email(authorization_code)

        return Response(ReturnResponse.Response(0, __name__, 'success',
                                                result).return_json(),
                        status=status.HTTP_200_OK)
Ejemplo n.º 4
0
 def get(self, request, *args, **kwargs):
     if request.user.is_authenticated:
         user_id = kwargs.get('User_Id', 0)
         user = CustomUser.objects.get(pk=user_id)
         result = "email:{0}".format(user.Email)
         logger.debug(result)
         return Response(ReturnResponse.Response(0, __name__, 'success',
                                                 result).return_json(),
                         status=status.HTTP_200_OK)
Ejemplo n.º 5
0
    def post(self, request, *args, **kwargs):
        apiKey = kwargs.get('ApiKey', 0)
        tempHumidity = ""

        try:
            print(request.body.decode("utf-8"))
            tempHumidity = json.loads(request.body.decode("utf-8"))
            result = 'Parsed JSON:{0}'.format(tempHumidity)
            logger.debug(result)
        except:
            result = 'Failed to parse JSON:{0}'.format(
                request.body.decode("utf-8"))
            logger.error(result)
            return Response(ReturnResponse.Response(1, __name__, 'failed',
                                                    result).return_json(),
                            status=status.HTTP_200_OK)

        try:
            print('API Key=' + apiKey)
            print('Temperature=' + tempHumidity['Temperature'])
            print('Humidity=' + tempHumidity['Humidity'])
            print('Time=' + tempHumidity['Time'])

        except:
            result = 'failed reading data'
            logger.error(result)
            return Response(ReturnResponse.Response(1, __name__, 'failed',
                                                    result).return_json(),
                            status=status.HTTP_200_OK)

        serializer = TemperatureSerializer(data=tempHumidity)
        if serializer.is_valid():
            temperature = Temperature()
            temperature.Temperature = serializer.Temperature
            temperature.Sent = serializer.Sent
            temperature.save()
            return Response(ReturnResponse.Response(0, __name__, 'success',
                                                    "done").return_json(),
                            status=status.HTTP_200_OK)
        else:
            return Response(ReturnResponse.Response(
                1, __name__, 'failed saving temperature',
                result).return_json(),
                            status=status.HTTP_200_OK)
Ejemplo n.º 6
0
    def send_reset_password_email(self, authorization_code):

        try:
            password_reset = PasswordReset.objects.get(Authorization_Code=authorization_code)
            result = 'Read authorization code from DB:{0}'.format(authorization_code)
            logger.debug(result)
        except ObjectDoesNotExist:
            result = 'Failed Finding Authorization Code in Password Reset:{0}'.format(authorization_code)
            logger.info(result)
            return ReturnResponse.Response(0, __name__, 'failed', result).return_json()

        if authorization_code != password_reset.Authorization_Code:
            result = 'Codes do not match.  Request password again.:{0}<>{1}'.format(authorization_code,
                                                                                    password_reset.Authorization_Code)
            logger.info(result)
            return ReturnResponse.Response(0, __name__, 'failed', result).return_json()

        if password_reset.Status_Id.pk == PASSWORD_RESET_STATUS['FINISHED']:
            result = 'Already Used this request.  Request password again. ID:{0}'.format(password_reset.Status_Id.pk)
            logger.info(result)
            return ReturnResponse.Response(0, __name__, 'failed', result).return_json()

        password_reset.Clicked = time.time()
        password_reset.Status_Id = PasswordResetStatus.objects.get(pk=PASSWORD_RESET_STATUS['FINISHED'])
        password_reset.save()

        try:
            email_template = EmailTemplate.objects.get(pk=EMAIL_TEMPLATE['RESET'])
            result = 'retrieve email template file:{0}'.format(email_template.HTML_Filename.name)
            logger.debug(result)
        except ObjectDoesNotExist:
            result = 'Failed retrieving email RESET template:{0}'.format(EMAIL_TEMPLATE['RESET'])
            logger.critical(result)
            return ReturnResponse.Response(1, __name__, 'failed', result).return_json()

        notification = Notification()
        notification.To_Id = password_reset.User_Id.pk
        notification.Message_Id = email_template.pk
        notification.Type_Id = NotificationType.objects.get(pk=NOTIFICATION_TYPE['EMAIL'])

        try:
            self.load_template(email_template.HTML_Filename.name)
            result = 'read email template file:{0}'.format(email_template.HTML_Filename.name)
            logger.debug(result)
        except:
            result = 'Failed reading email template:{0}'.format(email_template.HTML_Filename.name)
            logger.critical(result)
            return ReturnResponse.Response(1, __name__, 'failed', result).return_json()

        self.subject = email_template.Subject
        self.fromEmail = email_template.From + '@' + EMAIL_FROM_DOMAIN

        try:
            user = CustomUser.objects.get(Id=password_reset.User_Id.pk)
            result = 'Read email address from user:{0}'.format(user.Email)
            logger.debug(result)
        except ObjectDoesNotExist:
            result = 'Failed reading user email address from User ID:{0}'.format(password_reset.User_Id)
            logger.critical(result)
            return ReturnResponse.Response(1, __name__, 'failed', result).return_json()

        self.toEmail = user.Email

        try:
            name = Name.objects.get(User_Id=user.Id, Type_Id=NameType.objects.get(pk=NAME_TYPE['FIRST']))
            first_name = name.Name
            result = 'Read user id and first name from password reset:{0}'.format(name.Id)
            logger.debug(result)
        except ObjectDoesNotExist:
            first_name = ""
            result = 'Failed reading user id and first name from password reset:{0}'.format(user.Id)
            logger.error(result)

        new_password = self.generate_password()
        user.set_password(new_password)
        user.save()
        self.replace_string_in_template('NEW_PASSWORD', str(new_password))
        self.subject = self.subject.replace('NAME', first_name)
        self.replace_string_in_template('EMAIL_LOGIN_URL', str(EMAIL_LOGIN_URL))
        if self.send() == 1:
            notification.Status_Id = NotificationStatus.objects.get(pk=NOTIFICATION_STATUS['SENT'])
            result = 'Reset Password Email Sent to:{0}'.format(user.Email)
            logger.debug(result)
        else:
            notification.Status_Id = NotificationStatus.objects.get(pk=NOTIFICATION_STATUS['FAILED'])
            result = 'Failed sending Reset Password Email to:{0}'.format(user.Email)
            logger.error(result)
        notification.save()
        return ReturnResponse.Response(0, __name__, 'success', result).return_json()
Ejemplo n.º 7
0
    def send_forgot_password_email(self, user):

        try:
            password_reset = PasswordReset.objects.get(User_Id=user, Status_Id=PASSWORD_RESET_STATUS['ACTIVE'])
            result = 'Loaded password reset for user Id:{0}'.format(user.pk)
            logger.debug(result)
        except ObjectDoesNotExist:
            result = 'Failed Loading password reset for user Id:{0}'.format(user.pk)
            logger.critical(result)
            return ReturnResponse.Response(1, __name__, 'failed', result).return_json()

        password_reset.Clicked = time.time()
        password_reset.save()

        if password_reset.Status_Id == PASSWORD_RESET_STATUS['EXPIRED']:
            result = 'This Request has expired!  Click forgot password again.'
            logger.debug(result)
            return ReturnResponse.Response(0, __name__, 'success', result).return_json()

        if (password_reset.Clicked - password_reset.Inserted) > timedelta(1):
            password_reset.Status_Id = PasswordResetStatus(pk=PASSWORD_RESET_STATUS['EXPIRED'])
            password_reset.save()
            result = 'This Request has expired!  Click forgot password again.'
            logger.debug(result)
            return ReturnResponse.Response(0, __name__, 'success', result).return_json()

        if password_reset.Status_Id == PASSWORD_RESET_STATUS['CLICKED']:
            result = 'This Request has already been used!  Click forgot password again.'
            logger.debug(result)
            return ReturnResponse.Response(0, __name__, 'success', result).return_json()

        if password_reset.Status_Id == PASSWORD_RESET_STATUS['ACTIVE']:
            password_reset.Status_Id = PasswordResetStatus(pk=PASSWORD_RESET_STATUS['CLICKED'])
            password_reset.save()

        try:
            email_template = EmailTemplate.objects.get(pk=EMAIL_TEMPLATE['FORGOT'])
            result = 'Loaded email template FORGOT:{0}'.format(email_template.HTML_Filename.name)
            logger.debug(result)
        except ObjectDoesNotExist:
            result = 'Failed Loading email template FORGOT ID{0}:'.format(EMAIL_TEMPLATE['FORGOT'])
            logger.critical(result)
            return ReturnResponse.Response(1, __name__, 'failed', result).return_json()

        notification = Notification()
        notification.To_Id = user.pk
        notification.Message_Id = email_template.pk

        try:
            notification.Type_Id = NotificationType.objects.get(pk=NOTIFICATION_TYPE['EMAIL'])
            result = 'Loaded Notification type EMAIL:{0}'.format(NOTIFICATION_TYPE['EMAIL'])
            logger.debug(result)
        except ObjectDoesNotExist:
            result = 'Failed Loading Notification type EMAIL:{0}'.format(NOTIFICATION_TYPE['EMAIL'])
            logger.critical(result)
            return ReturnResponse.Response(1, __name__, 'failed', result).return_json()

        try:
            self.load_template(email_template.HTML_Filename.name)
            result = 'read email template file:{0}'.format(email_template.HTML_Filename.name)
            logger.debug(result)
        except:
            result = 'Failed reading email template:{0}'.format(email_template.HTML_Filename.name)
            logger.critical(result)
            return ReturnResponse.Response(1, __name__, 'failed', result).return_json()

        self.subject = email_template.Subject
        self.fromEmail = email_template.From + '@' + EMAIL_FROM_DOMAIN
        self.toEmail = user.Email

        try:
            name = Name.objects.get(User_Id=user, Type_Id=NameType.objects.get(pk=NAME_TYPE['FIRST']))
            first_name = name.Name
            result = 'Read User from DB:{0}'.format(first_name)
            logger.debug(result)
        except ObjectDoesNotExist:
            first_name = ""
            result = 'Failed reading user first name from DB UserId:{0}'.format(user.pk)
            logger.critical(result)

        self.subject = self.subject.replace('NAME', first_name)
        self.replace_string_in_template('PASSWORD_RESET_URL', PASSWORD_RESET_URL + password_reset.Authorization_Code)

        if self.send() == 1:
            notification.Status_Id = NotificationStatus.objects.get(pk=NOTIFICATION_STATUS['SENT'])
            result = 'Forgot Password Email Sent to:{0}'.format(user.Email)
            logger.debug(result)
        else:
            notification.Status_Id = NotificationStatus.objects.get(pk=NOTIFICATION_STATUS['FAILED'])
            result = 'Failed sending Forgot Password Email to:{0}'.format(user.Email)
            logger.error(result)
        notification.save()
        return ReturnResponse.Response(0, __name__, 'success', result).return_json()
Ejemplo n.º 8
0
    def send_welcome_email(self, new_user, new_password):

        try:
            new_user = CustomUser.objects.get(pk=new_user.pk)
            result = 'Loaded New User in Users: Id:{0}'.format(new_user.pk)
            logger.debug(result)
        except ObjectDoesNotExist:
            result = 'Could not find New User in Users: Id:{0}'.format(new_user.pk)
            logger.error(result)
            return ReturnResponse.Response(1, __name__, 'failed', result).return_json()

        try:
            email_template = EmailTemplate.objects.get(pk=EMAIL_TEMPLATE['WELCOME'])
            result = 'Loaded Email Template WELCOME:{0}'.format(email_template.HTML_Filename.name)
            logger.debug(result)
        except ObjectDoesNotExist:
            result = 'Could not find New User in Users: Id:{0}'.format(new_user.pk)
            logger.error(result)
            return ReturnResponse.Response(1, __name__, 'failed', result).return_json()

        notification = Notification()
        notification.To_Id = new_user.pk
        notification.Message_Id = email_template.pk

        try:
            notification.Type_Id = NotificationType.objects.get(pk=NOTIFICATION_TYPE['EMAIL'])
            result = 'Loaded email type notification: Id:{0}'.format(NOTIFICATION_TYPE['EMAIL'])
            logger.debug(result)
        except ObjectDoesNotExist:
            result = 'Failed loading email type notification: Id:{0}'.format(NOTIFICATION_TYPE['EMAIL'])
            logger.error(result)
            return ReturnResponse.Response(1, __name__, 'failed', result).return_json()

        try:
            self.load_template(email_template.HTML_Filename.name)
            result = 'read email template file:{0}'.format(email_template.HTML_Filename.name)
            logger.debug(result)
        except:
            result = 'Failed reading email template:{0}'.format(email_template.HTML_Filename.name)
            logger.critical(result)
            return ReturnResponse.Response(1, __name__, 'failed', result).return_json()

        self.subject = email_template.Subject
        self.fromEmail = email_template.From + '@' + EMAIL_FROM_DOMAIN

        try:
            email_address_status = EmailAddressStatus.objects.get(pk=EMAIL_ADDRESS_STATUS['ACTIVE'])
            result = 'Loaded email address status Active: Id:{0}'.format(EMAIL_ADDRESS_STATUS['ACTIVE'])
            logger.debug(result)
        except ObjectDoesNotExist:
            result = 'Failed Loading email address status Active: Id:{0}'.format(EMAIL_ADDRESS_STATUS['ACTIVE'])
            logger.critical(result)
            return ReturnResponse.Response(1, __name__, 'failed', result).return_json()

        try:
            email_address = EmailAddress.objects.get(User_Id=new_user, Status_Id=email_address_status)
            result = 'Loaded email address :{0}'.format(email_address.Email)
            logger.debug(result)
        except ObjectDoesNotExist:
            result = 'Failed Loading email address:'
            logger.critical(result)
            return ReturnResponse.Response(1, __name__, 'failed', result).return_json()
        self.toEmail = email_address.Email

        try:
            name_type = NameType.objects.get(pk=NAME_TYPE['FIRST'])
            result = 'Loaded name type:{0}'.format(name_type.Type)
            logger.debug(result)
        except ObjectDoesNotExist:
            result = 'Failed Loading Name Type First:'
            logger.critical(result)
            return ReturnResponse.Response(1, __name__, 'failed', result).return_json()

        try:
            name = Name.objects.get(User_Id=new_user, Type_Id=name_type)
            result = 'Loaded name:{0}'.format(name.Name)
            logger.debug(result)
        except ObjectDoesNotExist:
            result = 'Failed Loading Name:'
            logger.critical(result)
            return ReturnResponse.Response(1, __name__, 'failed', result).return_json()

        self.subject = self.subject.replace('NAME', name.Name)
        self.replace_string_in_template('PASSWORD', str(new_password))
        self.replace_string_in_template('USERNAME', email_address.Email)
        self.replace_string_in_template('FIRST_NAME', name.Name)

        if self.send() == 1:
            notification.Status_Id = NotificationStatus.objects.get(pk=NOTIFICATION_STATUS['SENT'])
            result = 'Welcome Email Sent to:{0}'.format(email_address.Email)
            logger.debug(result)
        else:
            notification.Status_Id = NotificationStatus.objects.get(pk=NOTIFICATION_STATUS['FAILED'])
            result = 'Failed Welcome Email Sent to:{0}'.format(email_address.Email)
            logger.info(result)
        notification.save()
        return ReturnResponse.Response(0, __name__, 'success', result).return_json()
Ejemplo n.º 9
0
    def get(self, request, *args, **kwargs):

        authorization_code = kwargs.get('Authorization_Code', 0)

        try:
            self.get_queryset().filter(
                Authorization_Code=authorization_code).count()
            verify_register = Register.objects.get(
                Authorization_Code=authorization_code)
        except ObjectDoesNotExist:
            register_status = RegisterStatus.objects.get(
                pk=REGISTER_STATUS['INVALID'])
            result = 'Invalid Register Code:{0} :{1}'.format(
                register_status.Status, authorization_code)
            logger.error(result)
            return Response(ReturnResponse.Response(1, __name__, 'failed',
                                                    result).return_json(),
                            status=status.HTTP_200_OK)

        if verify_register.Status_Id.pk == REGISTER_STATUS['EXPIRED']:
            result = 'Expired Register Code:{0} :{1}'.format(
                verify_register.Status, authorization_code)
            logger.error(result)
            return Response(ReturnResponse.Response(1, __name__, 'failed',
                                                    result).return_json(),
                            status=status.HTTP_200_OK)

        if verify_register.Status_Id.pk == REGISTER_STATUS['VERIFIED']:
            result = 'Already Verified Register Code:{0} :{1}'.format(
                verify_register.Status, authorization_code)
            logger.error(result)
            return Response(ReturnResponse.Response(1, __name__, 'failed',
                                                    result).return_json(),
                            status=status.HTTP_200_OK)

        if verify_register.Status_Id.pk == REGISTER_STATUS['NEW']:
            result = 'New Register Code:{0} :{1}'.format(
                verify_register.Status, authorization_code)
            logger.error(result)
            return Response(ReturnResponse.Response(1, __name__, 'failed',
                                                    result).return_json(),
                            status=status.HTTP_200_OK)

        if verify_register.Status_Id.pk != REGISTER_STATUS['SENT']:
            result = 'Register Code Already Sent:{0} :{1}'.format(
                verify_register.Status, authorization_code)
            logger.error(result)
            return Response(ReturnResponse.Response(1, __name__, 'failed',
                                                    result).return_json(),
                            status=status.HTTP_200_OK)

        if (pytz.utc.localize(datetime.utcnow()) -
                verify_register.Inserted) > timedelta(1):
            register_status = RegisterStatus.objects.get(
                pk=REGISTER_STATUS['EXPIRED'])
            verify_register.Status_Id = register_status
            verify_register.save()
            result = 'Register Code Expired:{0} :{1}'.format(
                verify_register.Status, authorization_code)
            logger.error(result)
            return Response(ReturnResponse.Response(1, __name__, 'failed',
                                                    result).return_json(),
                            status=status.HTTP_200_OK)

        new_user = CustomUser(Status_Id=UserStatus.objects.get(
            pk=USER_STATUS['ACTIVE']))
        user_util = Util()
        if user_util.find_username(verify_register.First_Name +
                                   verify_register.Last_Name) != 0:
            result = 'Failed creating username: already exists'
            logger.error(result)
            return Response(ReturnResponse.Response(1, __name__, 'failed',
                                                    result).return_json(),
                            status=status.HTTP_200_OK)

        email_util = EmailUtil()
        if email_util.find_email(verify_register.Email) != 0:
            email_status = EmailAddressStatus.objects.get(
                pk=EMAIL_ADDRESS_STATUS['EXISTS'])
            result = 'Email Already exists:{0}: {1}:'.format(
                email_status.Status, verify_register.Email)
            logger.error(result)
            return Response(ReturnResponse.Response(1, __name__, 'failed',
                                                    result).return_json(),
                            status=status.HTTP_200_OK)

        new_user.Email = verify_register.Email
        new_user.Username = user_util.new_username
        pw = self.generate_password()
        new_user.set_password(pw)
        new_user.save()

        email = EmailAddress(
            Email=verify_register.Email,
            User_Id=new_user,
            Status_Id=EmailAddressStatus(pk=EMAIL_ADDRESS_STATUS['ACTIVE']))

        first_name = Name(Name=verify_register.First_Name,
                          Type_Id=NameType.objects.get(pk=NAME_TYPE['FIRST']),
                          User_Id=new_user)

        last_name = Name(Name=verify_register.Last_Name,
                         Type_Id=NameType.objects.get(pk=NAME_TYPE['LAST']),
                         User_Id=new_user)

        email.save()
        first_name.save()
        last_name.save()

        my_mail = MyEmail("Welcome Email")
        my_mail.send_welcome_email(new_user, pw)

        verify_register.Status_Id = RegisterStatus.objects.get(
            Id=REGISTER_STATUS['VERIFIED'])
        verify_register.save()

        result = 'New User Added:{0}:'.format(new_user.pk)
        logger.info(result)
        return Response(ReturnResponse.Response(1, __name__, 'success',
                                                result).return_json(),
                        status=status.HTTP_200_OK)
Ejemplo n.º 10
0
    def post(self, request, *args, **kwargs):
        userLogin = UserLogin()
        try:
            data = JSONParser().parse(request)
            result = 'parsed json:{0}'.format(data)
            logger.debug(result)
        except:
            result = 'Could not Retrieve email and/or password'
            logger.error(result)
            return Response(
                ReturnResponse.Response(1, __name__, 'failed',
                                        result).return_json())

        try:
            email = data['email']
            result = 'parsed email:{0}'.format(email)
            logger.debug(result)
        except:
            result = 'Could not parse email{0}'.format(data)
            logger.error(result)
            return Response(
                ReturnResponse.Response(1, __name__, 'failed',
                                        result).return_json())

        try:
            password = data['password']
            result = 'parsed password:{0}'.format(password)
            logger.debug(result)
        except:
            result = 'Could not Retrieve password'.format(data)
            logger.error(result)
            return Response(
                ReturnResponse.Response(1, __name__, 'failed',
                                        result).return_json())

        try:
            user = CustomUser.objects.get(Email=email)
            result = 'Retrieved user address:{0} and userId:{1}'.format(
                user.Email, user.Id)
            logger.debug(result)
        except ObjectDoesNotExist:
            email_address_status = EmailAddressStatus.objects.get(
                pk=EMAIL_ADDRESS_STATUS['NOTFOUND'])
            result = email_address_status.Status
            logger.error(result)
            return Response(
                ReturnResponse.Response(1, __name__, 'failed',
                                        result).return_json())

        if not user.is_active:
            result = 'Email Address not Active: {0}'.format(user.is_active)
            logger.error(result)
            return Response(
                ReturnResponse.Response(1, __name__, 'failed',
                                        result).return_json())

        if user.check_password(password):
            jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
            jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
            payload = jwt_payload_handler(user)
            result = jwt_encode_handler(payload)
            logger.debug(result)
            user.is_logged_in = True
            return Response(ReturnResponse.Response(0, __name__, 'success',
                                                    result).return_json(),
                            status=status.HTTP_200_OK)
        else:
            password_status = PasswordStatus.objects.get(
                pk=PASSWORD_STATUS['FAILED'])
            result = 'Wrong Password:'******'failed',
                                    result).return_json())
Ejemplo n.º 11
0
 def get(self, request, *args, **kwargs):
     result = "logged out"
     return Response(ReturnResponse.Response(0, __name__, 'success',
                                             result).return_json(),
                     status=status.HTTP_200_OK)
Ejemplo n.º 12
0
    def post(self, request, *args, **kwargs):
        try:
            data = JSONParser().parse(request)
            result = 'parsed Email:{0} from:{1}'.format(data['email'], data)
            logger.error(result)
        except:
            result = 'Failed Parsing Email from:{0}'.format(request)
            logger.debug(result)
            return Response(
                ReturnResponse.Response(1, __name__, 'failed',
                                        result).return_json())

        try:
            validate_email(data['email'])
            result = 'Validated Email :{0}'.format(data['email'])
            logger.debug(result)
            email = data['email']
        except:
            result = 'Failed Validating Email :{0}'.format(data['email'])
            logger.error(result)
            return Response(
                ReturnResponse.Response(1, __name__, 'failed',
                                        result).return_json())

        try:
            custom_user = CustomUser.objects.get(Email=email)
            result = 'Retrieved custom user email:{0}'.format(
                custom_user.Email)
            logger.debug(result)
        except ObjectDoesNotExist:
            email_address_status = EmailAddressStatus.objects.get(
                pk=EMAIL_ADDRESS_STATUS['NOTFOUND'])
            result = {'status': 1, 'reason': email_address_status.Status}
            logger.error(result)
            return Response(
                ReturnResponse.Response(1, __name__, 'failed',
                                        result).return_json())

        if not custom_user.is_active:
            result = {'status': 1, 'reason': 'Email Address not Active'}
            return Response(
                ReturnResponse.Response(1, __name__, 'failed',
                                        result).return_json())

        try:
            PasswordReset.objects.get(Status_Id=PasswordResetStatus(
                pk=PASSWORD_RESET_STATUS['ACTIVE']),
                                      User_Id=custom_user.Id)
            result = 'Found password reset in table:{0}'.format(
                PasswordReset.Id)
            logger.debug(result)
        except ObjectDoesNotExist:
            password_reset = PasswordReset()
            password_reset.Authorization_Code = self.get_authorization_code()
            password_reset.User_Id = custom_user
            password_reset.Status_Id = PasswordResetStatus(
                pk=PASSWORD_RESET_STATUS['ACTIVE'])
            password_reset.save()
            result = 'created password_reset in table:{0}'.format(
                PasswordReset.User_Id)
            logger.debug(result)

        my_mail = MyEmail("Forgot Password")
        result = my_mail.send_forgot_password_email(custom_user)
        return Response(
            ReturnResponse.Response(0, __name__, 'success',
                                    result).return_json())
Ejemplo n.º 13
0
    def post(self, request, *args, **kwargs):

        try:
            data = JSONParser().parse(request)
            result = 'Parsed JSON:{0}'.format(data)
            logger.debug(result)
        except:
            result = 'Failed to parse JSON:{0}'.format(request)
            logger.error(result)
            return Response(
                ReturnResponse.Response(1, __name__, 'failed',
                                        result).return_json())

        try:
            validate_email(data['register_data']['Email'])
            result = 'Email Address Valid:{0}'.format(
                data['register_data']['Email'])
            logger.debug(result)
        except validate_email.ValidationError:
            result = 'Email Address not Valid:'.format(data['register_data'])
            logger.error(result)
            return Response(
                ReturnResponse.Response(1, __name__, 'failed',
                                        result).return_json())

        email_util = EmailUtil()
        if email_util.find_email(data['register_data']['Email']) != 0:
            result = 'Email Address Already Exists:'.format(
                data['register_data'])
            logger.error(result)
            return Response(
                ReturnResponse.Response(1, __name__, 'failed',
                                        result).return_json())
        else:
            result = 'Email Address Available:{0}'.format(
                data['register_data']['Email'])
            logger.debug(result)

        serializer = RegisterSerializer(data=data['register_data'])
        if serializer.is_valid():
            new_register_status = RegisterStatus.objects.get(
                pk=REGISTER_STATUS['NEW'])
            serializer.save(User_Agent=request.user_agent.browser,
                            IP_Address=get_ip(request),
                            Authorization_Code=self.get_authorization_code(),
                            Status_Id=new_register_status)
            my_email = MyEmail('Verify Email')
            result = my_email.send_verify_email(serializer.data['Id'])
            logger.debug(result)
            status_serializer = RegisterStatusSerializer(new_register_status)
            result = status_serializer.data
            logger.debug(result)
            return Response(ReturnResponse.Response(1, __name__, 'success',
                                                    result).return_json(),
                            status=status.HTTP_201_CREATED)
        else:
            result = serializer.errors
            logger.error(result)
            return Response(ReturnResponse.Response(1, __name__, 'success',
                                                    result).return_json(),
                            status=status.HTTP_400_BAD_REQUEST)