def robots(self, userid=None): """List all the available robots.""" #TODO: get robot list from somewhere (user?) user_list = DBSession.query(model.Robot).filter_by(userid=userid).all() user_list = [x.name.split('@')[0] for x in user_list] friends = DBSession.query(model.Friend).filter_by(uid_left=userid).all() friend_list = [] for friend in friends: robots = DBSession.query(model.Robot).filter_by(userid=friend.uid_right).all() if robots: friend = DBSession.query(model.Login).filter_by(id=friend.uid_right).one() for robot in robots: friend_list.append("%s's %s" % (friend.name, robot.name.split('@')[0])) other_list = [] robo_list = [u'Ninja', u'Pirate', u'Robot', u'Wizard', u'Velociraptor', u'Zombie', u'robot07', u'robot08'] # Build a custom widget to hold the form.x class RoboForm(WebbotForm): class user(twf.RadioButtonList): options = user_list class example(twf.CheckBoxList): options = robo_list class friends(twf.CheckBoxList): options = friend_list class others(twf.CheckBoxList): options = other_list return dict(form=RoboForm(action='start_game'), page_title='', form_title='Here are all the robots you can play with:')
def games(self, userid=None): """List all the available games.""" user_games = DBSession.query(model.Game).filter_by(userid=userid).all() game_list = DBSession.query(model.Game) \ .filter(model.Game.userid != userid) \ .order_by(desc(model.Game.date)).limit(10).all() return dict(your_games=user_games, games=game_list)
def make_friends(self, **kwargs): import json data = json.loads(kwargs['data']) user_id = kwargs['uid'] for item in data['data']: uid = item['uid2'] if DBSession.query(model.Login).filter_by(id=uid).all(): if not DBSession.query(model.Friend).filter_by(uid_left=user_id).filter_by(uid_right=uid).all(): friendship = model.Friend(uid_left=user_id, uid_right=uid) DBSession.add(friendship) friendship = model.Friend(uid_left=uid, uid_right=user_id) DBSession.add(friendship)
def robots(self, userid=None): """List all the available robots.""" #TODO: get robot list from somewhere (user?) user_list = DBSession.query(model.Robot).filter_by(userid=userid).all() user_list = [x.name.split('@')[0] for x in user_list] robo_list = [u'Ninja', u'Pirate', u'Robot', u'Wizard', u'Velociraptor', u'Zombie', u'robot07', u'robot08'] # Build a custom widget to hold the form.x class RoboForm(WebbotForm): class user(twf.RadioButtonList()): options = user_list class example(twf.CheckBoxList()): options = robo_list return dict(form=RoboForm(action='start_game'), page_title='', form_title='Here are all the robots you can play with:')
def upload_code(self, **kw): code = kw['code'].value name = kw['name'] uid = kw['userid'] if not uid: flash('Must be logged in to upload robots.') else: # Try to detect OpenShiftiness base = os.environ.get('OPENSHIFT_REPO_DIR') or '../../' with open("%spybotwar/robots/%s@%s.py" % (base, name, uid), 'w') as local_file: local_file.write(code) # Save a ref to the file in the DB if not DBSession.query(model.Robot).filter_by(userid=uid).filter_by(name=name).all(): robot = model.Robot(userid=uid, name=name) DBSession.add(robot) redirect("/")
def test_query_obj(self): """Model objects can be queried""" obj = DBSession.query(self.klass).one() for key, value in self.attrs.iteritems(): assert_equals(getattr(obj, key), value)
def games(self): """List all the available games.""" game_list = DBSession.query(model.Game).all() print(game_list[0]) return dict(games=game_list)
def by_display_name(cls, dname): return DBSession.query(cls).filter_by(display_name=dname).first()
def by_user_id(self, userid): return DBSession.query(self).filter_by(user_id=userid).first()
def by_user_name(cls, username): """Return the user object whose user name is ``username``.""" return DBSession.query(cls).filter_by(user_name=username).first()
def by_email_address(cls, email): """Return the user object whose email address is ``email``.""" return DBSession.query(cls).filter_by(email_address=email).first()