Example #1
0
def user_session():
    if authenticate(request.cookies.get('sessionToken')):
        if 'redirect' in authenticate(request.cookies.get('sessionToken')):
            return "create-profile"
        else:
            return "homepage"
    else:
        return "no"
Example #2
0
def home():
    print(os.listdir())
    user = authenticate(request.cookies.get('sessionToken'))
    if "redirect" in user:
        return redirect(user["redirect"])
    user_id = user["uid"]
    user_object = firebase_functions.getUser(user_id)
    matched_object_list = []
    if user_object.get('matched_count') is not None:
        matched_object_list = [
            firebase_functions.getUser(list(x.keys())[0])
            for x in user_object['matched_count']
        ]
    # Get conversations
    involvedConversations = firebase_functions.getInvolvedConversations(
        user_id)
    for conversation in involvedConversations:
        otherUserID = conversation["chat_id"].replace(user_id,
                                                      "").replace("_", "")
        otherUser = firebase_functions.getUser(otherUserID)
        conversation["otherUser"] = otherUser

    return render_template('home.html',
                           user=user_object,
                           matched_object_list=matched_object_list,
                           involvedConversations=involvedConversations,
                           showAccountStatus=True)
Example #3
0
def about():
    user = authenticate(request.cookies.get('sessionToken'))
    if "redirect" in user:
        return redirect(user["redirect"])
    uid = user["uid"]
    user_object = firebase_functions.getUser(uid)
    return render_template("about.html",
                           showAccountStatus=True,
                           user=user_object)
Example #4
0
def chat(chatID):
    #TESTING: Isaac's account and my account can chat at http://127.0.0.1:5000/chat/di1Lsn3iCla2Qhzk2nByBKmfUeD3_3IjzLCVthGTrlbwkk4woYHfpZB43
    #need a way to kick the user out if their userID is not in the chatID?
    #remember to always iniatialize one message in chatID['messages'] array for the chat to work
    user = authenticate(request.cookies.get('sessionToken'))
    if "redirect" in user:
        return redirect(user["redirect"])
    uid = user['uid']
    print('uid', uid)
    print('_' in chatID and uid in chatID.split('_'), request.method)
    if '_' in chatID and uid in chatID.split('_'):
        if request.method == "POST":
            other_user_id = chatID.split('_')[0] if chatID.split(
                '_')[1] == uid else chatID.split('_')[1]
            other_user = firebase_functions.getUser(other_user_id)
            if other_user['notification_settings'].get('phone') is not None:
                send_message(other_user['notification_settings']['phone'])

            # msg = Message("Hello",
            #       sender="*****@*****.**",
            #       recipients=["*****@*****.**"])

            # mail.send(msg)

            # firebase_functions.sendChat(chatID, user['user_id'], msg)

        chatID = str(chatID)
        other_ID = chatID.replace("_", "").replace(user['user_id'], "")
        userInfo = firebase_functions.getUser(user['user_id'])
        other_doc = firebase_functions.getUser(other_ID)
        # question= "They do not have any question yet."
        #if other_doc['guide_qns] == []: then do the following things:
        question = "Here is something you can ask to kickstart the conversation: "
        question += "\"" + random.choice(other_doc['guide_qns']) + "\""
        messages = firebase_functions.getChatConversation(chatID)['messages']
        messages_array = []
        for message in messages:
            time = message['time_in_string']
            username = message['sender_name']
            complete_msg = time + " " + username + ": " + message['text']
            messages_array.append(complete_msg)
        return render_template('chat.html',
                               messages_array=messages_array,
                               chatID=chatID,
                               uid=user["uid"],
                               user=userInfo,
                               otherUser=other_doc,
                               userName=user["name"],
                               question=question,
                               showAccountStatus=True)
    else:
        content = 'Unauthorized to access this chat conversation'
        #TODO add option to show error-message in template
        return render_template("chat_general.html",
                               error_message=content,
                               showAccountStatus=True)
Example #5
0
def match_users():
    user_id = authenticate(request.cookies.get('sessionToken'))['user_id']
    if user_id == "di1Lsn3iCla2Qhzk2nByBKmfUeD3":

        all_users = firebase_functions.getAllUsers()

        # Remove chats that were never used in the previous match and clear matches that were made in the
        # previous match cycle from matched_count

        for user_id, user_details in all_users.items():
            if user_details.get('matched_count') is None or user_details[
                    'matched_count'] == []:
                continue
            else:
                matches = user_details['matched_count']
                for match in matches:
                    partner_id = list(match.keys())[0]
                    chat_id = list(match.values())[0]
                    chat = firebase_functions.getChatConversation(chat_id)
                    if chat is not None:
                        if len(chat['messages']) > 1:
                            if user_details.get(
                                    'active_chat_partners') is None:
                                firebase_functions.editUser(
                                    user_id,
                                    {"active_chat_partners": [partner_id]})
                            else:
                                new_chat_partners = user_details[
                                    'active_chat_partners'].copy()
                                new_chat_partners.append(partner_id)
                                firebase_functions.editUser(
                                    user_id, {
                                        "active_chat_partners":
                                        new_chat_partners
                                    })
                        else:
                            firebase_functions.deleteChatConversation(chat_id)
                firebase_functions.editUser(user_id, {"matched_count": []})

        # Matching algorithm

        matched_dict, unmatched_group = matching_algo(all_users)
        # Add new match to the different users
        matches_and_unmatched_handler(matched_dict, unmatched_group)

        content = {
            'matching done': 'chats that were never initiated are removed'
        }
        return content, status.HTTP_200_OK
    else:
        content = {'please move along': 'nothing to see here'}
        return content, status.HTTP_404_NOT_FOUND
Example #6
0
def edit_profile():
    user = authenticate(request.cookies.get('sessionToken'))
    if "redirect" in user:
        return redirect(user["redirect"])
    uid = user["uid"]
    user_object = firebase_functions.getUser(uid)
    existingUserInfo = firebase_functions.getUser(uid)

    class ExistingUserInfo(object):
        existingUserInfo = firebase_functions.getUser(uid)
        profilePic = ""
        pronouns = existingUserInfo["gender_pronouns"]
        classYear = existingUserInfo["grad_year"]
        funFact = existingUserInfo["fun_fact"]
        wantMatch = existingUserInfo["want_match"]
        guideQuestionOne = existingUserInfo["guide_qns"][0] if len(
            existingUserInfo["guide_qns"]) > 0 else ""
        guideQuestionTwo = existingUserInfo["guide_qns"][1] if len(
            existingUserInfo["guide_qns"]) > 1 else ""
        guideQuestionThree = existingUserInfo["guide_qns"][2] if len(
            existingUserInfo["guide_qns"]) > 2 else ""
        bio = existingUserInfo["bio"]
        sportsQuestion = existingUserInfo["questionnaire_scores"][0]
        readingQuestion = existingUserInfo["questionnaire_scores"][1]
        cookingQuestion = existingUserInfo["questionnaire_scores"][2]
        DCFoodQuestion = existingUserInfo["questionnaire_scores"][3]
        MoviesVBoardGamesQuestion = existingUserInfo["questionnaire_scores"][4]
        phoneNotification = existingUserInfo['notification_settings'][
            'phone'] if existingUserInfo['notification_settings'].get(
                'phone') is not None else ''

    form = forms.EditProfileForm(obj=ExistingUserInfo)
    if form.validate_on_submit():
        if form.profilePic.data is not None and form.profilePic.data != "":
            form.profilePic.data.save(
                os.path.join("tempStorage", form.profilePic.data.filename))
            print(
                firebase_functions.uploadProfilePic(
                    uid,
                    os.path.join("tempStorage",
                                 form.profilePic.data.filename)))
        # Edit User Profile
        guide_qns = []
        for qn in [
                form.guideQuestionOne.data, form.guideQuestionTwo.data,
                form.guideQuestionThree.data
        ]:
            if qn != "":
                guide_qns.append(qn)
        questionnaire_scores = [
            form.sportsQuestion.data, form.readingQuestion.data,
            form.cookingQuestion.data, form.DCFoodQuestion.data,
            form.MoviesVBoardGamesQuestion.data
        ]
        newInfo = {}
        # Add values to update
        newInfo["gender_pronouns"] = form.pronouns.data
        newInfo["grad_year"] = form.classYear.data
        newInfo["want_match"] = form.wantMatch.data
        newInfo["fun_fact"] = form.funFact.data
        newInfo["fun_fact"] = form.funFact.data
        newInfo["guide_qns"] = guide_qns
        newInfo["bio"] = form.bio.data
        newInfo["questionnaire_scores"] = questionnaire_scores
        if form.phoneNotification.data != "":
            newInfo["notification_settings"] = {
                'phone': form.phoneNotification.data
            }
        else:
            newInfo["notification_settings"] = {}
        firebase_functions.editUser(uid, newInfo)
        return redirect("/")
    return render_template("edit_profile.html",
                           form=form,
                           userInfo=existingUserInfo,
                           showAccountStatus=True,
                           user=user_object)
Example #7
0
def create_profile():
    user = authenticate(request.cookies.get('sessionToken'))
    existingUserInfo = firebase_functions.getUser(user['uid'])
    if existingUserInfo is not None and existingUserInfo.get(
            'grad_year'
    ) is not '':  # if user already has a profile, redirect to edit profile. This is important since we are doing free first 3 matches only for new user
        return redirect('/edit-profile')
    if "redirect" in user and user["redirect"] != "/create-profile":
        print(user['redirect'])
        return redirect(user["redirect"])
    form = forms.CreateProfileForm()
    if form.validate_on_submit():
        uid = user["uid"]
        # Upload Profile Pic
        if form.profilePic.data is not None:
            form.profilePic.data.save(
                os.path.join("tempStorage", form.profilePic.data.filename))
            print(
                firebase_functions.uploadProfilePic(
                    uid,
                    os.path.join("tempStorage",
                                 form.profilePic.data.filename)))
        # Edit User Profile
        print(form.data)
        guide_qns = []
        for qn in [
                form.guideQuestionOne.data, form.guideQuestionTwo.data,
                form.guideQuestionThree.data
        ]:
            if qn != "":
                guide_qns.append(qn)
        questionnaire_scores = [
            form.sportsQuestion.data, form.readingQuestion.data,
            form.cookingQuestion.data, form.DCFoodQuestion.data,
            form.MoviesVBoardGamesQuestion.data
        ]
        newInfo = {}
        # Add nonempty values to update
        if form.pronouns.data != "":
            newInfo["gender_pronouns"] = form.pronouns.data
        if form.classYear.data != "":
            newInfo["grad_year"] = form.classYear.data
        if form.funFact.data != "":
            newInfo["fun_fact"] = form.funFact.data
        if form.funFact.data != "":
            newInfo["fun_fact"] = form.funFact.data
        if guide_qns != []:
            newInfo["guide_qns"] = guide_qns
        if form.bio.data != "":
            newInfo["bio"] = form.bio.data
        if form.phoneNotification.data != "":
            newInfo["notification_settings"] = {
                'phone': form.phoneNotification.data
            }
        newInfo["questionnaire_scores"] = questionnaire_scores
        firebase_functions.editUser(uid, newInfo)
        all_users = firebase_functions.getAllUsers()
        matched_dict, unmatched_group = find_match_for_new_user(uid, all_users)
        matches_and_unmatched_handler(matched_dict, unmatched_group)
        return redirect("/")
    return render_template("create_profile.html", form=form)
Example #8
0
def chat_general():
    user = authenticate(request.cookies.get('sessionToken'))
    if "redirect" in user:
        return redirect(user["redirect"])
    return render_template("chat_general.html", showAccountStatus=True)