Last active
December 30, 2019 10:56
-
-
Save DenoGeek/416902b465a47237fd97c3184d3f49a2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MobileAuthSerializer(serializers.Serializer): | |
phone_number=serializers.CharField(required=True) | |
def save(self): | |
#Retrieve the user with this mobile number or create a user instance with that phone number | |
try: | |
user=User.objects.get(mobile=self.validated_data['phone_number']) | |
except Exception as e: | |
user=User.objects.create(mobile=self.validated_data['phone_number'],email=self.validated_data['phone_number']) | |
#Generate the users token and send it out | |
MobileToken.objects.filter(user=user).update(is_used=True) | |
token=MobileToken.objects.create( | |
user=user, | |
verification_code=str(self.random_with_N_digits(7)), | |
phone_number=self.validated_data['phone_number'] | |
) | |
return token | |
#method to generate jobs reference numbers | |
def random_with_N_digits(self,n): | |
range_start = 10**(n-1) | |
range_end = (10**n)-1 | |
return randint(range_start, range_end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment