def post(self): checked_request = self.__check_request(request.form) timer = Timer.find(checked_request['id']) if not timer: abort(404) res = datetime.strptime(checked_request['end_at'], '%Y-%m-%d %H:%M:%S') - timer.start_at [tmp_minutes, seconds] = divmod(res.total_seconds(), 60) [hours, minutes] = divmod(tmp_minutes, 60) result_time = '%02d:%02d:%02d' % (hours, minutes, seconds) with Timer.transaction(): timer.update(end_at=checked_request['end_at'], result_time=result_time) # raking update session = Timer._get_session() sum_label = func.sec_to_time( func.sum(func.time_to_sec(Timer.result_time))).label('sum') ranking_data = (session.query(Timer, sum_label).filter( Timer.end_at != None, Timer.spot_id == timer.spot.id).group_by( Timer.user_id).order_by(sum_label.desc()).all()) n = 0 ranking = [] res = {} for row in ranking_data: n += 1 if row[0].user.id == g.user.id: res['rank'] = n res['user_name'] = row[0].user.name res['spot_name'] = row[0].spot.name res['result_time'] = result_time ranking.append({ 'rank': n, 'user': { 'id': row[0].user.id, 'name': row[0].user.name }, 'sum': str(row.sum) }) [lng, lat] = re.findall('[0-9]*\.[0-9]*', timer.spot.latlng) doc = g.mongo.focus rank = doc.ranking rank.update({'spot_id': timer.spot.id}, { '$set': { 'data': ranking, 'spot': { 'name': timer.spot.name, 'lat': lat, 'lng': lng, } } }, upsert=True, multi=False) # end ranking update return jsonify(status=200, message='ok', request=request.form, response=res)
def post(self): checked_request = self.__check_request(request.form) timer = Timer.find(checked_request['id']) if not timer: abort(404) res = datetime.strptime(checked_request['end_at'],'%Y-%m-%d %H:%M:%S') - timer.start_at [tmp_minutes, seconds] = divmod(res.total_seconds(), 60) [hours, minutes] = divmod(tmp_minutes,60) result_time = '%02d:%02d:%02d' % (hours, minutes, seconds) with Timer.transaction(): timer.update( end_at=checked_request['end_at'], result_time=result_time ) # raking update session = Timer._get_session() sum_label = func.sec_to_time(func.sum(func.time_to_sec(Timer.result_time))).label('sum') ranking_data = (session .query(Timer, sum_label) .filter(Timer.end_at!=None, Timer.spot_id==timer.spot.id) .group_by(Timer.user_id) .order_by(sum_label.desc()) .all()) n = 0 ranking = [] res={} for row in ranking_data: n += 1; if row[0].user.id == g.user.id: res['rank'] = n res['user_name'] = row[0].user.name res['spot_name'] = row[0].spot.name res['result_time'] = result_time ranking.append( { 'rank':n, 'user':{ 'id':row[0].user.id, 'name':row[0].user.name }, 'sum':str(row.sum) } ) [lng,lat] = re.findall('[0-9]*\.[0-9]*', timer.spot.latlng) doc = g.mongo.focus rank = doc.ranking rank.update( {'spot_id': timer.spot.id}, {'$set':{'data':ranking,'spot':{'name':timer.spot.name, 'lat':lat, 'lng':lng, } }}, upsert=True, multi=False) # end ranking update return jsonify(status=200, message='ok', request=request.form, response=res)
def post(self): checked_request = self.__check_request(request.form) if not Spot.find_by(forsquare_id=checked_request['foursquare_id']): checked_request = self.__re_check_request(request.form) spot = Spot(forsquare_id=checked_request['foursquare_id'], name=checked_request['_location'], latlng='POINT(' + checked_request['lng'] + ' ' + checked_request['lat'] + ")") spot.insert() spot = Spot.find_by(forsquare_id=checked_request['foursquare_id']) with Timer.transaction(): timer = Timer(user_id=g.user.id, spot_id=spot.id, start_at=checked_request["start_at"]) timer.insert() return jsonify(status=200, message='ok', request=request.form, response={'timer_id': timer.id})
def post(self): checked_request = self.__check_request(request.form) if not Spot.find_by(forsquare_id=checked_request['foursquare_id']): checked_request = self.__re_check_request(request.form) spot = Spot( forsquare_id=checked_request['foursquare_id'], name=checked_request['_location'], latlng= 'POINT('+ checked_request['lng'] +' '+ checked_request['lat'] + ")" ) spot.insert() spot = Spot.find_by(forsquare_id=checked_request['foursquare_id']) with Timer.transaction(): timer = Timer(user_id=g.user.id, spot_id=spot.id, start_at=checked_request["start_at"] ) timer.insert() return jsonify(status=200, message='ok', request=request.form, response={'timer_id':timer.id} )