Пример #1
0
    def get(self):
        if self.session.get('is_valid') == True:
            user_personality = app_datastore.get_personality(self.session['student_id'])

            sanguine_strength_count = user_personality.sanguine_strength_count
            sanguine_weakness_count = user_personality.sanguine_weakness_count
            choleric_strength_count = user_personality.choleric_strength_count
            choleric_weakness_count = user_personality.choleric_weakness_count
            melancholic_strength_count = user_personality.melancholy_strength_count
            melancholic_weakness_count = user_personality.melancholy_weakness_count
            phlegmatic_strength_count = user_personality.phlegmatic_strength_count
            phlegmatic_weakness_count = user_personality.phlegmatic_weakness_count
            sanguine_count = sanguine_strength_count + sanguine_weakness_count
            choleric_count = choleric_strength_count + choleric_weakness_count
            melancholic_count = melancholic_strength_count + melancholic_weakness_count
            phlegmatic_count = phlegmatic_strength_count + phlegmatic_weakness_count

            top_strength = user_personality.two_dominant_temperaments_strength[0]
            second_strength = user_personality.two_dominant_temperaments_strength[1]
            top_weakness = user_personality.two_dominant_temperaments_weakness[0]
            second_weakness = user_personality.two_dominant_temperaments_weakness[1]
            top_both = user_personality.two_dominant_temperaments_both[0]
            second_both = user_personality.two_dominant_temperaments_both[1]

            top_extreme = four_temperaments.get_random_extreme(top_strength)
            second_extreme = four_temperaments.get_random_extreme(second_strength)
            top_flaw = four_temperaments.get_random_flaw(top_weakness)
            second_flaw = four_temperaments.get_random_flaw(second_weakness)
            top_get_along = four_temperaments.get_random_how_to_get_along_with(top_weakness)
            second_get_along = four_temperaments.get_random_how_to_get_along_with(second_weakness)
            top_need = four_temperaments.get_random_needs_to_be(top_both)
            second_need = four_temperaments.get_random_needs_to_be(second_both)
            top_improvement = four_temperaments.get_random_improvement(top_both)
            second_improvement = four_temperaments.get_random_improvement(second_both)

            template_values = {
                'student_name': self.session.get('student_name'),
                'student_email': self.session.get('student_email'),
                'sanguine_strength_count': sanguine_strength_count,
                'sanguine_weakness_count': sanguine_weakness_count,
                'choleric_strength_count': choleric_strength_count,
                'choleric_weakness_count': choleric_weakness_count,
                'melancholic_strength_count': melancholic_strength_count,
                'melancholic_weakness_count': melancholic_weakness_count,
                'phlegmatic_strength_count': phlegmatic_strength_count,
                'phlegmatic_weakness_count': phlegmatic_weakness_count,
                'sanguine_count': sanguine_count,
                'choleric_count': choleric_count,
                'melancholic_count': melancholic_count,
                'phlegmatic_count': phlegmatic_count,
                'top_extreme': top_extreme, 
                'second_extreme': second_extreme, 
                'top_flaw': top_flaw, 
                'second_flaw': second_flaw,
                'top_get_along': top_get_along,
                'second_get_along': second_get_along,
                'top_need': top_need,
                'second_need': second_need,
                'top_improvement': top_improvement,
                'second_improvement': second_improvement
            }
            template = jinja_environment.get_template('improvement.html')
            self.response.out.write(template.render(template_values))
        else:
            self.redirect(app_domain)
Пример #2
0
    def get(self):
        urlfetch.set_default_fetch_deadline(10)

        # Retrieve token
        if self.session.get('is_valid') != True:
            self.session['ivle_token'] = self.request.get('token')
            self.session['is_valid'] = json.load(urllib2.urlopen('https://ivle.nus.edu.sg/api/Lapi.svc/Validate?APIKey=' + ivle_api_key + '&Token=' + self.session.get('ivle_token')))['Success']
        
        # If the user is authenticated and token is not empty
        if self.session.get('is_valid') == True and self.session.get('ivle_token') != '':

            # Initialise values
            aspirations_completed = False
            education_completed = False
            experience_completed = False
            personality_completed = False
            aspirations_html = ''
            best_modules_html = ''
            interests_html = ''
            skills_and_knowledge_html = ''
            strengths_at_work_html = ''
            involvements_html = ''
            emotions_html = ''
            strengths_as_a_friend_html = ''
            personality_best_html = ''
            advice_html = ''
            social_networks_html=''

            # If the user's session is new
            if app_datastore.user_exists(self.session.get('student_id')) == False:

                # Retrieve profile information from IVLE and remember for the current browser session
                student_profile_object = json.load(urllib2.urlopen('https://ivle.nus.edu.sg/api/Lapi.svc/Profile_View?APIKey=' + ivle_api_key + '&AuthToken=' + self.session.get('ivle_token')))['Results'][0]
                self.session['student_id'] = student_profile_object['UserID']
                self.session['student_name'] = student_profile_object['Name']
                self.session['student_email'] = student_profile_object['Email']
                self.session['student_matriculation_year'] = student_profile_object['MatriculationYear']
                self.session['student_first_major'] = student_profile_object['FirstMajor']
                self.session['student_second_major'] = student_profile_object['SecondMajor']
                self.session['student_faculty'] = student_profile_object['Faculty']
                self.session['log_identity'] = self.session.get('student_name') + " (" + self.session.get('student_id') + ")" 

                logging.debug(self.session.get('log_identity') + " has logged in")

            # If the user exists in the datastore
            if app_datastore.user_exists(self.session['student_id']):

                # Update the email session variable if there is a different email in the datastore than in IVLE
                if (app_datastore.get_user(self.session['student_id']).email != self.session['student_email']):
                    self.session['student_email'] = app_datastore.get_user(self.session['student_id']).email

                # Describe existing aspirations
                try:
                    aspirations = app_datastore.get_aspirations(self.session['student_id']).aspirations
                    aspirations_completed = app_datastore.get_aspirations(self.session['student_id']).completed
                    number_of_aspirations = len(aspirations)
                    aspiration_id = 1
                    for aspiration in aspirations:
                        if aspiration[0] in "aeiou":
                            aspirations_html += "an "
                        else:
                            aspirations_html += "a "
                        aspirations_html += aspiration
                        if aspiration_id + 1 < number_of_aspirations:
                            aspirations_html += ", "
                        elif aspiration_id + 1 == number_of_aspirations:
                            aspirations_html += " or "
                        aspiration_id += 1
                except Exception:
                    pass

                # Describe existing best modules
                try:
                    best_modules = app_datastore.get_education(self.session['student_id']).best_modules
                    education_completed = app_datastore.get_education(self.session['student_id']).completed
                    number_of_best_modules = len(best_modules)
                    if number_of_best_modules > 3:
                        remaining_best_module_count = number_of_best_modules - 3
                        best_module_id_1 = randint(0, number_of_best_modules-1)
                        best_module_id_2 = best_module_id_1
                        while (best_module_id_2 == best_module_id_1):
                            best_module_id_2 = randint(0, number_of_best_modules-1)
                        best_module_id_3 = best_module_id_1
                        while (best_module_id_3 == best_module_id_1 or best_module_id_3 == best_module_id_2):
                            best_module_id_3 = randint(0, number_of_best_modules-1)
                        best_modules_html = best_modules[best_module_id_1] + ", " + best_modules[best_module_id_2] + ", " + best_modules[best_module_id_3] + " and " + str(remaining_best_module_count) + " other module"
                        if remaining_best_module_count > 1:
                            best_modules_html += "s"
                    else:
                        i = 1
                        for best_module in best_modules:
                            best_modules_html += best_module
                            if i + 1 < number_of_best_modules:
                                best_modules_html += ", "
                            elif i + 1 == number_of_best_modules:
                                best_modules_html += " and "
                            i += 1
                except Exception:
                    pass

                # Describe existing interests
                try:
                    interests = app_datastore.get_experience(self.session['student_id']).interests
                    experience_completed = app_datastore.get_experience(self.session['student_id']).completed
                    if len(interests) > 3:
                        remaining_interest_count = len(interests) - 3
                        interest_id_1 = randint(0, len(interests)-1)
                        interest_id_2 = interest_id_1
                        while (interest_id_2 == interest_id_1):
                            interest_id_2 = randint(0, len(interests)-1)
                        interest_id_3 = interest_id_1
                        while (interest_id_3 == interest_id_1 or interest_id_3 == interest_id_2):
                            interest_id_3 = randint(0, len(interests)-1)
                        interests_html = interests[interest_id_1] + ", " + interests[interest_id_2] + ", " + interests[interest_id_3] + " and " + str(remaining_interest_count) + " other interest"
                        if remaining_interest_count > 1:
                            interests_html += "s"
                    else:
                        i = 1
                        for interest in interests:
                            interests_html += interest
                            if i + 1 < len(interests):
                                interests_html += ", "
                            elif i + 1 == len(interests):
                                interests_html += " and "
                            i += 1
                except Exception:
                    pass


                # Describe existing skills
                try:
                    sk = app_datastore.get_experience(self.session['student_id']).skills_and_knowledge
                    experience_completed = app_datastore.get_experience(self.session['student_id']).completed
                    if len(sk) > 3:
                        remaining_sk_count = len(sk) - 3
                        sk_id_1 = randint(0, len(sk)-1)
                        sk_id_2 = sk_id_1
                        while (sk_id_2 == sk_id_1):
                            sk_id_2 = randint(0, len(sk)-1)
                        sk_id_3 = sk_id_1
                        while (sk_id_3 == sk_id_1 or sk_id_3 == sk_id_2):
                            sk_id_3 = randint(0, len(sk)-1)
                        skills_and_knowledge_html = sk[sk_id_1] + ", " + sk[sk_id_2] + ", " + sk[sk_id_3] + " and " + str(remaining_sk_count) + " other skill"
                        if remaining_sk_count > 1:
                            skills_and_knowledge_html += "s"
                    else:
                        i = 1
                        for skill_or_knowledge in sk:
                            skills_and_knowledge_html += skill_or_knowledge
                            if i + 1 < len(sk):
                                skills_and_knowledge_html += ", "
                            elif i + 1 == len(sk):
                                skills_and_knowledge_html += " and "
                            i += 1
                except Exception:
                    pass

                # Describe and two strengths at work
                try:
                    personality_completed = app_datastore.get_personality(self.session['student_id']).completed
                    trait1 = app_datastore.get_personality(self.session['student_id']).two_dominant_temperaments_both[0]
                    trait2 = app_datastore.get_personality(self.session['student_id']).two_dominant_temperaments_both[1]
                    phrase1 = four_temperaments.get_random_at_work(trait1)
                    phrase2 = four_temperaments.get_random_at_work(trait2)
                    strengths_at_work_html = phrase1 + " and " + phrase2
                except Exception:
                    pass

                # Describe existing involvements
                try:
                    involvements = app_datastore.get_experience(self.session['student_id']).involvements
                    experience_completed = app_datastore.get_experience(self.session['student_id']).completed
                    if len(involvements) > 3:
                        remaining_involvement_count = len(involvements) - 3
                        involvement_id_1 = randint(0, len(involvements)-1)
                        involvement_id_2 = involvement_id_1
                        while (involvement_id_2 == involvement_id_1):
                            involvement_id_2 = randint(0, len(involvements)-1)
                        involvement_id_3 = involvement_id_1
                        while (involvement_id_3 == involvement_id_1 or involvement_id_3 == involvement_id_2):
                            involvement_id_3 = randint(0, len(involvements)-1)
                        involvements_html = involvements[involvement_id_1] + ", " + involvements[involvement_id_2] + ", " + involvements[involvement_id_3] + " and " + str(remaining_involvement_count) + " other involvement"
                        if remaining_involvement_count > 1:
                            involvements_html += "s"
                    else:
                        i = 1
                        for involvement in involvements:
                            involvements_html += involvement
                            if i + 1 < len(involvements):
                                involvements_html += ", "
                            elif i + 1 == len(involvements):
                                involvements_html += " or "
                            i += 1
                except Exception:
                    pass

                # Describe any two emotions
                try:
                    personality_completed = app_datastore.get_personality(self.session['student_id']).completed
                    trait1 = app_datastore.get_personality(self.session['student_id']).two_dominant_temperaments_both[0]
                    trait2 = app_datastore.get_personality(self.session['student_id']).two_dominant_temperaments_both[1]
                    phrase1 = four_temperaments.get_random_emotion(trait1)
                    phrase2 = four_temperaments.get_random_emotion(trait2)
                    emotions_html = phrase1 + " and " + phrase2
                except Exception:
                    pass

                # Describe any two strengths as a friend
                try:
                    personality_completed = app_datastore.get_personality(self.session['student_id']).completed
                    trait1 = app_datastore.get_personality(self.session['student_id']).two_dominant_temperaments_both[0]
                    trait2 = app_datastore.get_personality(self.session['student_id']).two_dominant_temperaments_both[1]
                    phrase1 = four_temperaments.get_random_as_a_friend(trait1)
                    phrase2 = four_temperaments.get_random_as_a_friend(trait2)
                    strengths_as_a_friend_html = phrase1 + " and " + phrase2
                except Exception:
                    pass

                # Describe any best from each dominant trait in terms of personality. There are only two dominant traits out of four.
                try:
                    personality_completed = app_datastore.get_personality(self.session['student_id']).completed
                    trait1 = app_datastore.get_personality(self.session['student_id']).two_dominant_temperaments_both[0]
                    trait2 = app_datastore.get_personality(self.session['student_id']).two_dominant_temperaments_both[1]
                    phrase1 = four_temperaments.get_random_best_in(trait1)
                    phrase2 = four_temperaments.get_random_best_in(trait2)
                    personality_best_html = phrase1 + " and " + phrase2
                except Exception:
                    pass

                # Describe any one existing advice
                try:
                    advice_html = app_datastore.get_random_advice(self.session['student_id'])
                    experience_completed = app_datastore.get_experience(self.session['student_id']).completed
                except Exception:
                    pass

                # List social networks in bullet points
                try:
                    social_networks_obj = app_datastore.get_user(self.session['student_id']).social_networks
                    for link in social_networks_obj:
                        social_networks_html+="<a href=http://" + link + "><li>http://" + link + "</li></a>"
                except Exception:
                    pass

            # else if the user is new
            else:

                # Register into the datastore
                app_datastore.insert_user(student_profile_object)
                logging.debug(self.session.get('log_identity') + " registered.")
                
            template_values = {
                # Jumbotron
                'student_name': self.session.get('student_name'),
                'student_email': self.session.get('student_email'),
                'matriculation_year': self.session.get('student_matriculation_year'),
                'first_major': self.session.get('student_first_major'),
                'second_major': self.session.get('student_second_major'),
                'faculty': self.session.get('student_faculty'),
                # Summary
                'aspirations_completed': aspirations_completed,
                'education_completed': education_completed,
                'experience_completed': experience_completed,
                'personality_completed': personality_completed,
                'best_modules': best_modules_html,
                'aspirations': aspirations_html.lower(),
                'interests': interests_html.lower(),
                'skills': skills_and_knowledge_html.lower(),
                'two_emotions_from_two_traits': emotions_html,
                'two_strengths_from_two_traits_at_work': strengths_at_work_html,
                'involvements': involvements_html,
                'two_stengths_from_two_traits_as_a_friend': strengths_as_a_friend_html,
                'two_best_from_two_traits': personality_best_html,
                'advice': advice_html,
                # Sidebar
                'gender': app_datastore.get_user(self.session['student_id']).gender,
                'country': app_datastore.get_user(self.session['student_id']).country,
                'date_of_birth': app_datastore.get_user(self.session['student_id']).date_of_birth,
                'website': app_datastore.get_user(self.session['student_id']).website,
                'social_networks': social_networks_html
            }

            template = jinja_environment.get_template('snapshot.html')
            self.response.out.write(template.render(template_values))
        else:
            self.redirect(app_domain)
Пример #3
0
def get_symmetrical(std_id):
    # populate the list of user to be compare with
    compare_list = app_datastore.get_other_records()
    symmetrical_list = {}
    index = 0
    num_of_matching_asp = 0
    total_num_of_compare_std_asp = 0
    num_of_matching_personality = 0
    # loop through the list to find the common connection
    curr_user_asp_list = app_datastore.get_aspirations(std_id).aspirations
    curr_user_personality_list = app_datastore.get_personality(std_id).words
    curr_user_two_temp = app_datastore.get_personality(std_id).two_dominant_temperaments_both
    # for loop each item for each other user
    for other_user in compare_list:
        if (
            app_datastore.asp_exists(other_user.student_id)
            and app_datastore.personality_exists(other_user.student_id)
            and app_datastore.experience_exists(other_user.student_id)
            and app_datastore.education_exists(other_user.student_id)
        ):
            try:
                other_user_two_temp = app_datastore.get_personality(
                    other_user.student_id
                ).two_dominant_temperaments_both
                if other_user.student_id == std_id:
                    pass
                elif not similar_temperaments(curr_user_two_temp, other_user_two_temp):
                    pass
                else:
                    num_of_matching_asp = 0
                    total_num_of_compare_std_asp = 0
                    num_of_matching_personality = 0
                    try:
                        other_asp = app_datastore.get_aspirations(other_user.student_id).aspirations
                        other_personality = app_datastore.get_personality(other_user.student_id).words
                        # count the aspiration part
                        for asp in curr_user_asp_list:
                            compare = difflib.get_close_matches(asp, other_asp, n=20, cutoff=0.8)
                            num_of_matching_asp += len(compare)
                        total_num_of_compare_std_asp = len(other_asp)
                        # count the personality part
                        for personality in curr_user_personality_list:
                            compare = difflib.get_close_matches(personality, other_personality, n=40, cutoff=1)
                            num_of_matching_personality += len(compare)
                            # cal using the formula then include the user if more than 80
                        formula = (
                            float(
                                (
                                    float(num_of_matching_asp / total_num_of_compare_std_asp)
                                    + float(num_of_matching_personality / 40.0)
                                )
                                / 2.0
                            )
                            * 100.0
                        )
                        result = round(formula, 1)
                        symmetrical_list[other_user.student_id] = result
                    except Exception:
                        pass
            except Exception:
                pass
        # check if the 2 temperament are the same
        else:
            pass

    sorted_dict = sorted(symmetrical_list, key=symmetrical_list.get, reverse=True)
    result_dict = {}
    place_index = 0
    for x in xrange(0, len(sorted_dict)):
        place_index = x + 1
        p_percent = "person_" + str(place_index) + "_percent"
        p_name = "person_" + str(place_index) + "_name"
        p_dob = "person_" + str(place_index) + "_dob"
        p_gender = "person_" + str(place_index) + "_gender"
        p_country = "person_" + str(place_index) + "_country"
        p_major = "person_" + str(place_index) + "_major"
        p_faculty = "person_" + str(place_index) + "_faculty"
        p_skills = "person_" + str(place_index) + "_skills"
        p_interests = "person_" + str(place_index) + "_interests"
        p_involvements = "person_" + str(place_index) + "_involvements"
        p_module = "person_" + str(place_index) + "_best_modules"
        p_advice = "person_" + str(place_index) + "_advice"
        p_aspirations = "person_" + str(place_index) + "_aspirations"
        p_networks = "person_" + str(place_index) + "_networks"
        p_website = "person_" + str(place_index) + "_website"
        p_pic = "person_" + str(place_index) + "_pic"
        p_email = "person_" + str(place_index) + "_email"
        result_dict[p_percent] = int(round(symmetrical_list.get(sorted_dict[index])))
        result_dict[p_name] = app_datastore.get_user(sorted_dict[index]).name
        result_dict[p_dob] = app_datastore.get_user(sorted_dict[index]).date_of_birth
        result_dict[p_gender] = app_datastore.get_user(sorted_dict[index]).gender
        result_dict[p_country] = app_datastore.get_user(sorted_dict[index]).country
        result_dict[p_major] = app_datastore.get_user(sorted_dict[index]).first_major
        if app_datastore.get_user(sorted_dict[index]).second_major != "":
            result_dict[p_major] += " and " + app_datastore.get_user(sorted_dict[index]).second_major
        result_dict[p_faculty] = app_datastore.get_user(sorted_dict[index]).faculty
        result_dict[p_skills] = app_datastore.prepare_list(
            app_datastore.get_experience(sorted_dict[index]).skills_and_knowledge
        )
        result_dict[p_interests] = app_datastore.prepare_list(
            app_datastore.get_experience(sorted_dict[index]).interests
        )
        result_dict[p_involvements] = app_datastore.prepare_list(
            app_datastore.get_experience(sorted_dict[index]).involvements
        )
        result_dict[p_module] = app_datastore.prepare_list(app_datastore.get_education(sorted_dict[index]).best_modules)
        result_dict[p_advice] = app_datastore.prepare_list(app_datastore.get_experience(sorted_dict[index]).advices)
        result_dict[p_aspirations] = app_datastore.prepare_list(
            app_datastore.get_aspirations(sorted_dict[index]).aspirations
        )
        result_dict[p_networks] = app_datastore.prepare_list(app_datastore.get_user(sorted_dict[index]).social_networks)
        result_dict[p_website] = app_datastore.get_user(sorted_dict[index]).website
        result_dict[p_pic] = app_datastore.get_pic_url(sorted_dict[index])
        result_dict[p_email] = app_datastore.get_user(sorted_dict[index]).email
        index += 1

    return result_dict