Exemplo n.º 1
0
 def match_user_from_phone(phone):
     phone_data = validate_get_phone(phone)
     user_profile = UserProfile.get_user_from_phone_number(
         phone_number=phone_data['phone_number'],
         phone_code=phone_data['phone_code'])
     if not user_profile:
         Phone.create(phone)
     return user_profile
Exemplo n.º 2
0
    def create(cls,
               first_name,
               last_name,
               username=None,
               email=None,
               phone=None,
               email_otp=None,
               phone_otp=None):
        email_verified = False
        phone_verified = False
        phone_data = None
        if not email and not phone:
            raise_error('ERR-AUTH-DETAIL-MISSING')
        if email:
            if not validate_email(email):
                raise_error('ERR-GNRL-INVALID-EMAIL')
            if email_otp and (Email.get_email(email=email).otp != email_otp):
                raise_error('ERR-AUTH-INVALID-OTP')
            else:
                email_verified = True
            if UserProfile.match_user_from_email(email=email):
                raise_error('ERR-USER-OTHER-WITH-EMAIL')
        if phone:
            if not email:
                email = UserProfile.get_random_username_email(
                    first_name=first_name)[1]
            phone_data = validate_get_phone(phone)
            if phone_otp and (Phone.get_phone(
                    phone_number=phone_data['phone_number'],
                    phone_code=phone_data['phone_code']).otp != phone_otp):
                raise_error('ERR-AUTH-INVALID-OTP')
            else:
                phone_verified = True
            if UserProfile.match_user_from_phone(
                    phone_number=phone_data['phone_number'],
                    phone_code=phone_data['phone_code']):
                raise_error('ERR-USER-OTHER-WITH-PHONE')
        if username and UserProfile.match_user_from_username(username):
            raise_error('ERR-USER-OTHER-WITH-USERNAME')

        if username is None or not username:
            username = UserProfile.get_random_username(first_name)

        user = User.objects.create(username=username,
                                   email=email,
                                   first_name=first_name,
                                   last_name=last_name)
        user_profile = cls.objects.create(user=user)
        if email_verified:
            user_profile.email_verified = True
        if phone_data:
            user_profile.phone_number = phone_data['phone_number']
            user_profile.phone_code = phone_data['phone_code']
        if phone_verified:
            user_profile.phone_otp = phone_otp
            user_profile.phone_verified = True
        user_profile.save()
        return user_profile
Exemplo n.º 3
0
 def create(phone):
     phone_data = validate_get_phone(phone)
     try:
         obj = Phone.objects.get(number=phone_data['phone_number'],
                                 code=phone_data['phone_code'])
     except ObjectDoesNotExist:
         obj = Phone.objects.create(number=phone_data['phone_number'],
                                    code=phone_data['phone_code'],
                                    otp=random_with_N_digits(4))
     return obj
Exemplo n.º 4
0
    def create(phone, send_otp=False):
        phone_data = validate_get_phone(phone)
        try:
            obj = Phone.objects.get(number=phone_data['phone_number'],
                                    code=phone_data['phone_code'])
            obj.otp = random_with_N_digits(6)
            obj.save()
        except ObjectDoesNotExist:

            obj = Phone.objects.create(number=phone_data['phone_number'],
                                       code=phone_data['phone_code'],
                                       otp=random_with_N_digits(6))
        if send_otp and settings.ENV_SETUP == 'PRODUCTION':
            utils.msg91_phone_otp_verification(phone=obj.number, OTP=obj.otp)
        return obj
Exemplo n.º 5
0
    def update_phone(self, phone, OTP):
        phone_data = validate_get_phone(phone)
        if not OTP:
            raise_error('ERR-AUTH-005')
        if OTP and (Phone.get_phone(phone_number=phone_data['phone_number'],
                                    phone_code=phone_data['phone_code']).otp !=
                    OTP):
            raise_error('ERR-AUTH-INVALID-OTP')

        user_profile = UserProfile.match_user_from_phone(
            phone_number=phone_data['phone_number'],
            phone_code=phone_data['phone_code'])
        if user_profile is not None and user_profile != self:
            raise_error('ERR-USER-OTHER-WITH-PHONE')
        else:
            self.phone_code = phone_data['phone_code']
            self.phone_number = phone_data['phone_number']
            self.phone_otp = OTP
            self.phone_verified = True
            self.save()
Exemplo n.º 6
0
 def get_from_phone(user, phone, fail_silently=True):
     """
     Look for contact by phone
     :param user:
     :param phone:
     :param fail_silently:
     :return:
     """
     phone_data = validate_get_phone(phone)
     if phone:
         try:
             query = UserContact.objects.filter(
                 user=user,
                 phone_number=phone_data['phone_number'],
                 phone_code=phone_data['phone_code'])
             return query.first()
         except ObjectDoesNotExist:
             if fail_silently:
                 return None
             else:
                 raise raise_error('ERR-CONT-002')