def import_contact(contact): user = None healer = get_object_or_404(Healer, user=request.user) first_name = contact['first name'] last_name = contact['last name'] email = contact['email'] phone = contact.get('phone', None) # skip if there is no first name if first_name == '': return email = is_valid_email(email) if email: user = User.objects.filter(email__iexact=email) if user.exists(): user = user[0] if not user: user = create_dummy_user(first_name, last_name, email) # if can't find or create user, skip if not user: return is_client = is_my_client(healer.user, user) # if client created by healer if not is_client: friendship = Clients(from_user=healer.user, to_user=user) friendship.save() try: client = Client.objects.get(user=user) except ObjectDoesNotExist: client = Client(user=user) client.approval_rating = 100 client.save() contact_info = client.get_contact_info(healer) if not contact_info: contact_info = ContactInfo.objects.create( healer=healer, client=client, first_name=user.first_name, last_name=user.last_name) contact_info.emails.add(ContactEmail(email=user.email)) if phone: client.phone_numbers.add(ClientPhoneNumber(number=phone)) contact_info.phone_numbers.add(ContactPhone(number=phone))
def create_new_client(form, healer): if form.is_valid(): user = None user_exist = False if form.cleaned_data['email']: try: user = User.objects.filter( email__iexact=form.cleaned_data['email'])[0] user_exist = True except IndexError: pass if user_exist and not user.is_active: error = 'This email is already in use. Please check your email and click the Email Confirmation link.' email_address = get_or_create_email_address(user) EmailConfirmation.objects.send_confirmation(email_address) return error if not user: user = create_dummy_user(form.cleaned_data['first_name'], form.cleaned_data['last_name'], form.cleaned_data['email']) # if can't find or create user, return None if not user: return '' try: client = Client.objects.get(user=user) except Client.DoesNotExist: client = Client(user=user) client.save() contact_info = client.get_contact_info(healer) if not contact_info: contact_info = ContactInfo.objects.create( healer=healer, client=client, first_name=user.first_name, last_name=user.last_name) contact_info.emails.add(ContactEmail(email=user.email)) if form.cleaned_data['phone']: client.phone_numbers.add( ClientPhoneNumber(number=form.cleaned_data['phone'])) contact_info.phone_numbers.add( ContactPhone(number=form.cleaned_data['phone'])) if form.cleaned_data.get('referred_by', False): client.referred_by = form.cleaned_data['referred_by'] client.save() if form.cleaned_data.get('send_intake_form', False): #send intake form to client healer.send_intake_form(client) password = form.cleaned_data['password1'] if password: client.user.set_password(password) client.user.save() EmailAddress.objects.add_email(client.user, form.cleaned_data['email']) after_signup(client.user, account_type='client', email_confirmed=False) return client error_list = '' for error in form.errors: if error == '__all__': error_list += '%s' % str(form.errors[error]) else: error_list += '%s: %s' % (error, str(form.errors[error])) return error_list