-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
executable file
·52 lines (44 loc) · 1.46 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""utils.py - File for collecting general utility functions."""
import logging
from google.appengine.ext import ndb
import endpoints
from models import Score
def get_by_urlsafe(urlsafe, model):
"""Returns an ndb.Model entity that the urlsafe key points to. Checks
that the type of entity returned is of the correct kind. Raises an
error if the key String is malformed or the entity is of the incorrect
kind
Args:
urlsafe: A urlsafe key string
model: The expected entity kind
Returns:
The entity that the urlsafe Key string points to or None if no entity
exists.
Raises:
ValueError:"""
try:
key = ndb.Key(urlsafe=urlsafe)
except TypeError:
raise endpoints.BadRequestException('Invalid Key')
except Exception, e:
if e.__class__.__name__ == 'ProtocolBufferDecodeError':
raise endpoints.BadRequestException('Invalid Key')
else:
raise
entity = key.get()
if not entity:
return None
if not isinstance(entity, model):
raise ValueError('Incorrect Kind')
return entity
def wins_minus_losses_count(user):
"""Calculates a user's score from games won or lost"""
scores = Score.query(Score.user == user.key)
count_wins = 0
count_losses = 0
for score in scores:
if (score.won == True):
count_wins += 1
else:
count_losses += 1
return count_wins - count_losses