def _get_article(id, session=Session): article = None try: id = abs(int(id)) if id > 2147483647: raise ValueError article = session.query(Article).get(id) except ValueError, e: article = session.query(Article) \ .filter(Article.title==unicode(id)) \ .first()
def _get_article(id, session = Session): article = None try: id = abs(int(id)) if id > 2147483647: raise ValueError article = session.query(Article).get(id) except ValueError,e: article = session.query(Article) \ .filter(Article.title==unicode(id)) \ .first()
def players(self): session = Session() players = [{'first': p.first, 'last': p.last, 'position': p.position, 'id': p.id} for p in session.query(Player).all()] return players
def multifeed_handler(self): """Handles callback for multiFeedStory form submission.""" log_fb_request(request) facebook.process_request() c.is_app_user = facebook.api_client.added donor_fbid = request.POST.get('donor') recipient_fbid = request.POST.get('recipient') donation_id = request.POST.get('did') # look up donation in DB session = meta.Session() donation = session.query(Donation).get(donation_id) if donation is None: err_obj = {"errorCode": 1, "errorTitle" : "Feed Error", "errorMessage" : "Cannot find donation information."} return json.dumps(err_obj) gifthref = "%s/gift?id=%d" % (c.canvas_url, donation.id) template_data = {} template_data['gift'] = donation.gift.name template_data['gifthref'] = gifthref template_data['charity'] = donation.charity.name template_data['charityhref'] = donation.charity.url template_data['comments_xid'] = 'wg-gift.%d' % donation.id template_data['images'] = [{'src':h.gift_image_url(donation.gift_id), 'href':gifthref},] template_data['user_message'] = 'prompt3' obj = {'content': {'feed': {'template_id':110890046851, 'user_message':'prompt2', 'template_data':template_data}, 'next_fbjs': 'feed_done();'}, 'method':'multiFeedStory', 'user_message':'prompt1'} log.debug("multifeed_handler response JSON: %s" % json.dumps(obj)) return json.dumps(obj)
def view(self, name): try: c.player = model.Player.query.filter_by(name=name).one() except: abort(404) games_q = model.Game.query.filter_by(player=c.player) # Pull some simple stats c.game_count = games_q.count() # Count how this player's games are distributed across various # categories session = elixir.session count = func.count(model.Game.id).label('count') c.breakdowns = {} # label => [(table, count), ...] for table, label in [ (model.EndType, 'Ending'), (model.Role, 'Role'), (model.Race, 'Race'), (model.Gender, 'Gender'), (model.Alignment, 'Alignment'), ]: # Create a LEFT JOIN to get all of this player's games without # eliminating options s/he has never used # property.primaryjoin mumbo-jumbo courtsey of: # http://www.mail-archive.com/[email protected]/msg16304.html join = and_(getattr(table, 'games').property.primaryjoin, model.Game.player == c.player) # To be correct, the query must get the ids and their counts, then # join THAT again to get the full table rows. # Excellent subquery example from: # http://www.sqlalchemy.org/docs/ormtutorial.html#using-subqueries subq = session.query(table.id.label('table_id'), count) \ .outerjoin((model.Game, join)) \ .group_by(table.id) \ .subquery() q = session.query(table, subq.c.count) \ .outerjoin((subq, table.id == subq.c.table_id)) \ .order_by(subq.c.count.desc()) c.breakdowns[label] = q.all() return render('/players/view.mako')
def players(self): session = Session() players = [{ 'first': p.first, 'last': p.last, 'position': p.position, 'id': p.id } for p in session.query(Player).all()] return players
def add_player(self): first = self.form_result['first'] last = self.form_result['last'] position = self.form_result['position'] session = Session() if session.query(Player).filter_by(first=first, last=last).count() > 0: h.flash("Player already exists!") return h.redirect_to(controller='roster') player = Player(first, last, position) session.add(player) session.commit() return h.redirect_to(controller='roster', action='index')
def save_player(self): id = self.form_result['id'] first = self.form_result['first'] last = self.form_result['last'] position = self.form_result['position'] session = Session() player = session.query(Player).filter_by(id=id).one() player.first = first player.last = last player.last = last player.position = position session.commit() return h.redirect_to(controller='roster')
def delete_player(self): session = Session() id = self.form_result['id'] first = self.form_result['first'] last = self.form_result['last'] position = self.form_result['position'] players = session.query(Player).filter_by(id=id, first=first, last=last, position=position).all() if len(players) <> 1: h.flash("The player was modified by someone else while you were staring at the screen!") else: player = players[0] session.delete(player) session.commit() h.flash("Player %s was deleted" % player.id) return h.redirect_to(controller='roster')
def delete_player(self): session = Session() id = self.form_result['id'] first = self.form_result['first'] last = self.form_result['last'] position = self.form_result['position'] players = session.query(Player).filter_by(id=id, first=first, last=last, position=position).all() if len(players) <> 1: h.flash( "The player was modified by someone else while you were staring at the screen!" ) else: player = players[0] session.delete(player) session.commit() h.flash("Player %s was deleted" % player.id) return h.redirect_to(controller='roster')
def ajax_transaction_status(self): """Ajax call from wrap_it_up page to get transaction status.""" log_fb_request(request) facebook.process_request() c.is_app_user = facebook.api_client.added if not c.is_app_user: return self._ajax_fault("not app user!") elif facebook.fb_params.get('is_ajax', 0) == 0: return self._ajax_fault("not ajax!") donation_id = request.POST.get('did') if donation_id is None: return self._ajax_fault("need to specify donation ID") # look up donation in DB session = meta.Session() donation = session.query(Donation).get(donation_id) obj = {} obj['error'] = False obj['transactionStatus'] = donation.transaction_status return json.dumps(obj)
def _nearestEdge(self, wkt): distance = functions.distance(ways_table.c.the_geom, "ST_GeomFromText('" + wkt + "', 4326)").label('dist') # find the nearest way return session.query(ways_table, distance).order_by('dist')
@jsonify def players(self): session = Session() players = [{'first': p.first, 'last': p.last, 'position': p.position, 'id': p.id} for p in session.query(Player).all()] return players @jsonify def add_player(self): obj = json.loads(request.body) schema = PlayerForm() try: form_result = schema.to_python(obj) except formencode.Invalid, error: response.content_type = 'text/plain' return 'Invalid: '+unicode(error) else: session = Session() first, last, position = obj['first'], obj['last'], obj['position'] if session.query(Player).filter_by(last=last, first=first, position=position).count() == 0: session.add(Player(first, last, position)) session.commit() return {'result': 'OK'} else: return {'result':'fail', 'msg': 'Player already exists'}
def edit_player(self, id): session = Session() player = session.query(Player).filter_by(id=id).one() c.player = player return render('edit_player.html')
def index(self): db_session = Session() c.page_title = 'Player List' c.players = session.query(Player).all() return render('list_players.html')
def players(self): session = Session() players = [{ 'first': p.first, 'last': p.last, 'position': p.position, 'id': p.id } for p in session.query(Player).all()] return players @jsonify def add_player(self): obj = json.loads(request.body) schema = PlayerForm() try: form_result = schema.to_python(obj) except formencode.Invalid, error: response.content_type = 'text/plain' return 'Invalid: ' + unicode(error) else: session = Session() first, last, position = obj['first'], obj['last'], obj['position'] if session.query(Player).filter_by(last=last, first=first, position=position).count() == 0: session.add(Player(first, last, position)) session.commit() return {'result': 'OK'} else: return {'result': 'fail', 'msg': 'Player already exists'}