def post(self, request, format=None): data = request.data try: player = request.user song = Song.objects.get(pk=data['song']) play = Play(player=player, song=song) play.save() serial = SongSerializer(song) song.plays += 1 song.save() return Response(serial.data, status=status.HTTP_201_CREATED) except Song.DoesNotExist: return Response("Song PK DNE", status=status.HTTP_400_BAD_REQUEST)
def createPlays(self,chromossomes,population): for i in range(len(self.gameUsers)): # Distribui os chromossomos entre os jogadores for j in range(5): a = random.randint(0,len(chromossomes)-1) b = -1 isDifferent = True while isDifferent: b = random.randint(0,len(chromossomes)-1) if a != b: isDifferent = False play = Play(answer = -1, level = self.currentLevel, chromosomeOne = chromossomes[a], chromosomeOneIndex = a, chromosomeTwo = chromossomes[b], chromosomeTwoIndex = b,play_player = self.gameUsers[i], play_experiment = self.Experiment, answer_time = -1, points = -1) play.save()
def gatraPlayer_PostPlay(request): allowed_methods = ['POST', 'OPTIONS'] if request.method == 'OPTIONS': response = HttpResponse('', status=http_REQUEST_OK) response['Allow'] = ', '.join(allowed_methods) return response if request.method != 'POST': return HttpResponse(request.META, status=http_NOT_ALLOWED) if 'HTTP_GATRA_HASH' in request.META.keys(): _hash = request.META['HTTP_GATRA_HASH'] try: h = Hash.objects.get(valid_hash = _hash) except: return HttpResponse(_hash, status=http_UNAUTHORIZED) else: return HttpResponse('', status=http_UNAUTHORIZED) try: jsonData = json.loads(request.body) except: return HttpResponse('Could not load json', status=http_BAD_REQUEST) if ('title' in jsonData.keys() and 'duration' in jsonData.keys() and 'user_name' in jsonData.keys() and 'user_id' in jsonData.keys() and 'country' in jsonData.keys() and 'idp' in jsonData.keys() and 'media_id' in jsonData.keys() and 'media_filename' in jsonData.keys() and 'media_type' in jsonData.keys()): play = Play() play.title = jsonData['title'] play.duration = jsonData['duration'] play.user_agent = get_user_agent(request) ua = parse(play.user_agent) if ua.is_mobile: play.device_type = 'mobile' elif ua.is_tablet: play.device_type = 'tablet' elif ua.is_pc: play.device_type = 'desktop' else: play.device_type = 'unknown' play.ip_source = get_client_ip(request) play.user_name = jsonData['user_name'] play.user_id = jsonData['user_id'] play.country = jsonData['country'] play.idp = jsonData['idp'] play.media_id = jsonData['media_id'] play.media_filename = jsonData['media_filename'] play.media_type = jsonData['media_type'] if 'season' in jsonData.keys(): play.season = jsonData['season'] if 'episode' in jsonData.keys(): play.episode = jsonData['episode'] if 'idp_name' in jsonData.keys(): play.idp_name = jsonData['idp_name'] play.save() location = "http://gatra.zolechamedia.net:6969/play/" + str(play.id) + "/" status = http_POST_OK data = {'location' : location}; response = HttpResponse(json.dumps(data), status=status, content_type='application/json') # response['Location'] = location return response return HttpResponse('Mandatory json value not found', status=http_BAD_REQUEST)
def gatraThirdparty_PostPlay(request): allowed_methods = ['POST', 'OPTIONS'] if request.method == 'OPTIONS': response = HttpResponse('', status=http_REQUEST_OK) response['Allow'] = ', '.join(allowed_methods) return response if request.method != 'POST': return HttpResponse('', status=http_NOT_ALLOWED) try: jsonData = json.loads(request.body) except: return HttpResponse('Could not load json', status=http_BAD_REQUEST) if ('device_type' in jsonData.keys() and 'user_agent' in jsonData.keys() and 'user_id' in jsonData.keys() and 'access' in jsonData.keys() and 'country' in jsonData.keys() and 'idp' in jsonData.keys() and 'media_id' in jsonData.keys()): play = Play() if 'title' in jsonData.keys(): play.title = jsonData['title'] if 'duration' in jsonData.keys(): play.duration = jsonData['duration'] play.device_type = jsonData['device_type'] play.user_agent = jsonData['user_agent'] if 'ip_source' in jsonData.keys(): play.ip_source = jsonData['ip_source'] if 'user_name' in jsonData.keys(): play.user_name = jsonData['user_name'] play.user_id = jsonData['user_id'] play.access = jsonData['access'] play.country = jsonData['country'] play.idp = jsonData['idp'] play.media_id = jsonData['media_id'] if 'media_filename' in jsonData.keys(): play.media_filename = jsonData['media_filename'] if 'media_type' in jsonData.keys(): play.media_type = jsonData['media_type'] if 'season' in jsonData.keys(): play.season = jsonData['season'] if 'episode' in jsonData.keys(): play.episode = jsonData['episode'] if 'idp_name' in jsonData.keys(): play.idp_name = jsonData['idp_name'] play.save() status = http_POST_OK response = HttpResponse('', status=status, content_type='application/json') return response return HttpResponse('Mandatory json value not found', status=http_BAD_REQUEST)
def read_data(csvfile): with open(csvfile, 'rU') as f: next(f) reader = csv.reader(f) week = 0 #this is for the week number of the season week_cutoff = None for row in reader: if all([r == '' for r in row]): break gid = row[0] #we can get the date now date = datetime.datetime.strptime(gid[0:8],'%Y%m%d') #now we want to deal with the week number if week_cutoff != next_tuesday(date): week += 1 week_cutoff = next_tuesday(date) print 'season ' + csvfile[5:9] + ': Week ' + str(week) qtr = int(row[1]) #need to deal with kickoff, should be 60 if both are 0 #also play under review... row3errors = set(['.M', '**', 'ND','.O','.B','.H','.W','.G']) if row[3] in row3errors: #play under review... minute = 0 second = 0 else: minute = int(row[2]) if row[2] else 0 second = int(row[3]) if minute and row[3] else 0 off_team = row[4] def_team = row[5] down_num = int(row[6]) if row[6] else -1 togo = int(row[7]) if row[7] else -1 yardline = int(row[8]) if row[8] else -1 description = row[9] if description and description[0] == '\xa0': description ='' offscore = int(row[10]) if row[10] else 0 defscore = int(row[11]) if row[11] else 0 season = int(csvfile[5:9]) playoffs = True if week > 16 else False #things greater than week 16 are playoffs formation, turnover, yardage, action, success, points, penalty = parse_description(description) key_rows = [0,1,2,3,6,7,8,10,11] key = ''.join([row[ind] for ind in key_rows]) plays = Play.objects.filter(key=key) #now we want to get the info if not plays or all([p.description == description for p in plays]): play = Play(gid=gid, date=date, description=description, offence_team=off_team, defence_team=def_team, offence_score=offscore, defence_score=defscore, week=week, quarter=qtr, minute = minute, second = second, down = down_num, togo = togo, yardline = yardline, season = season, playoffs = playoffs, formation = formation, turnover = turnover, yardage = yardage, action = action, success = success, points = points, penalty = penalty, key = key, ) play.save()