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()
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)
def getChats(): """ returns the messages that belongs to a channel or dm if it is valid """ channel = request.form.get("channel") ispublic = request.form.get("ispublic") if ispublic == "true": # get the channel information if the it exists otherwise fail channel = PublicChannel.objects(name=channel).first() if channel: # set the last channel to the channel just accessed current_user.lastchannel = channel.name current_user.save() messages = channel.getChats() else: return jsonify({"success": False}) else: # fetch a Pair's messages if the pair exists else fail otheruser = current_user.getotherperson(channel) otheruser = User.objects(username=otheruser).first() if otheruser: user = User.objects(username=current_user.username).get() pair = Pair.getAPair(user, otheruser) if pair: messages = pair.getChats() current_user.lastchannel = pair.pairname current_user.save() else: return jsonify({"success": False}) # send the messages return jsonify({"success": True, "messages":messages})
def create_tournament(): name = request.values['tournament-name'] tournament = Tournament(organizer=current_user, name=name) pair = Pair(tournament=tournament) for i in range(17): key = 'captain{}'.format(i) if key in request.values: if request.values[key] != '': id = request.values[key] cap = Captain.query.get(int(id)) cap.team.total_score = 0 for member in cap.team.members: member.score = 0 member.assessed = False db.session.add(member) cap.tournament = tournament if len(pair.captains) < 2: pair.captains.append(cap) else: pair = Pair(tournament=tournament) pair.captains.append(cap) db.session.add(cap) db.session.add(pair) db.session.commit() for i in range(6): key = 'judge{}'.format(i) if key in request.values: if request.values[key] != '': id = request.values[key] judge = Judge.query.get(int(id)) tournament.judges.append(judge) db.session.add(tournament) db.session.commit() number_of_teams = request.values['number-of-teams'] tournament.number_of_teams = number_of_teams db.session.add(tournament) db.session.commit() flash('Tournament has been created successfully!') return redirect(url_for('index'))
def find_pair(cards_by_rank): result = find_x_of_a_kind(cards_by_rank, 2) if not result: return None (twosome, kickers) = result return Pair(twosome + kickers, twosome[0].rank, *kickers)
def create_pairs(g, r): givers = g[:] recievers = r[:] pairs = [] for giver in givers: try: reciever = choose_reciever(giver, recievers) recievers.remove(reciever) pairs.append(Pair(giver, reciever)) except: return create_pairs(g, r) return pairs
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"})
def _make_pairs(pipe={}): weighted_set, group_id = pipe["weighted_set"], pipe["group_id"] with app.app_context(): group = Group.query.filter_by(id=group_id).first() final_pairs = [] pairs = [user for user, _ in sorted(weighted_set, key=lambda k: k[1])] pair_timestamp = datetime.now() for index, giver in enumerate(pairs): receiver = pairs[(index + 1) % len(pairs)] new_pair = Pair( group=group, receiver=receiver, giver=giver, timestamp=pair_timestamp, channel_id=str(uuid4()), ) final_pairs.append(giver.email) new_pair.save_to_db(db) return {**pipe, "final_pairs": final_pairs}
def advance_team(captain_id): captain = Captain.query.filter_by(id=captain_id).first() tournament = Tournament.query.filter_by(id=captain.tournament_id).first() current_pair = captain.pairs[-1] current_round = current_pair.round if current_round > 1 and len(current_pair.captains) < 2: return jsonify({'status': "error"}) put = False for pair in tournament.pairs: if pair.round == current_round + 1: if len(pair.captains) < 2: put = True captain.team.total_score = 0 for member in captain.team.members: member.score = 0 member.assessed = False db.session.add(member) db.session.commit() db.session.add(captain) db.session.commit() pair.captains.append(captain) pair.update_scores() db.session.add(pair) db.session.commit() if not put: new_pair = Pair(tournament=tournament, round=current_round + 1) new_pair.captains.append(captain) db.session.add(new_pair) db.session.commit() return jsonify({})
def addMessage(data): """ adds a message that was just typed by a user to the channel its on and emits it to be shown only to users currently on that channel """ sender = data["sender"] message = data["message"] channel = data["channel"] typeOfchannel = data["type"] if typeOfchannel == "private": otheruser = current_user.getotherperson(channel) otheruser = User.objects(username=otheruser).first() if otheruser: user = User.objects(username=current_user.username).get() pair = Pair.getAPair(otheruser, user) pair.addChat(sender=sender, message=message) emit("show message", pair.getlastchat(), room=pair.pairname) else: channel = PublicChannel.objects(name=channel).first() if channel: # add chat to channel channel.addChat(sender=sender, message=message) emit("show message", channel.getlastchat(), room=channel.name)
def test_that_pairing_adds_one_to_count(self): pair = Pair(pair1 = self.person1, pair2 = self.person2, count = 0) pair.add_count_to_pair() self.assertEqual(1, pair.count)
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