<filename>loadingscreen.py
'''
Loading Screen
Team:
<NAME>
<NAME>
<NAME>
<NAME>
<NAME>
'''

from Tkinter import *
import Tkinter as tk
import globalvars as gv
from function2 import *
from function3 import *
from function4 import *
from function5 import *
import operator
import copy
from PIL import ImageTk, Image



LABEL_FONT = ("Forte", 13)
TEXT_FONT = ("Forte", 13)


# definition for displaypage frame
class LoadingScreen():
    def __init__(self, dimensions, titleofwindow):
        def destroywindow():
            main.quit()
            main.destroy()
            exit()

        main = tk.Tk()
        main.resizable(False, False)
        main.geometry(dimensions)
        main.title(titleofwindow)
        main.configure(background='lightblue')
        main.protocol("WM_DELETE_WINDOW", lambda: destroywindow())

        # list to store input x and y dimensions of window
        xANDy = dimensions.split("x")

        # getting coordinates for center of screen, then set them using Tk.geometry
        xposition = int(main.winfo_screenwidth() / 2 - int(xANDy[0]) / 2)
        yposition = int(main.winfo_screenheight() / 2 - int(xANDy[1]) / 2)

        # set the location of 'main'
        main.geometry("+%d+%d" % (xposition, yposition))

        ##################################################################################################################
        ##################################################################################################################

        # label definitions
        img = ImageTk.PhotoImage(Image.open("./loading.png"))
        imagelabel = Label(main, image=img, background='lightblue')
        imagelabel.pack(anchor="center")

        loadingtext = Label(main, text="\n\nWe are processing your matches, it will \ntake some time", font=LABEL_FONT, background='lightblue')
        loadingtext.pack(anchor="center")

        progresstext = Label(main, text="\n\nLoading...", font=TEXT_FONT, background='lightblue', fg='darkgreen')
        progresstext.pack(anchor="center")





        # call function to execute functions 1-5
        def call_all_functions(userdict, maindict):

            ##################################################################################################################
            ############################################# Call functions #####################################################
            ##################################################################################################################

            # define list of shortlisted users from each function called (all results are stored as list, and appended into this list)
            lists_from_each_functions = []

            ##################################################################################################################
            ############################################# Function 1
            templist = []
            # for each user, append their 'Name' into a temp list first, which will be appended into lists_from_each_functions
            # this represents function 1 (list all the profiles, their names, gender and age)
            for key, value in maindict.items():
                templist.append(str(maindict[key]['Name']).replace("['", "").replace("']", "").replace("\"", ""))

            lists_from_each_functions.append(templist)

            ##########################################################################################################
            ############################################# Function 2

            # Call function 2 (returns a list with accepted users based on currentuser's accepted_countries)
            list_accepted_profiles_based_on_country = processUser(userdict, maindict)
            # print list_accepted_profiles_based_on_country

            # print "countries (not narrowed down to 3): " + str(list_accepted_profiles_based_on_country)
            # add this list to lists_from_each_functions
            lists_from_each_functions.append(list_accepted_profiles_based_on_country)

            # print lists_from_each_functions

            # printCountries(userdict, list_accepted_profiles_based_on_country)

            # after finish function 2, update the progresstext label
            progresstext.config(text="25% done..")

            ##########################################################################################################
            ############################################# Function 3

            # dictionary that holds filtered profiles based on likes and dislikes
            dict_accepted_profiles_based_on_likesdislikes = processInterest(userdict, maindict)
            # init list that will contain top 3 profiles based on likes and dislikes
            listoftop3_accepted_profiles_based_on_likesdislikes = []

            copyof_dict_accepted_profiles_based_on_likesdislikes = {}
            # make a copy of the dictionary made by function 3 (shortlisted profiles based on likes and dislikes)
            copyof_dict_accepted_profiles_based_on_likesdislikes = copy.deepcopy(
                dict_accepted_profiles_based_on_likesdislikes)

            # repeat 3 times to get top 3 matches based on likes & dislikes
            for i in range(0, 3):
                # if dictionary not empty and if highest value is > 0 (meaning point is more than 0)
                if len(copyof_dict_accepted_profiles_based_on_likesdislikes) != 0 and \
                        max(copyof_dict_accepted_profiles_based_on_likesdislikes.iteritems(),
                            key=operator.itemgetter(1))[1] > 0:
                    # get key (profile) with highest score, if theres a tie, only one of the profiles is returned
                    profile_with_highest_score = \
                    max(copyof_dict_accepted_profiles_based_on_likesdislikes.iteritems(), key=operator.itemgetter(1))[0]

                    # append this profile into listoftop3_accepted_profiles_based_on_likesdislikes
                    listoftop3_accepted_profiles_based_on_likesdislikes.append(str(profile_with_highest_score))

                    # delete profile with highest score from the dictionary
                    del copyof_dict_accepted_profiles_based_on_likesdislikes[str(profile_with_highest_score)]

            # delete the copy of the dicitonary (dont waste space)
            del copyof_dict_accepted_profiles_based_on_likesdislikes

            # print "top3_likesdislikes: " + str(listoftop3_accepted_profiles_based_on_likesdislikes)
            # add this list to lists_from_each_functions
            lists_from_each_functions.append(listoftop3_accepted_profiles_based_on_likesdislikes)
            # print "lists_from_each_functions: " + str(lists_from_each_functions)

            # printLikesDislikes(dict_accepted_profiles_based_on_likesdislikes)

            # after finish function 3, update the progresstext label
            progresstext.config(text="50% done..")

            ##########################################################################################################
            ############################################# Function 4

            dict_accepted_profiles_based_on_books = processBook(userdict, maindict)
            # init list that will contain top 3 profiles based on books
            listoftop3_accepted_profiles_based_on_books = []

            # make a copy of the dictionary made by function 3 (shortlisted profiles based on likes and dislikes)
            copyof_dict_accepted_profiles_based_on_books = copy.deepcopy(dict_accepted_profiles_based_on_books)
            # repeat 3 times to get top 3 matches based on likes & dislikes
            for i in range(0, 3):
                # if dictionary not empty and if highest value is > 0 (meaning point is more than 0)
                if len(copyof_dict_accepted_profiles_based_on_books) != 0 and \
                        max(copyof_dict_accepted_profiles_based_on_books.iteritems(), key=operator.itemgetter(1))[1] > 0:
                    #print copyof_dict_accepted_profiles_based_on_books
                    # get key (profile) with highest score, if theres a tie, only one of the profiles is returned
                    profile_with_highest_score = \
                    max(copyof_dict_accepted_profiles_based_on_books.iteritems(), key=operator.itemgetter(1))[0]

                    # append this profile into listoftop3_accepted_profiles_based_on_likesdislikes
                    listoftop3_accepted_profiles_based_on_books.append(str(profile_with_highest_score))
                    # delete profile with highest score from the dictionary
                    del copyof_dict_accepted_profiles_based_on_books[profile_with_highest_score]

            # delete the copy of the dicitonary (dont waste space)
            del copyof_dict_accepted_profiles_based_on_books

            # add this list to lists_from_each_functions
            lists_from_each_functions.append(listoftop3_accepted_profiles_based_on_books)


            ##########################################################################################################
            ############################################# Function 5

            bestMatched = processMatches(list_accepted_profiles_based_on_country,
                                         dict_accepted_profiles_based_on_likesdislikes,
                                         dict_accepted_profiles_based_on_books, userdict, maindict)

            # print "bestMatched: " + str(bestMatched)
            lists_from_each_functions.append(bestMatched)
            # print "updated list: " + str(lists_from_each_functions)

            # after finish function 5, update the progresstext label
            progresstext.config(text="Done!!")

            ########################################### end of call functions ################################################
            ##################################################################################################################

            # update global variables with list of shortlisted Usernames
            # ^ once function 5 is done executing and global vars are updated
            # (list of shortlisted Usernames from each function)
            gv.setlist_of_shortlisted_users(lists_from_each_functions)

            main.quit()
            main.destroy()

        # end of function definition of call_all_functions()

        ##################################################################################################################

        # get user's dictionary from global variable 'userdictionary'
        # (this was added at 'displayprofilepage' upon clicking 'Find Match!' button
        userdict = gv.userdictionary
        userdict.pop('Username', None)  # remove user's 'Username' field from the his/her own profile dictionary

        # get maindictionary
        maindict = gv.maindictionary
        main.after(50, lambda: call_all_functions(userdict,maindict))

        main.mainloop()

        gv.setpagetodisplay("openFindMatchResultsPage")
<filename>loadingscreen.py
'''
Loading Screen
Team:
<NAME>
<NAME>
<NAME>
<NAME>
<NAME>
'''

from Tkinter import *
import Tkinter as tk
import globalvars as gv
from function2 import *
from function3 import *
from function4 import *
from function5 import *
import operator
import copy
from PIL import ImageTk, Image



LABEL_FONT = ("Forte", 13)
TEXT_FONT = ("Forte", 13)


# definition for displaypage frame
class LoadingScreen():
    def __init__(self, dimensions, titleofwindow):
        def destroywindow():
            main.quit()
            main.destroy()
            exit()

        main = tk.Tk()
        main.resizable(False, False)
        main.geometry(dimensions)
        main.title(titleofwindow)
        main.configure(background='lightblue')
        main.protocol("WM_DELETE_WINDOW", lambda: destroywindow())

        # list to store input x and y dimensions of window
        xANDy = dimensions.split("x")

        # getting coordinates for center of screen, then set them using Tk.geometry
        xposition = int(main.winfo_screenwidth() / 2 - int(xANDy[0]) / 2)
        yposition = int(main.winfo_screenheight() / 2 - int(xANDy[1]) / 2)

        # set the location of 'main'
        main.geometry("+%d+%d" % (xposition, yposition))

        ##################################################################################################################
        ##################################################################################################################

        # label definitions
        img = ImageTk.PhotoImage(Image.open("./loading.png"))
        imagelabel = Label(main, image=img, background='lightblue')
        imagelabel.pack(anchor="center")

        loadingtext = Label(main, text="\n\nWe are processing your matches, it will \ntake some time", font=LABEL_FONT, background='lightblue')
        loadingtext.pack(anchor="center")

        progresstext = Label(main, text="\n\nLoading...", font=TEXT_FONT, background='lightblue', fg='darkgreen')
        progresstext.pack(anchor="center")





        # call function to execute functions 1-5
        def call_all_functions(userdict, maindict):

            ##################################################################################################################
            ############################################# Call functions #####################################################
            ##################################################################################################################

            # define list of shortlisted users from each function called (all results are stored as list, and appended into this list)
            lists_from_each_functions = []

            ##################################################################################################################
            ############################################# Function 1
            templist = []
            # for each user, append their 'Name' into a temp list first, which will be appended into lists_from_each_functions
            # this represents function 1 (list all the profiles, their names, gender and age)
            for key, value in maindict.items():
                templist.append(str(maindict[key]['Name']).replace("['", "").replace("']", "").replace("\"", ""))

            lists_from_each_functions.append(templist)

            ##########################################################################################################
            ############################################# Function 2

            # Call function 2 (returns a list with accepted users based on currentuser's accepted_countries)
            list_accepted_profiles_based_on_country = processUser(userdict, maindict)
            # print list_accepted_profiles_based_on_country

            # print "countries (not narrowed down to 3): " + str(list_accepted_profiles_based_on_country)
            # add this list to lists_from_each_functions
            lists_from_each_functions.append(list_accepted_profiles_based_on_country)

            # print lists_from_each_functions

            # printCountries(userdict, list_accepted_profiles_based_on_country)

            # after finish function 2, update the progresstext label
            progresstext.config(text="25% done..")

            ##########################################################################################################
            ############################################# Function 3

            # dictionary that holds filtered profiles based on likes and dislikes
            dict_accepted_profiles_based_on_likesdislikes = processInterest(userdict, maindict)
            # init list that will contain top 3 profiles based on likes and dislikes
            listoftop3_accepted_profiles_based_on_likesdislikes = []

            copyof_dict_accepted_profiles_based_on_likesdislikes = {}
            # make a copy of the dictionary made by function 3 (shortlisted profiles based on likes and dislikes)
            copyof_dict_accepted_profiles_based_on_likesdislikes = copy.deepcopy(
                dict_accepted_profiles_based_on_likesdislikes)

            # repeat 3 times to get top 3 matches based on likes & dislikes
            for i in range(0, 3):
                # if dictionary not empty and if highest value is > 0 (meaning point is more than 0)
                if len(copyof_dict_accepted_profiles_based_on_likesdislikes) != 0 and \
                        max(copyof_dict_accepted_profiles_based_on_likesdislikes.iteritems(),
                            key=operator.itemgetter(1))[1] > 0:
                    # get key (profile) with highest score, if theres a tie, only one of the profiles is returned
                    profile_with_highest_score = \
                    max(copyof_dict_accepted_profiles_based_on_likesdislikes.iteritems(), key=operator.itemgetter(1))[0]

                    # append this profile into listoftop3_accepted_profiles_based_on_likesdislikes
                    listoftop3_accepted_profiles_based_on_likesdislikes.append(str(profile_with_highest_score))

                    # delete profile with highest score from the dictionary
                    del copyof_dict_accepted_profiles_based_on_likesdislikes[str(profile_with_highest_score)]

            # delete the copy of the dicitonary (dont waste space)
            del copyof_dict_accepted_profiles_based_on_likesdislikes

            # print "top3_likesdislikes: " + str(listoftop3_accepted_profiles_based_on_likesdislikes)
            # add this list to lists_from_each_functions
            lists_from_each_functions.append(listoftop3_accepted_profiles_based_on_likesdislikes)
            # print "lists_from_each_functions: " + str(lists_from_each_functions)

            # printLikesDislikes(dict_accepted_profiles_based_on_likesdislikes)

            # after finish function 3, update the progresstext label
            progresstext.config(text="50% done..")

            ##########################################################################################################
            ############################################# Function 4

            dict_accepted_profiles_based_on_books = processBook(userdict, maindict)
            # init list that will contain top 3 profiles based on books
            listoftop3_accepted_profiles_based_on_books = []

            # make a copy of the dictionary made by function 3 (shortlisted profiles based on likes and dislikes)
            copyof_dict_accepted_profiles_based_on_books = copy.deepcopy(dict_accepted_profiles_based_on_books)
            # repeat 3 times to get top 3 matches based on likes & dislikes
            for i in range(0, 3):
                # if dictionary not empty and if highest value is > 0 (meaning point is more than 0)
                if len(copyof_dict_accepted_profiles_based_on_books) != 0 and \
                        max(copyof_dict_accepted_profiles_based_on_books.iteritems(), key=operator.itemgetter(1))[1] > 0:
                    #print copyof_dict_accepted_profiles_based_on_books
                    # get key (profile) with highest score, if theres a tie, only one of the profiles is returned
                    profile_with_highest_score = \
                    max(copyof_dict_accepted_profiles_based_on_books.iteritems(), key=operator.itemgetter(1))[0]

                    # append this profile into listoftop3_accepted_profiles_based_on_likesdislikes
                    listoftop3_accepted_profiles_based_on_books.append(str(profile_with_highest_score))
                    # delete profile with highest score from the dictionary
                    del copyof_dict_accepted_profiles_based_on_books[profile_with_highest_score]

            # delete the copy of the dicitonary (dont waste space)
            del copyof_dict_accepted_profiles_based_on_books

            # add this list to lists_from_each_functions
            lists_from_each_functions.append(listoftop3_accepted_profiles_based_on_books)


            ##########################################################################################################
            ############################################# Function 5

            bestMatched = processMatches(list_accepted_profiles_based_on_country,
                                         dict_accepted_profiles_based_on_likesdislikes,
                                         dict_accepted_profiles_based_on_books, userdict, maindict)

            # print "bestMatched: " + str(bestMatched)
            lists_from_each_functions.append(bestMatched)
            # print "updated list: " + str(lists_from_each_functions)

            # after finish function 5, update the progresstext label
            progresstext.config(text="Done!!")

            ########################################### end of call functions ################################################
            ##################################################################################################################

            # update global variables with list of shortlisted Usernames
            # ^ once function 5 is done executing and global vars are updated
            # (list of shortlisted Usernames from each function)
            gv.setlist_of_shortlisted_users(lists_from_each_functions)

            main.quit()
            main.destroy()

        # end of function definition of call_all_functions()

        ##################################################################################################################

        # get user's dictionary from global variable 'userdictionary'
        # (this was added at 'displayprofilepage' upon clicking 'Find Match!' button
        userdict = gv.userdictionary
        userdict.pop('Username', None)  # remove user's 'Username' field from the his/her own profile dictionary

        # get maindictionary
        maindict = gv.maindictionary
        main.after(50, lambda: call_all_functions(userdict,maindict))

        main.mainloop()

        gv.setpagetodisplay("openFindMatchResultsPage")
Loading Screen
 Team:
 <NAME>
 <NAME>
 <NAME>
 <NAME>
 <NAME> # definition for displaypage frame # list to store input x and y dimensions of window # getting coordinates for center of screen, then set them using Tk.geometry # set the location of 'main' ################################################################################################################## ################################################################################################################## # label definitions # call function to execute functions 1-5 ################################################################################################################## ############################################# Call functions ##################################################### ################################################################################################################## # define list of shortlisted users from each function called (all results are stored as list, and appended into this list) ################################################################################################################## ############################################# Function 1 # for each user, append their 'Name' into a temp list first, which will be appended into lists_from_each_functions # this represents function 1 (list all the profiles, their names, gender and age) ########################################################################################################## ############################################# Function 2 # Call function 2 (returns a list with accepted users based on currentuser's accepted_countries) # print list_accepted_profiles_based_on_country # print "countries (not narrowed down to 3): " + str(list_accepted_profiles_based_on_country) # add this list to lists_from_each_functions # print lists_from_each_functions # printCountries(userdict, list_accepted_profiles_based_on_country) # after finish function 2, update the progresstext label ########################################################################################################## ############################################# Function 3 # dictionary that holds filtered profiles based on likes and dislikes # init list that will contain top 3 profiles based on likes and dislikes # make a copy of the dictionary made by function 3 (shortlisted profiles based on likes and dislikes) # repeat 3 times to get top 3 matches based on likes & dislikes # if dictionary not empty and if highest value is > 0 (meaning point is more than 0) # get key (profile) with highest score, if theres a tie, only one of the profiles is returned # append this profile into listoftop3_accepted_profiles_based_on_likesdislikes # delete profile with highest score from the dictionary # delete the copy of the dicitonary (dont waste space) # print "top3_likesdislikes: " + str(listoftop3_accepted_profiles_based_on_likesdislikes) # add this list to lists_from_each_functions # print "lists_from_each_functions: " + str(lists_from_each_functions) # printLikesDislikes(dict_accepted_profiles_based_on_likesdislikes) # after finish function 3, update the progresstext label ########################################################################################################## ############################################# Function 4 # init list that will contain top 3 profiles based on books # make a copy of the dictionary made by function 3 (shortlisted profiles based on likes and dislikes) # repeat 3 times to get top 3 matches based on likes & dislikes # if dictionary not empty and if highest value is > 0 (meaning point is more than 0) #print copyof_dict_accepted_profiles_based_on_books # get key (profile) with highest score, if theres a tie, only one of the profiles is returned # append this profile into listoftop3_accepted_profiles_based_on_likesdislikes # delete profile with highest score from the dictionary # delete the copy of the dicitonary (dont waste space) # add this list to lists_from_each_functions ########################################################################################################## ############################################# Function 5 # print "bestMatched: " + str(bestMatched) # print "updated list: " + str(lists_from_each_functions) # after finish function 5, update the progresstext label ########################################### end of call functions ################################################ ################################################################################################################## # update global variables with list of shortlisted Usernames # ^ once function 5 is done executing and global vars are updated # (list of shortlisted Usernames from each function) # end of function definition of call_all_functions() ################################################################################################################## # get user's dictionary from global variable 'userdictionary' # (this was added at 'displayprofilepage' upon clicking 'Find Match!' button # remove user's 'Username' field from the his/her own profile dictionary # get maindictionary