Exemplo n.º 1
0
    def _CalculateScore(self):
        # NOTE: Since the 'score' property is a ComputedProperty, it will
        # be re-computed before every put. Consequently, when a Blockable is put for
        # the first time, we won't see a pre-existing value for 'score'. Here, we
        # avoid the score calculation for newly-created Blockables as they shouldn't
        # have any Votes associated with them and, thus, should have a score of 0.
        if not datastore_utils.HasValue(self, 'score'):
            return 0

        tally = 0
        votes = self.GetVotes()
        for vote in votes:
            if vote.was_yes_vote:
                tally += vote.weight
            else:
                tally -= vote.weight
        return tally
Exemplo n.º 2
0
    def testHasValue(self):
        class Foo(ndb.Model):
            a = ndb.ComputedProperty(lambda self: 'a')
            b = ndb.StringProperty()

        foo = Foo()
        self.assertFalse(datastore_utils.HasValue(foo, 'a'))
        self.assertFalse(datastore_utils.HasValue(foo, 'b'))

        foo.b = 'b'
        self.assertFalse(datastore_utils.HasValue(foo, 'a'))
        self.assertTrue(datastore_utils.HasValue(foo, 'b'))

        foo.put()
        self.assertTrue(datastore_utils.HasValue(foo, 'a'))
        self.assertTrue(datastore_utils.HasValue(foo, 'b'))