def add_message(chat_id, from_user, message, reference_type=None, reference_id=None): from datetime import datetime chat = Chat.get_obj(id=chat_id) if not chat.active: raise_error('ERR-CHAT-001') msg_number = Message.objects.filter(chat=chat).count() + 1 msg = Message.objects.create(chat=chat, from_user=from_user, text=message, message_number=msg_number) chat.modified = datetime.now() chat.save() if reference_type and reference_id: msg.reference_type = reference_type msg.reference_id = reference_id msg.save() if chat.started is False: chat.started = True chat.save() notify_new_msg_to_user(from_user=from_user, chat=chat, count=msg_number) data = {'message': msg} return data
def match_user_from_username(username): try: query = UserProfile.objects.get(username__iexact=username) return query except User.MultipleObjectsReturned: raise_error('ERR-DJNG-003') except ObjectDoesNotExist: return None
def match_user_from_email(email): try: query = User.objects.get(email__iexact=email).userprofile return query except User.MultipleObjectsReturned: raise_error('ERR-MULTIPLE-OBJECTS') except ObjectDoesNotExist: return None
def match_user_from_phone(phone_number, phone_code): try: query = UserProfile.objects.get(phone_number=phone_number, phone_code=phone_code) return query except User.MultipleObjectsReturned: raise_error('ERR-MULTIPLE-OBJECTS') except ObjectDoesNotExist: return None
def update_username(cls, user, username): if user is None or username is None: return try: user_obj = User.objects.get(username=username) if user != user_obj: raise_error('ERR-USER-OTHER-WITH-USERNAME') except ObjectDoesNotExist: user.username = username user.save()
def match_user_from_email(email): if not validate_email(email): raise_error('ERR-GNRL-002') try: query = User.objects.get(email__iexact=email).userprofile return query except User.MultipleObjectsReturned: raise_error('ERR-DJNG-003') except ObjectDoesNotExist: return None
def create(email, send_otp=False): if not validate_email(email): raise_error('ERR-GNRL-INVALID-EMAIL') try: obj = Email.objects.get(email=email) except ObjectDoesNotExist: obj = Email.objects.create(email=email, otp=random_with_N_digits(6)) if send_otp and settings.ENV_SETUP == 'PRODUCTION': pass return obj
def phone_input(operation, phone, OTP=None): if operation == 'VERIFY_USER_PHONE': user_profile = UserProfile.match_user_from_phone(phone) if not user_profile: raise_error('ERR-USER-001') if not OTP: raise_error('') if not user_profile.phone_otp: raise_error('ERR-USER-006') if not (user_profile.phone_otp == OTP): raise_error('ERR-AUTH-005') user_profile.phone_verified = True user_profile.save() verified = True data = {'phone_verified': verified} return data elif operation == 'VERIFY_USER_REGISTRATION': user_profile = UserProfile.match_user_from_phone(phone) if user_profile: data = {'user_registered': True} else: data = {'user_registered': False} elif operation == 'VERIFY_USER_REGISTRATION_SEND_OTP': user_profile = UserProfile.match_user_from_phone(phone) if user_profile: user_profile.set_phone_otp() # msg91_phone_otp_verification(phone=phone, OTP=user_profile.phone_otp) else: phone_obj = Phone.create(phone) # msg91_phone_otp_verification(phone=phone, OTP=phone_obj.otp) if user_profile: data = {'user_registered': True} else: data = {'user_registered': False} elif operation == 'SEND_PHONE_VERIFICATION_OTP_ALLOW_ANY': Phone.create(phone=phone, send_otp=True) return {'message': 'OTP Sent'} elif operation == 'SEND_PHONE_VERIFICATION_OTP': user_profile = UserProfile.match_user_from_phone(phone) if user_profile: user_profile.set_phone_otp() OTP = user_profile.phone_otp # data = msg91_phone_otp_verification(phone=phone, OTP=OTP) else: raise_error('ERR-USER-001') return data
def verify_phone(self, OTP): self.set_phone_otp() if self.phone_otp == OTP: self.phone_verified = True self.save() verified = True else: raise_error('ERR0011') verified = False data = {'phone_verified': verified} return data
def create(user1, user2): if not user1 or not user2: raise_error('ERR-CHAT-003') elif user1.username == user2.username: raise_error('ERR-CHAT-002') try: chat = Chat.objects.get( Q(Q(user1=user1) & Q(user2=user2)) | Q(Q(user1=user2) & Q(user2=user1))) except ObjectDoesNotExist: chat = Chat.objects.create(user1=user1, user2=user2) chat.create_firebase_id() return chat
def create_from_email(cls, email, first_name, last_name, phone=None, username=None, otp=None): if not email: raise_error('ERR-GNRL-IVALID-EMAIL') user_profile = cls.create(email=email, first_name=first_name, last_name=last_name, username=username, phone=phone, email_otp=otp) return user_profile
def create_with_phone( cls, phone, first_name, last_name, username=None, otp=None, email=None, ): if not phone: raise_error('ERR-GNRL-INVALID-PHONE') user_profile = cls.create(phone=phone, email=email, first_name=first_name, last_name=last_name, username=username, phone_otp=otp) return user_profile
def create_chat_with_member(user, member_id): if not user or not member_id: raise_error('ERR-CHAT-003') elif user.id == member_id: raise_error('ERR-CHAT-002') if user.id < member_id: user1 = user user2 = User.objects.get(id=member_id) else: user1 = User.objects.get(id=member_id) user2 = user try: chat = Chat.objects.get( Q(Q(user1=user1) & Q(user2=user2)) | Q(Q(user1=user2) & Q(user2=user1))) except ObjectDoesNotExist: chat = Chat.objects.create(user1=user1, user2=user2) chat.create_firebase_id() return chat
def create_with_phone(cls, phone_number, email, first_name, last_name, username=None, phone_otp=None): stored_phone = Phone.create(phone=phone_number) if phone_otp and (stored_phone.otp != phone_otp): raise_error('ERR-AUTH-005') user_profile = cls.create(email=email, first_name=first_name, last_name=last_name, username=username) user_profile.phone_number = stored_phone.number user_profile.phone_code = stored_phone.code user_profile.phone_otp = stored_phone.otp if phone_otp: user_profile.phone_verified = True user_profile.save() return user_profile
def update_email(cls, user, new_email, otp, password): if user is None or new_email is None or otp is None or password is None: return if not validate_email(email=new_email): raise_error('ERR-GNRL-IVALID-EMAIL') stored_otp = Email.get_email(email=new_email).otp if stored_otp != otp: raise_error('ERR-AUTH-INVALID-OTP') if not user.check_password(password): raise_error('ERR-AUTH-INVALID-PASSWORD') try: user_obj = User.objects.get(email=new_email) if user != user_obj: raise_error('ERR-USER-OTHER-WITH-EMAIL') else: raise_error('ERR-USER-YOU-WITH-EMAIL') except ObjectDoesNotExist: user.email = new_email user.save()
def get_from_email(user, email, fail_silently=True): """ Look for contact by email :param user: :param email: :param fail_silently: :return: """ if email: try: query_ = UserContact.objects.filter(user=user, email__iexact=email) return query_.first() except ObjectDoesNotExist: if fail_silently: return None else: raise raise_error('ERR-CONT-001')
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')
def create(cls, email, first_name, last_name, username=None, phone=None): if phone and UserProfile.match_user_from_phone(phone): raise_error('ERR-USER-004') if UserProfile.match_user_from_email(email=email): raise_error('ERR-USER-005') if username and UserProfile.match_user_from_username(username): raise_error('ERR-USER-008') if username is None: 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) firebase.setup_user_on_firebase(user) return user_profile
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()
def get_otp(phone_number, phone_code): try: obj = Phone.objects.get(number=phone_number, code=phone_code) return obj.otp except ObjectDoesNotExist: raise_error('ERR-DJNG-002')
def get_email(email): try: obj = Email.objects.get(email=email) except ObjectDoesNotExist: raise_error('ERR-DJNG-002') return obj
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
def get_from_user(cls, user): try: obj = cls.objects.get(user=user) except ObjectDoesNotExist: raise_error(code='ERR-USER-001') return obj