def post_message(request, party_id): redis = StrictRedis(connection_pool=redis_connection_pool) message_type = request.POST.get('messageType') user = User.from_request(redis, request) party = Party.get(redis, party_id) m = Message.for_party(redis, party) if message_type == 'chat': text = request.POST.get('text') m.text = text if message_type == 'favorite': track = request.POST.get('trackKey') m.track = track if message_type == 'vote_to_skip': track = request.POST.get('trackKey') m.track = track m.user_id = user.id m.message_type = message_type m.save(redis) party.broadcast_message_added(redis, m) return HttpResponse(status=httplib.CREATED)
def party(request, room_name): if room_name is None: return redirect("/p/rdio") connection = StrictRedis(connection_pool=redis_connection_pool) party = Party.get(connection, room_name) if not party: party = Party() party.id = room_name party.name = room_name party.save(connection) user = User.from_request(connection, request) party.add_user(connection, user) party.broadcast_user_list_state(connection) party.save(connection) context = { "room_name": room_name, "body_class": "party", "room_id": room_name, "initial_player_state_json": json.dumps(party.get_player_state_payload()), "initial_queue_state_json": json.dumps(party.get_queue_state_payload()), "initial_user_list_state_json": json.dumps(party.get_user_list_state_payload()), "initial_messages_state_json": json.dumps(party.get_messages_state_payload(connection)), "initial_theme_state_json": json.dumps(party.get_theme_state_payload()), "current_user": json.dumps(user.to_dict()), } make_room_daemon(room_name) return render(request, "party.html", context)
def get_party_by_id(request, party_id): redis = StrictRedis(connection_pool=redis_connection_pool) party = Party.get(redis, party_id) if party: return JsonResponse({'results': party.to_dict()}) else: return HttpResponseNotFound()
def get_party_queue(request, party_id): redis = StrictRedis(connection_pool=redis_connection_pool) party = Party.get(redis, party_id) if party: results_list = party.queue_to_dict() return JsonResponse({'results': results_list}) else: return HttpResponseNotFound()
def test_queue_remove(self): """ Do we dequeue the right song from the party? Does that song get properly removed? """ q1 = self.party.enqueue_song(self.user, "t123") q2 = self.party.enqueue_song(self.user, "t456") q2.upvote(self.user2) next_entry = self.party.dequeue_next_song() self.assertEquals(next_entry, q2) self.party.save(self.redis) p = Party.get(self.redis, self.party.id) self.assertEquals(p.queue[0].id, q1.id)
def tick(self): # Refresh the party data self.party = Party.get(self.redis, self.party_id) position = (datetime.utcnow() - (self.current_start_time or datetime.utcnow())).seconds if (not self.currently_playing) or (position > self.current_track_duration) or self.party.should_skip(): self.play_next_track() self.party.broadcast_user_list_state(self.redis) return self.should_keep_running()
def vote_to_skip(request, party_id): if request.method == "POST": redis = StrictRedis(connection_pool=redis_connection_pool) user = User.from_request(redis, request) party = Party.get(redis, party_id) party.vote_to_skip(user) party.save(redis) return JsonResponse({'success': True}) else: return HttpResponseNotFound()
def handle(self, room_id, *args, **kwargs): self.party_id = room_id self.redis = StrictRedis(connection_pool=redis_connection_pool) self.party = Party.get(self.redis, room_id) self.currently_playing = None self.current_track_duration = None self.current_start_time = None self.play_track(self.party.playing_track_key) self.run()
def add_to_queue(request, party_id): if request.method == "POST": redis = StrictRedis(connection_pool=redis_connection_pool) user = User.from_request(redis, request) party = Party.get(redis, party_id) party.enqueue_song(user, request.POST.get('trackKey')) party.save(redis) party.broadcast_queue_state(redis) return JsonResponse({'success': True}) else: return HttpResponseNotFound()
def set_theme(request, party_id): if request.method == "POST": redis = StrictRedis(connection_pool=redis_connection_pool) theme = request.POST.get('theme') party = Party.get(redis, party_id) party.theme = theme party.save(redis) party.broadcast_theme_state(redis) return JsonResponse({'success': True}) else: return HttpResponseNotFound()
def downvote(request, party_id): if request.method == "POST": redis = StrictRedis(connection_pool=redis_connection_pool) user = User.from_request(redis, request) party = Party.get(redis, party_id) queue_entry = party.get_queue_entry(request.POST.get('id')) queue_entry.downvote(user) party.save(redis) party.broadcast_queue_state(redis) return JsonResponse({'success': True}) else: return HttpResponseNotFound()
def ping_party(request, party_id): redis = StrictRedis(connection_pool=redis_connection_pool) party = None user = User.from_request(redis, request) if user: party = Party.get(redis, party_id) if user and party: user.visit_party(party_id) user.save(redis) party.add_user(redis, user) return JsonResponse({'success': True}) else: return HttpResponseNotFound()
def test_empty_votes_serialized_correctly(self): """ Do we dequeue the right song from the party? Does that song get properly removed? """ self.party.enqueue_song(self.user, "t123") self.party.save(self.redis) p = Party.get(self.redis, self.party.id) self.assertEquals(p.queue[0].downvotes, set([]))
def test_create_queue_entry(self): """ Can we enqueue and save a song on a queue? """ self.party.enqueue_song(self.user, "t112233") self.party.save(self.redis) p = Party.get(self.redis, self.party.id) self.assertEquals(p.queue[0].track_key, "t112233")