Giving Positions

0 views
Skip to first unread message

PJ

unread,
Jun 30, 2008, 4:21:06 PM6/30/08
to Google App Engine
Hi,
I'm developing a fantast league application but I'm having an issue
with the league issuing positions both in the oveall rankings and in
the private rankings and I'm not sure of the best way to do this.
At the minute I have this for sorting overall:

class SortOverall(webapp.RequestHandler):
def post(self):
weekend = int(self.request.get("weekend"))
## print weekend
## print '<br>'
teams_query = UserTeams.all().filter("nextsort =",weekend
+1).order('-position')
teams = teams_query.fetch(1)
a = len(teams)
if a == 0:
pos = 0
else:
pos = teams[0].position
## print pos
## print '<br>'
teams_query = UserTeams.all().filter("nextsort =",weekend).order('-
points')
teams = teams_query.fetch(500)
for p in teams:
key = p.key()
po = db.get(key)
pos = pos + 1
po.lastposition = p.position
po.position = pos
po.nextsort = weekend + 1
## print po.teamname
## print ' - '
## print po.position
## print '<br>'
db.put(po)

self.redirect('/results')

If you leave in the print actions you can see that its finding the
right entries and ordering them correctly put its not putting the
resulting number into the database, although it will do it
intermittantly (maybe 30% of the time).

For sorting in the private leagues I have this:

class SortLeague(webapp.RequestHandler):
def post(self):
weekend = int(self.request.get("weekend"))
league_query = League.all().filter("nextsort =",weekend)
league = league_query.fetch(500)
for p in league:
team_query = LeagueTeams.all().filter("league =",str(p.key()))
teams = team_query.fetch(500)
for q in teams:
## print q.player
## print '<br>'
a = db.get(q.team)
points = a.points
## print points
## print'<br>'
b = db.get(q.key())
## print b
## print '<br>'
b.points = points
db.put(b)
## print b
## print '<br>'
team_query = LeagueTeams.all().filter("league
=",str(p.key())).order("-points")
teams = team_query.fetch(500)
a = 0
## print 'stage 2'
for q in teams:
a = a+1
b = db.get(q.key())
## print q.leaguename
## print '<br>'
b.lastposition = q.position
b.position = a
db.put(b)





key = p.key()
po = db.get(key)
po.nextsort = weekend + 1

db.put(po)
## print po.name
## print '<br>'
self.redirect('/results')

but it produces the same results - the print bits seem to indicate its
working but no data is being inserted into the datastore, although it
does, again, work intermittanly, although maybe only 10% of the time.

Does anyone know how to fix this, or a better way of doing this in the
first place?
Any help would be appreciated,
Paul

PJ

unread,
Jun 30, 2008, 6:20:28 PM6/30/08
to Google App Engine
Just a bit to add to this:
When I attempted to change the league sorting function to this to
update all the points and then positions (based on
http://code.google.com/appengine/docs/datastore/creatinggettinganddeletingdata.html#Creating_and_Updating_an_Entity
, 3rd code snippet) I got this:

class SortLeague(webapp.RequestHandler):
def post(self):
weekend = int(self.request.get("weekend"))
league_query = League.all().filter("nextsort =",weekend)
league = league_query.fetch(500)
for p in league:
team_query = LeagueTeams.all().filter("league =",str(p.key()))
teams = team_query.fetch(500)
for q in teams:

a = db.get(q.team)
points = a.points

q.points = points

db.put(teams)
team_query = LeagueTeams.all().filter("league
=",str(p.key())).order("-points")
teams = team_query.fetch(500)
a = 0

for q in teams:
a = a+1

q.lastposition = q.position
q.position = a
db.put(teams)



p.nextsort = weekend+1

db.put(league)
self.redirect('/results')

But it produces an error of BadRequestError: All entities must be in
the same entity group. Would changing to this method solve the
problem, and if it would how do I get all of the entities into the
same group?
Thanks for any help - I'm nearly ready to kick the computer in
frustration, and this is normally the stage at which I get told the
answers really obvious :)
Paul
Reply all
Reply to author
Forward
0 new messages