def add_phone_number(self, inviter, parsed_phone_number, contact_nickname, send_invite_callable): """ inviter: The user who added the friend parsed_phone_number: A result of the phonenumbers.parse() function contact_nickname: A string that will be the username of the new friend send_invite_callable: A callable that accepts 3 parameters: inviter, phone_number, current_time Returns the PhoneNumber object belonging to the user who was added """ if not isinstance(parsed_phone_number, phonenumbers.phonenumber.PhoneNumber): raise ValueError('phone must be a PhoneNumber object') phone_number_str = phonenumbers.format_number(parsed_phone_number, phonenumbers.PhoneNumberFormat.E164) phone_number, _ = PhoneNumber.objects.get_or_create_phone_number(phone_number_str, contact_nickname, self.current_date) _, member_created = AlbumMember.objects.get_or_create(user=phone_number.user, album=self.album, defaults={ 'added_by_user': inviter, 'datetime_added': self.current_date, 'album_admin': False }) if phone_number.verified: if member_created: members_added_to_album.send(sender=None, member_users=[phone_number.user], by_user=inviter, to_album=self.album) else: if send_invite_callable: send_invite_callable(inviter, phone_number, self.current_date) return phone_number
def add_user_id(self, inviter, user_id): """ Returns True is user_id succesfully added, or False if no such user_id exists """ try: added_user = get_user_model().objects.get(pk=user_id) except get_user_model().DoesNotExist: return False _, created = AlbumMember.objects.get_or_create(user=added_user, album=self.album, defaults={ 'added_by_user': inviter, 'datetime_added': self.current_date, 'album_admin': False }) if created: members_added_to_album.send(sender=None, member_users=[added_user], by_user=inviter, to_album=self.album) return True