Exemple #1
0
def new_contact(request):
    '''
    Adds or modifies a contact.
    Makes a User object (and a UserProfile and ContactInfo) for them.
    '''
    
    #We're going to have two lists of forms; one for the three objects, and one for phone numbers.
    contact_forms = []
    phone_forms = []
    blank_phone_form = PhoneNumberForm(prefix="phone_new")
    
    if request.POST: #Are we posting new information?
        
        #First let's figure out if we are dealing with an existing user or adding a new one.
        try:
            referenced_user = User.objects.get(id=request.POST['user_id'])
        except MultiValueDictKeyError:
            referenced_user = False #We didn't get a user passed in via POST.
    
        user_form = UserContactForm(request.POST, prefix="user")
        contact_form = UserContactInfoForm(request.POST, prefix="contact")
        profile_form = UserProfileForm(request.POST, prefix="profile")
        
        #Now we need to traverse the dict to pick out the phone forms.
        #They may come in three types:
        #phone_n, where n is the id of a PhoneNumber object - these are existing PhoneNumber objects
        #phone_new - these are phone number objects added with the "add phone" button
        #phone_get - this is the phone number passed into the form originally (used if you click "new contact" from a PhoneCall Task page.)
        populated_phone_forms = []
        for item, value in request.POST.items():
            #Take note: item will be something like phone_new-number-2
            if item.split('_')[0] == "phone": 
                #Look for phone items, but only branch off once per phone (ie, only on the "number," not the "type")                
                if item.split('_')[1].split('-')[1] == "number":
                    try:
                        entry = item.split('_')[1].split('-')[2] #Which entry is this?  There might be more than one number. 
                        type_string = str(item.split('-')[0]) + "-type-" + entry
                    except IndexError: #This is not a numbered entry.
                        type_string = str(item.split('-')[0]) + "-type"
                    type = request.POST[type_string] 
                    number = value
                    
                    if not(not number and not type): #We only want cases where both number and type are not blank.  If either is filled in, we'll proceed.
                    
                        case_indicator = item.split('_')[1].split('-')[0] #This will be either n, "new", or "get" as per above.
                        
                        if case_indicator == "new" or case_indicator == "get" or 0:
                            try:
                                phone_number_object = PhoneNumber.objects.get(number=number)
                                populated_phone_forms.append(PhoneNumberForm({'number':number, 'type':type}, instance=phone_number_object))
                            except PhoneNumber.DoesNotExist:
                                populated_phone_forms.append(PhoneNumberForm({'number':number, 'type':type}))
                                                    
                            
                        else: #Assume that it's the n case
                            phone_number_object = PhoneNumber.objects.get(id=case_indicator)
                            populated_phone_forms.append(PhoneNumberForm({'number':number, 'type':type}, instance=phone_number_object))
        
        
        #Send the forms to the handler for processing.
        invalid = handle_user_profile_form(user_form, contact_form, profile_form, populated_phone_forms, user = referenced_user) #Returns forms tuple if forms are invalid; False otherwise

        if not invalid: #Here we'll do something special if the handling went as we hoped.
            '''
            SUCCESS!
            '''
            if 'profile-birthday_month' in request.POST:
                profile_form.instance.birth_month = request.POST['profile-birthday_month']
                profile_form.instance.birth_day = request.POST['profile-birthday_day']
            profile_form.instance.save()
            
            #If wasn't working here.  strange.  TODO: Change from try block to if.  :-)
            try: #TODO: Justin and Kieran say: do this with sessions
                role = request.GET['role']
                if role == 'donor':
                    
                    #Not good here - I want direct access to the user object by now.  REFORM! (People like that reform, pappy)
                    encrypted_user_id = daily_crypt(user_form.instance.id) #Get the user id, encrypt it. 
                    
                    return redirect('/accounting/record_donation/?donor=' + encrypted_user_id)
                
            except LookupError: #Probably ought to be some different branching here - they don't ALWAYS need to go to watch calls.
                
                #Oh, and BTW, this is SUCCESS.
                return redirect(contact_form.instance.get_absolute_url()) #Send them to the contact page.
            
        
                    
        else: #Not valid - let's tell them so.
            contact_forms, phone_forms = invalid
            return render(request, 'contact/new_contact.html', locals())
        
        

    else: #No POST - this is a brand new form.
        contact_forms = [UserContactForm(prefix="user"), UserContactInfoForm(prefix="contact"), UserProfileForm(prefix="profile")]
    
        #We want email, first name, and last name to be required for all submissions.
        contact_forms[0].fields['email'].required = True
        contact_forms[0].fields['first_name'].required = True
        contact_forms[0].fields['last_name'].required = True
        
        
        try: #This might be a "new contact from phone number" request.  Let's find out.
            phone_forms.append(PhoneNumberForm(initial = {'number': request.GET['phone_number']}, prefix="phone_get")) #Yes it is! Put the phone number in the field.                  
        except MultiValueDictKeyError: 
            pass #No, it isn't.  Move along.  Nothing to see here.
    

    
    #Either we don't have POST (meaning this is a brand new form) or something is invalid.
    #In either case, let's set the fields to required and give them the template again.
    initial_lookup_form = SimplePartyLookup()
    
    return render(request, 'contact/new_contact.html', locals())
Exemple #2
0
def new_contact(request):
    '''
    Adds or modifies a contact.
    Makes a User object (and a UserProfile and ContactInfo) for them.
    '''

    #We're going to have two lists of forms; one for the three objects, and one for phone numbers.
    contact_forms = []
    phone_forms = []
    blank_phone_form = PhoneNumberForm(prefix="phone_new")

    if request.POST:  #Are we posting new information?

        #First let's figure out if we are dealing with an existing user or adding a new one.
        try:
            referenced_user = User.objects.get(id=request.POST['user_id'])
        except MultiValueDictKeyError:
            referenced_user = False  #We didn't get a user passed in via POST.

        user_form = UserContactForm(request.POST, prefix="user")
        contact_form = UserContactInfoForm(request.POST, prefix="contact")
        profile_form = UserProfileForm(request.POST, prefix="profile")

        #Now we need to traverse the dict to pick out the phone forms.
        #They may come in three types:
        #phone_n, where n is the id of a PhoneNumber object - these are existing PhoneNumber objects
        #phone_new - these are phone number objects added with the "add phone" button
        #phone_get - this is the phone number passed into the form originally (used if you click "new contact" from a PhoneCall Task page.)
        populated_phone_forms = []
        for item, value in request.POST.items():
            #Take note: item will be something like phone_new-number-2
            if item.split('_')[0] == "phone":
                #Look for phone items, but only branch off once per phone (ie, only on the "number," not the "type")
                if item.split('_')[1].split('-')[1] == "number":
                    try:
                        entry = item.split('_')[1].split(
                            '-'
                        )[2]  #Which entry is this?  There might be more than one number.
                        type_string = str(
                            item.split('-')[0]) + "-type-" + entry
                    except IndexError:  #This is not a numbered entry.
                        type_string = str(item.split('-')[0]) + "-type"
                    type = request.POST[type_string]
                    number = value

                    if not (
                            not number and not type
                    ):  #We only want cases where both number and type are not blank.  If either is filled in, we'll proceed.

                        case_indicator = item.split('_')[1].split(
                            '-'
                        )[0]  #This will be either n, "new", or "get" as per above.

                        if case_indicator == "new" or case_indicator == "get" or 0:
                            try:
                                phone_number_object = PhoneNumber.objects.get(
                                    number=number)
                                populated_phone_forms.append(
                                    PhoneNumberForm(
                                        {
                                            'number': number,
                                            'type': type
                                        },
                                        instance=phone_number_object))
                            except PhoneNumber.DoesNotExist:
                                populated_phone_forms.append(
                                    PhoneNumberForm({
                                        'number': number,
                                        'type': type
                                    }))

                        else:  #Assume that it's the n case
                            phone_number_object = PhoneNumber.objects.get(
                                id=case_indicator)
                            populated_phone_forms.append(
                                PhoneNumberForm(
                                    {
                                        'number': number,
                                        'type': type
                                    },
                                    instance=phone_number_object))

        #Send the forms to the handler for processing.
        invalid = handle_user_profile_form(
            user_form,
            contact_form,
            profile_form,
            populated_phone_forms,
            user=referenced_user
        )  #Returns forms tuple if forms are invalid; False otherwise

        if not invalid:  #Here we'll do something special if the handling went as we hoped.
            '''
            SUCCESS!
            '''
            if 'profile-birthday_month' in request.POST:
                profile_form.instance.birth_month = request.POST[
                    'profile-birthday_month']
                profile_form.instance.birth_day = request.POST[
                    'profile-birthday_day']
            profile_form.instance.save()

            #If wasn't working here.  strange.  TODO: Change from try block to if.  :-)
            try:  #TODO: Justin and Kieran say: do this with sessions
                role = request.GET['role']
                if role == 'donor':

                    #Not good here - I want direct access to the user object by now.  REFORM! (People like that reform, pappy)
                    encrypted_user_id = daily_crypt(
                        user_form.instance.id)  #Get the user id, encrypt it.

                    return redirect('/accounting/record_donation/?donor=' +
                                    encrypted_user_id)

            except LookupError:  #Probably ought to be some different branching here - they don't ALWAYS need to go to watch calls.

                #Oh, and BTW, this is SUCCESS.
                return redirect(contact_form.instance.get_absolute_url()
                                )  #Send them to the contact page.

        else:  #Not valid - let's tell them so.
            contact_forms, phone_forms = invalid
            return render(request, 'contact/new_contact.html', locals())

    else:  #No POST - this is a brand new form.
        contact_forms = [
            UserContactForm(prefix="user"),
            UserContactInfoForm(prefix="contact"),
            UserProfileForm(prefix="profile")
        ]

        #We want email, first name, and last name to be required for all submissions.
        contact_forms[0].fields['email'].required = True
        contact_forms[0].fields['first_name'].required = True
        contact_forms[0].fields['last_name'].required = True

        try:  #This might be a "new contact from phone number" request.  Let's find out.
            phone_forms.append(
                PhoneNumberForm(
                    initial={'number': request.GET['phone_number']},
                    prefix="phone_get")
            )  #Yes it is! Put the phone number in the field.
        except MultiValueDictKeyError:
            pass  #No, it isn't.  Move along.  Nothing to see here.

    #Either we don't have POST (meaning this is a brand new form) or something is invalid.
    #In either case, let's set the fields to required and give them the template again.
    initial_lookup_form = SimplePartyLookup()

    return render(request, 'contact/new_contact.html', locals())
Exemple #3
0
 def __init__(self, field=None, *args, **kwargs):
     super(MustBeUniqueField, self).__init__(*args, **kwargs)
     encrypted_field = daily_crypt(field) #Encrypt the list with today's daily salt
     self.widget.attrs['class'] = 'mustBeUniqueField'
     self.widget.attrs['elephant_data'] = str(encrypted_field)
Exemple #4
0
 def __init__(self, field=None, *args, **kwargs):
     super(MustBeUniqueField, self).__init__(*args, **kwargs)
     encrypted_field = daily_crypt(
         field)  #Encrypt the list with today's daily salt
     self.widget.attrs['class'] = 'mustBeUniqueField'
     self.widget.attrs['elephant_data'] = str(encrypted_field)