def ajax_get_winner(request): """" Closes out current round, and gets next video to play (i.e. winner of votes) and returns winning song data as JSON """ if request.is_ajax(): # Get arguments channel_hash_code = request.GET.get('code') try: data = {} # Pause voting while we update things c = channel.retrieve(channel_hash_code) c.is_accepting_votes = False c.save() # Get winner winning_video = voting.get_winner(channel_hash_code) data['video_url'] = winning_video.video_url data['artist'] = winning_video.artist_name data['song'] = winning_video.song_name # Delete last round/Setup next round cvr = voting.create_round(channel_hash_code) # Update channel c.is_accepting_votes = True c.last_song_play_date = datetime.now() c.current_round = cvr c.save() data['success'] = True except: data['success'] = False # Render response return HttpResponse(json.dumps(data), mimetype='application/json') else: # Should only be accessed in an ajax way, otherwise this request is # whack and throwing that shit back return HttpResponseRedirect("/")
def play_seed(request): """ Play seed channel (new channel based on a seed artist) """ # Read in seed artist from GET parameter raw_term = request.GET.get('artist') artist_name = urllib.unquote(raw_term).strip() # Artist validations if len(artist_name) == 0: template = "error_message.html" data = {} data['message'] = "Please specify an artist name." return render_to_response(template, data, context_instance=RequestContext(request)) if not echonest.check_if_artist_exists(artist_name): template = "error_message.html" data = {} data['message'] = "Sorry, we could not find an artist called: " + artist_name + ". Please try another artist." return render_to_response(template, data, context_instance=RequestContext(request)) # Construct channel & setup voting round (every time a new video # is played, a new 'round' of voting is started) c_dict = channel.create_channel_and_get_first_video(artist_name) c = c_dict['channel'] v = c_dict['video_info'] cvr = voting.create_round(c.channel_hash) c.current_round = cvr c.is_accepting_votes = True c.save() # Time to render data = dict([('channel_phone_number', os.environ['TWILIO_SMS_NUMBER']), ('channel_hash', c.channel_hash), ('video_url', v['source_url']), ('initial_artist', c_dict['artist']), ('initial_song', c_dict['song'])]) template = 'play.html' return render_to_response(template, data, context_instance=RequestContext(request))