def post(self, room_id): room = self.rooms.find_one({'id': room_id}) if not room: self.fail() return data = tornado.escape.json_decode(self.request.body) user_id = data['id'] pitch = float(data['pitch']) if pitch < 1: self.fail() return user = self.users.find_one({'id': user_id}) if not user or user_id not in room['users']: self.fail() return room['users'][user_id]['donated'] += pitch room['raised'] += pitch print(room['raised'] / float(len(room['users']))) items = amzn.find_item(int(room['raised'] / float(len(room['users']))), 3) self.rooms.update_one({'id': room_id}, {'$set': {'users': room['users'], 'items': items}, '$inc': {'raised': pitch}}) room.pop('_id', None) self.write(json.dumps(room))
def put(self, room_id): room = self.rooms.find_one({'id': room_id}) print(room) if not room: self.fail() return data = tornado.escape.json_decode(self.request.body) user_id = data['id'] user = self.users.find_one({'id': user_id}) if not user: self.fail() return if user['id'] in room: self.fail() return user_data = { 'donated': 1, 'prize_url': '', 'user_id': user_id, } room['users'][user_id] = user_data room['raised'] += 1 items = amzn.find_item(int(room['raised'] / float(len(room['users']))), 3) self.rooms.update_one({'id': room_id}, {'$set': {'users': room['users'], 'items': items}, '$inc': {'raised': 1}}) room.pop('_id') self.write(json.dumps(room))
def post(self): data = tornado.escape.json_decode(self.request.body) user_id = data['id'] user = self.users.find_one({'id': user_id}) print(user) if not user: self.fail() return user_data = { 'donated': 1, 'prize_url': '', 'user_id': user_id, } start_time = datetime.datetime.utcnow() room_id = uuid.uuid4().hex[:10] # generate 10 items < 1 items = amzn.find_item(1, 3) print('lol') room = { 'id': room_id, 'items': items, 'raised': 1, 'start_time': start_time.isoformat(), 'users': { user_id: user_data } } self.rooms.insert_one(room) run_at = start_time + timedelta(hours=1) delay = (run_at - start_time).total_seconds() #threading.Timer(delay, buy_items).start() room.pop('_id', None) self.write(json.dumps(room))