Esempio n. 1
0
def create_pair(programmer):
    programmers = Programmer.objects.all()
    number_programmers=len(programmers)
    for programmer1 in programmers:
        if not programmer1.id  == number_programmers:
            pair = Pair(pair1 = programmer1, pair2 = programmer, count = 0)
            pair.save()
Esempio n. 2
0
 def test_add_count_adds_1_to_pair_count(self):
     factory = RequestFactory()
     request = factory.get('request')
     programmer1 = Programmer(name='Kory Kraft')
     programmer1.save()
     programmer2 = Programmer(name='Jason Wickstra')
     programmer2.save()
     pair = Pair(pair1=programmer1, pair2=programmer2, count=0)
     pair.save()
     add_count(request=request, pair1_id=1, pair2_id=2)
     pair = Pair.objects.get(pk=1)
     self.assertEqual(pair.count, 1)
Esempio n. 3
0
def createADirectMessagePair(data):
	""" listen on the add new private channel socket, add a new private channel to the server, store the members and emit the new channel"""
	otheruser = data["name"]
	otheruser = User.objects(username=otheruser).first()

	if otheruser:
		# fetching the user again because currentuser is not compatible with Pair
		user = User.objects(username=current_user.username).get()
		try:
			pair = Pair(person1=user, person2=otheruser)
			pair.save()
			emit("show new private pair",{"pairname":pair.pairname,
					"event_sender":current_user.username},
					broadcast=True
			)
			print(pair.pairname)
		except:
			raise
			emit("error", {"message": "pair already exists"})			
	else:
		emit("error", {"message": "pair not created"})
Esempio n. 4
0
def question_ahead(request, offset):
    uname = request.session.get("username", "")
    if uname == "":
        return render_to_response("register.html")
    if request.session.get("source1", ""):
        source1 = request.session.get("source1")
    else:
        source1 = "source1"
    if request.session.get("source2", ""):
        source2 = request.session.get("source2")
    else:
        source2 = "source2"
    mp3list_indb_source1 = Mp3.objects.filter(source=source1)
    mp3list_indb_source2 = Mp3.objects.filter(source=source2)

    if request.COOKIES.has_key("songselected"):
        songselected = request.COOKIES["songselected"]
        songselected = set(eval(songselected))
    else:
        songselected = set()

    if request.COOKIES.has_key("score"):
        d = request.COOKIES["score"]
        d = eval(d)
    else:
        d = {}

    temp_songs = set()
    for select in songselected:
        temp_songs.update(Mp3.objects.filter(filename=select))

    s1_total = set(mp3list_indb_source1)
    mp3list_indb_source1 = list(s1_total.difference(temp_songs))

    s2_total = set(mp3list_indb_source2)
    mp3list_indb_source2 = list(s2_total.difference(temp_songs))

    if request.method == "POST":
        firstsong = request.POST.get("firstsong", "")
        secondsong = request.POST.get("secondsong", "")
        songname1 = request.POST.get("songname1", "")
        songname2 = request.POST.get("songname2", "")
        songselect = request.POST.get("songselect", "")
        try:
            offset = int(offset)
        except ValueError:
            raise Http404()
        if offset > 21:
            offset = 21
        firstsong = int(firstsong)
        secondsong = int(secondsong)
        if songselect == songname1:
            if firstsong <= secondsong:
                d = {songselect: int(secondsong) + 1}
            #if firstsong > secondsong:
            #	#d.update({offset-1:{songname1:int(firstsong),songname2:int(secondsong)}})
            #else:
            #	d.update({offset-1:{songname1:int(secondsong)+1,songname2:int(secondsong)}})
        else:
            if secondsong <= firstsong:
                d = {songselect: int(firstsong) + 1}
            #if secondsong > firstsong:
            #	d.update({offset-1:{songname1:int(firstsong),songname2:int(secondsong)}})
            #else:
            #	d.update({offset-1:{songname1:int(firstsong),songname2:int(firstsong)+1}})
            #d.update({offset-1:{songname1:firstsong,songname2:int(secondsong)+1}})
        [
            Mp3.objects.filter(filename=x).update(weight=int(y))
            for x, y in d.items()
        ]
        f = open(txtpath + "%s.txt" % uname, "a")
        f.write(songname1 + "," + songname2 + "," + songselect + "\n")
        f.close()
        next = offset + 1

        if offset <= 10:
            l = []
            for s1 in mp3list_indb_source1:
                l.append(s1.weight)
            c = Counter(l)
            sameWeight = []
            for count in c:
                if c[count] >= 2:
                    sameWeight.append(count)
            if len(sameWeight) > 0:
                mp3_choice = random.choice(sameWeight)
                #songs  = random.sample([item for item in mp3list_indb_source1 if item.weight == mp3_choice],2)
                song1 = random.choice([
                    item for item in mp3list_indb_source1
                    if item.weight == mp3_choice
                ])
                songselected.add(song1.filename)
                d1 = ",".join(song1.filename.split(".")[0][:-1]).split(",")
                #songleft = [item for item in mp3list_indb_source1 if item.weight == mp3_choice]
                songleft = set([
                    item for item in mp3list_indb_source1
                    if item.weight == mp3_choice
                ]).difference(set([song1]))
                #songleft.pop(songleft.index(song1))
                songleft_alpha = [
                    ",".join(s.filename.split(".")[0][:-1]).split(",")
                    for s in songleft
                ]
                maxDistance = [[toolbox.getDistance(d1, n), n]
                               for n in songleft_alpha]
                temp_list = []
                for temp in maxDistance:
                    temp_list.append(temp[0])
                #song2 = "".join([n for n in maxDistance if n[0] == max(temp_list)][1])+song1.filename.split(".")[0][-1:]
                song2_list = [
                    "".join(n[1]) for n in maxDistance
                    if n[0] == max(temp_list)
                ]
                #song2 may more than one
                if len(song2_list) >= 2:
                    song2_list = song2_list[0]
                else:
                    song2_list = song2_list[0]
                song2_name = song2_list + song1.filename.split(
                    ".")[0][-1:] + ".mp3"
                song2 = [
                    item for item in mp3list_indb_source1
                    if item.filename == song2_name
                ][0]
                songselected.add(song2.filename)
                songs = [song1, song2]
                #uname begin
                user = WebUser.objects.get(userMail=uname)
                val = song1.filename.split(
                    ".")[0][:-1] + "," + song2.filename.split(".")[0][:-1]
                pair = Pair(user=user, value=val)
                pair.save()
                #songselected.add(songs[0].filename)
                #songselected.add(songs[1].filename)
            else:
                songs = random.sample(mp3list_indb_source1, 2)
                songselected.add(songs[0].filename)
                songselected.add(songs[1].filename)
        else:
            l = []
            for s2 in mp3list_indb_source2:
                l.append(s2.weight)
            c = Counter(l)
            sameWeight = []
            for count in c:
                if c[count] >= 2:
                    sameWeight.append(count)
                pass
            if len(sameWeight) > 0:
                mp3_choice = random.choice(sameWeight)
                #songs = random.sample(mp3list_indb_source2.filter(weight=mp3_choice),2)
                #songs  = random.sample([item for item in mp3list_indb_source2 if item.weight == mp3_choice],2)
                song1 = random.choice([
                    item for item in mp3list_indb_source2
                    if item.weight == mp3_choice
                ])
                #songselected.add(song1.filename)
                d1 = ",".join(song1.filename.split(".")[0][:-1]).split(",")
                #songleft = [item for item in mp3list_indb_source1 if item.weight == mp3_choice]
                songleft = set([
                    item for item in mp3list_indb_source2
                    if item.weight == mp3_choice
                ]).difference(set([song1]))
                #songleft.pop(songleft.index(song1))
                songleft_alpha = [
                    ",".join(s.filename.split(".")[0][:-1]).split(",")
                    for s in songleft
                ]
                maxDistance = [[toolbox.getDistance(d1, n), n]
                               for n in songleft_alpha]
                temp_list = []
                for temp in maxDistance:
                    temp_list.append(temp[0])
                #song2 = "".join([n for n in maxDistance if n[0] == max(temp_list)][1])+song1.filename.split(".")[0][-1:]
                #song2_list = ["".join(n[1]) for n in maxDistance if n[0] == max(temp_list)]
                #song2 may more than one
                #if len(song2_list) >= 2 :
                #	song2_list = song2_list[0]
                #else:
                #	song2_list = song2_list[0]
                #song2_name = song2_list+ song1.filename.split(".")[0][-1:]+".mp3"
                #song2 = [item for item in mp3list_indb_source2 if item.filename == song2_name][0]
                #songselected.add(song2.filename)
                #songs = [song1,song2]

                user = WebUser.objects.get(userMail=uname)
                pairs = Pair.objects.filter(user=user)
                for pair in pairs:
                    for distance in temp_list:
                        song2_list = [
                            "".join(n[1]) for n in maxDistance
                            if n[0] == max(temp_list)
                        ]
                        if len(song2_list) >= 2:
                            song2_list = song2_list[0]
                        else:
                            song2_list = song2_list[0]
                        song2_name = song2_list + song1.filename.split(
                            ".")[0][-1:] + ".mp3"
                        song2 = [
                            item for item in mp3list_indb_source2
                            if item.filename == song2_name
                        ][0]
                        #check the select in pair table

                        #song1.filename.split(".")[0][:-1]+","+song2.filename.split(".")[0][:-1]
                        temp_songname1 = song1.filename.split(".")[0][:-1]
                        temp_songname2 = song2.filename.split(".")[0][:-1]
                        print temp_songname1 + "," + temp_songname2 + "=" + pair.value
                        if temp_songname1 + "," + temp_songname2 == pair.value or temp_songname2 + "," + temp_songname1 == pair.value:
                            temp_list.remove(max(temp_list))
                            print "in continue:", temp_list
                            continue
                        else:
                            print "no match found!select song2"
                            songselected.add(song1.filename)
                            songselected.add(song2.filename)
                            songs = [song1, song2]
                else:
                    if "songs" not in locals():
                        songs = random.sample(mp3list_indb_source2, 2)
                        songselected.add(songs[0].filename)
                        songselected.add(songs[1].filename)

                #songselected.add(songs[0].filename)
                #songselected.add(songs[1].filename)
            else:
                songs = random.sample(mp3list_indb_source2, 2)
                songselected.add(songs[0].filename)
                songselected.add(songs[1].filename)
        #mp3list_indb = Mp3.objects.all()
        #mp3list = []
        #for mp3 in mp3list_indb:
        #	mp3list.append(mp3)
        #if len(mp3list) >= 2:
        #	songs = random.sample(mp3list,2)
        #else:
        #	songs = []
        vars = {}
        vars["question_offset"] = offset
        vars["next_question"] = next
        vars["songs"] = songs
        response = render_to_response('question_ahead.html', vars)
        response.set_cookie("offset", offset)
        response.set_cookie("score", str(d))
        response.set_cookie("songselected", list(
            (mp3 for mp3 in songselected)))
        return response

    try:
        offset = int(offset)
    except ValueError:
        raise Http404()
    if offset > 21:
        offset = 21
    next = offset + 1

    if offset <= 10:
        l = []
        for s1 in mp3list_indb_source1:
            l.append(s1.weight)
        c = Counter(l)
        sameWeight = []
        for count in c:
            if c[count] >= 2:
                sameWeight.append(count)
            pass
        if len(sameWeight) > 0:
            mp3_choice = random.choice(sameWeight)
            #songs = random.sample(mp3list_indb_source1.filter(weight=mp3_choice),2)
            #songs  = random.sample([item for item in mp3list_indb_source1 if item.weight == mp3_choice],2)
            song1 = random.choice([
                item for item in mp3list_indb_source1
                if item.weight == mp3_choice
            ])
            songselected.add(song1.filename)
            d1 = ",".join(song1.filename.split(".")[0][:-1]).split(",")
            #songleft = [item for item in mp3list_indb_source1 if item.weight == mp3_choice]
            songleft = set([
                item for item in mp3list_indb_source1
                if item.weight == mp3_choice
            ]).difference(set([song1]))
            #songleft.pop(songleft.index(song1))
            songleft_alpha = [
                ",".join(s.filename.split(".")[0][:-1]).split(",")
                for s in songleft
            ]
            maxDistance = [[toolbox.getDistance(d1, n), n]
                           for n in songleft_alpha]
            temp_list = []
            for temp in maxDistance:
                temp_list.append(temp[0])
            #song2 = "".join([n for n in maxDistance if n[0] == max(temp_list)][1])+song1.filename.split(".")[0][-1:]
            song2_list = [
                "".join(n[1]) for n in maxDistance if n[0] == max(temp_list)
            ]
            #song2 may more than one
            if len(song2_list) >= 2:
                song2_list = song2_list[0]
            else:
                song2_list = song2_list[0]
            song2_name = song2_list + song1.filename.split(
                ".")[0][-1:] + ".mp3"
            song2 = [
                item for item in mp3list_indb_source1
                if item.filename == song2_name
            ][0]
            songselected.add(song2.filename)
            songs = [song1, song2]
            #uname
            user = WebUser.objects.get(userMail=uname)
            val = song1.filename.split(
                ".")[0][:-1] + "," + song2.filename.split(".")[0][:-1]
            pair = Pair(user=user, value=val)
            pair.save()
            #songselected.add(songs[0].filename)
            #songselected.add(songs[1].filename)
        else:
            songs = random.sample(mp3list_indb_source1, 2)
            songselected.add(songs[0].filename)
            songselected.add(songs[1].filename)
            user = WebUser.objects.get(userMail=uname)
            val = songs[0].filename.split(
                ".")[0][:-1] + "," + songs[1].filename.split(".")[0][:-1]
            pair = Pair(user=user, value=val)
            pair.save()
    else:
        l = []
        for s2 in mp3list_indb_source2:
            l.append(s2.weight)
        c = Counter(l)
        sameWeight = []
        for count in c:
            if c[count] >= 2:
                sameWeight.append(count)
            pass
        if len(sameWeight) > 0:
            mp3_choice = random.choice(sameWeight)
            #songs = random.sample(mp3list_indb_source2.filter(weight=mp3_choice),2)
            #songs  = random.sample([item for item in mp3list_indb_source2 if item.weight == mp3_choice],2)
            song1 = random.choice([
                item for item in mp3list_indb_source2
                if item.weight == mp3_choice
            ])

            d1 = ",".join(song1.filename.split(".")[0][:-1]).split(",")
            #songleft = [item for item in mp3list_indb_source1 if item.weight == mp3_choice]
            songleft = set([
                item for item in mp3list_indb_source2
                if item.weight == mp3_choice
            ]).difference(set([song1]))
            #songleft.pop(songleft.index(song1))
            songleft_alpha = [
                ",".join(s.filename.split(".")[0][:-1]).split(",")
                for s in songleft
            ]
            maxDistance = [[toolbox.getDistance(d1, n), n]
                           for n in songleft_alpha]
            temp_list = []
            for temp in maxDistance:
                temp_list.append(temp[0])
            #song2 = "".join([n for n in maxDistance if n[0] == max(temp_list)][1])+song1.filename.split(".")[0][-1:]

            #song2_list = ["".join(n[1]) for n in maxDistance if n[0] == max(temp_list)]
            #song2 may more than one
            #if len(song2_list) >= 2 :
            #	song2_list = song2_list[0]
            #else:
            #	song2_list = song2_list[0]
            #song2_name = song2_list+ song1.filename.split(".")[0][-1:]+".mp3"
            #song2 = [item for item in mp3list_indb_source2 if item.filename == song2_name][0]
            #songselected.add(song2.filename)
            #songs = [song1,song2]
            user = WebUser.objects.get(userMail=uname)
            pairs = Pair.objects.filter(user=user)
            for pair in pairs:
                for distance in temp_list:
                    song2_list = [
                        "".join(n[1]) for n in maxDistance
                        if n[0] == max(temp_list)
                    ]
                    if len(song2_list) >= 2:
                        song2_list = song2_list[0]
                    else:
                        song2_list = song2_list[0]
                    song2_name = song2_list + song1.filename.split(
                        ".")[0][-1:] + ".mp3"
                    song2 = [
                        item for item in mp3list_indb_source2
                        if item.filename == song2_name
                    ][0]
                    #check the select in pair table
                    if song1.filename + "," + song2.filename == pair.value or song2.filename + "," + song1.filename == pair.value:
                        temp_list.remove(max(temp_list))
                        continue
                    else:
                        songselected.add(song1.filename)
                        songselected.add(song2.filename)
                        songs = [song1, song2]
            else:
                if "songs" not in locals():
                    songs = random.sample(mp3list_indb_source2, 2)
                    songselected.add(songs[0].filename)
                    songselected.add(songs[1].filename)
        else:
            songs = random.sample(mp3list_indb_source2, 2)
            songselected.add(songs[0].filename)
            songselected.add(songs[1].filename)
    #mp3list_indb = Mp3.objects.all()
    #mp3list = []
    #for mp3 in mp3list_indb:
    #	mp3list.append(mp3)
    #if len(mp3list) >= 2:
    #	songs = random.sample(mp3list,2)
    #else:
    #	songs = []
    vars = {}
    vars["question_offset"] = offset
    vars["next_question"] = next
    vars["songs"] = songs
    response = render_to_response('question_ahead.html', vars)
    response.set_cookie("offset", offset)
    response.set_cookie("score", str(d))
    response.set_cookie("songselected", list((mp3 for mp3 in songselected)))
    return response