Exemplo n.º 1
0
    def get(self):
        template_values = {}
        user_data = UserData.current()

        if user_data is not None:
            user_email = user_data.user_email
            nickname = ""
            if facebook_util.is_facebook_user_id(user_email):
                nickname = facebook_util.get_facebook_nickname(user_email)

            application_filled = self.request.get('application_filled')
            make_payment = self.request.get('make_payment')

            if make_payment:
                student_email = self.request.get('student_email')
                is_parent_str = self.request.get('is_parent')

                query = SummerStudent.all()
                query.filter('email = ', student_email)
                student = query.get()

                if student is None:
                    output_str = 'Please <a href="/summer/application">apply</a> first' % student_email
                    self.response.out.write(output_str)
                    return

                if student.processing_fee_paid:
                    self.redirect("/summer/application-status")
                    return

                query = SummerParentData.all()
                query.filter('email = ', student.parent_email)
                parent = query.get()

                if parent is None:
                    logging.error("Unexpected NULL parent for student <%s> with parent <%s>" %
                                   (student_email, student.parent_email))

                if is_parent_str == "True":
                    is_parent = True
                else:
                    is_parent = False

                payee_phone_a = ""
                payee_phone_b = ""
                payee_phone_c = ""
                phone_parts = parent.phone.split("-")
                if phone_parts is not None:
                    payee_phone_a = phone_parts[0]
                    payee_phone_b = phone_parts[1]
                    payee_phone_c = phone_parts[2]

                template_values = {
                    "authenticated" : True,
                    "make_payment" : True,
                    "is_parent" : is_parent,
                    "is_parent_js" : json.dumps(is_parent),
                    "student" : student,
                    "student_js" : json.dumps(student.to_dict()),
                    "payee" : parent,
                    "payee_phone_a" : payee_phone_a,
                    "payee_phone_b" : payee_phone_b,
                    "payee_phone_c" : payee_phone_c,
                    "user_email" : user_email,
                    "nickname" : nickname,
                }

            elif not application_filled:
                template_values = self.authenticated_response()

            else:
                first_name = self.request.get('first_name')
                student_email = self.request.get('student_email')

                query = SummerStudent.all()
                query.filter('email = ', student_email)
                student = query.get()
                if student is None:
                    student = SummerStudent()
                    student.email = student_email
                    student.applier_email = user_email
                    student.processing_fee_paid = False
                    student.tuition_paid = False

                student.first_name = first_name
                student.last_name = self.request.get('last_name')

                student.date_of_birth = self.request.get('date_of_birth')

                if self.request.get('gender') == "Female":
                    student.is_female = True
                else:
                    student.is_female = False

                student.grade = self.request.get('grade')
                student.school = self.request.get('school')
                student.school_zipcode = self.request.get('school_zip')

                student.session_1 = self.request.get('session_1')
                student.session_2 = self.request.get('session_2')
                student.session_3 = self.request.get('session_3')

                session_choices = { "0":[], "1":[], "2":[], "3":[] }
                session_choices[student.session_1].append("Session 1")
                session_choices[student.session_2].append("Session 2")
                session_choices[student.session_3].append("Session 3")

                student.no_choice = session_choices["0"]
                student.first_choice = session_choices["1"]
                student.second_choice = session_choices["2"]
                student.third_choice = session_choices["3"]

                student.answer_why = self.request.get('answer_why')
                student.answer_how = self.request.get('answer_how')

                student.processing_fee = self.request.get('fee')

                student.tuition = 'TBD'

                student.application_year = '2012'
                student.application_status = 'Processing'

                if user_email == student_email:
                    is_parent = False
                    student.self_applied = True
                else:
                    is_parent = True
                    student.self_applied = False

                student.parent_relation = self.request.get('relation')
                student.parent_email = self.request.get('parent_email')

                student.put()

                query = SummerParentData.all()
                query.filter('email = ', student.parent_email)
                parent = query.get()
                if parent is None:
                    parent = SummerParentData()
                    parent.email = student.parent_email

                parent.first_name = self.request.get('parent_first_name')
                parent.last_name = self.request.get('parent_last_name')
                parent.address_1 = self.request.get('parent_address_1')
                parent.address_2 = self.request.get('parent_address_2')
                parent.city = self.request.get('parent_city')
                parent.state = self.request.get('parent_state')
                parent.zipcode = self.request.get('parent_zip')
                parent.country = self.request.get('parent_country')
                parent.phone = self.request.get('parent_phone')
                parent.comments = self.request.get('parent_comments')

                if student.key() not in parent.students:
                    parent.students.append(student.key())

                parent.put()

                if student.processing_fee_paid:
                    self.redirect("/summer/application-status")
                    return

                payee_phone_a = ""
                payee_phone_b = ""
                payee_phone_c = ""
                phone_parts = parent.phone.split("-")
                if phone_parts is not None:
                    payee_phone_a = phone_parts[0]
                    payee_phone_b = phone_parts[1]
                    payee_phone_c = phone_parts[2]

                # Only send email if the user's email is a valid one
                if not facebook_util.is_facebook_user_id(parent.email):
                    mail.send_mail( \
                        sender = FROM_EMAIL, \
                        to = parent.email, \
                        subject = "Khan Academy Discovery Lab Application", \
                        body = """Dear %s,
                
We have received your application for %s %s for the Khan Academy Discovery Lab 2012. Please ensure you have paid the $5.00 processing fee for the application.

We will notify you about the status of the application as soon as possible, no later than March 1st, 2012. You can always check the status of the application any time at http://www.khanacademy.org/summer/application-status.

Thank you!
Khan Academy Discovery Lab""" % (parent.first_name, student.first_name, student.last_name))

                template_values = {
                    "authenticated" : True,
                    "make_payment" : True,
                    "is_parent" : is_parent,
                    "is_parent_js" : json.dumps(is_parent),
                    "student" : student,
                    "student_js" : json.dumps(student.to_dict()),
                    "parent" : parent,
                    "parent_js" : json.dumps(parent.to_dict()),
                    "payee" : parent,
                    "payee_phone_a" : payee_phone_a,
                    "payee_phone_b" : payee_phone_b,
                    "payee_phone_c" : payee_phone_c,
                    "user_email" : user_email,
                    "nickname" : nickname,
                }

        else:
            template_values = {
                "authenticated" : False,
                "applied" : False
            }

        self.add_global_template_values(template_values)
        self.render_jinja2_template('summer/summer.html', template_values)