Skip to content

Commit

Permalink
TeamTotal.num_pledges incremented on pledge save
Browse files Browse the repository at this point in the history
  • Loading branch information
jparker165 committed Jun 23, 2014
1 parent e0963b8 commit 13ac586
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
3 changes: 2 additions & 1 deletion backend/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,8 @@ def get(self):
offset=offset, limit=limit):
teams.append({
"team": tt.team,
"total_cents": tt.totalCents})
"total_cents": tt.totalCents,
"num_pledges": tt.num_pledges})

self.response.headers['Content-Type'] = 'application/json'
json.dump({"teams": teams}, self.response)
Expand Down
14 changes: 10 additions & 4 deletions backend/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ class Error(Exception): pass
# State-of-the-art digital canvassing and field tools
# No negative ads
# Whatever helps us win
# 11: TeamTotal.num_pledges added. This is a live total of completed pledges for
# that team.
#
MODEL_VERSION = 10
MODEL_VERSION = 11


# Config singleton. Loaded once per instance and never modified. It's
Expand Down Expand Up @@ -252,13 +254,16 @@ class TeamTotal(db.Model):

totalCents = db.IntegerProperty(required=False)

num_pledges = db.IntegerProperty(required=True)

@classmethod
@db.transactional
def _create(cls, team_id, pledge_8_count):
def _create(cls, team_id, pledge_8_count, num_pledges):
tt = cls.get_by_key_name(team_id)
if tt is not None:
return tt
tt = cls(key_name=team_id, team=team_id, totalCents=pledge_8_count)
tt = cls(key_name=team_id, team=team_id, totalCents=pledge_8_count,
num_pledges=num_pledges)
tt.put()
return tt

Expand All @@ -275,7 +280,7 @@ def _pledge8Count(team_id):
def _get(cls, team_id):
tt = cls.get_by_key_name(team_id)
if tt is None:
tt = cls._create(team_id, cls._pledge8Count(team_id))
tt = cls._create(team_id, cls._pledge8Count(team_id), 0)
return tt

@classmethod
Expand All @@ -287,6 +292,7 @@ def get(cls, team_id):
def _add(cls, team_id, amount_cents):
tt = cls.get_by_key_name(team_id)
tt.totalCents += amount_cents
tt.num_pledges += 1
tt.put()

@classmethod
Expand Down
19 changes: 19 additions & 0 deletions unittests/test_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,25 @@ def makeDefaultRequest(self, phone=None, pledgePageSlug=None):

return self.app.post_json('/r/pledge', self.pledge)

def testTeamTotalModel(self):
for _ in range(3):
self.expectStripe()
self.expectSubscribe()
self.mockery.ReplayAll()

self.assertEquals(model.TeamTotal.all().count(), 0)
self.app.post_json('/r/pledge', self.pledge)
tt = model.TeamTotal.all()[0]
self.assertEquals(tt.totalCents, self.pledge["amountCents"])
self.assertEquals(tt.num_pledges, 1)

self.app.post_json('/r/pledge', self.pledge)
self.app.post_json('/r/pledge', self.pledge)
self.assertEquals(model.TeamTotal.all().count(), 1)
tt = model.TeamTotal.all()[0]
self.assertEquals(tt.totalCents, 12600)
self.assertEquals(tt.num_pledges, 3)

def testMailOnCreatePledge(self):
self.makeDefaultRequest()

Expand Down

0 comments on commit 13ac586

Please sign in to comment.