-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodels.py
More file actions
58 lines (46 loc) · 1.44 KB
/
models.py
File metadata and controls
58 lines (46 loc) · 1.44 KB
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
53
54
55
56
57
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright © 2015 vagrant
#
# Distributed under terms of the MIT license.
from google.appengine.ext import ndb, db
from datetime import datetime, timedelta
import logging
import random
from google.appengine.ext import ndb
class ValentineInfo(ndb.Model):
name = ndb.StringProperty(indexed=False)
photo = ndb.StringProperty(indexed=False)
who = ndb.StringProperty(indexed=False)
commit = ndb.StringProperty(indexed=False)
id = ndb.StringProperty(indexed=False)
@classmethod
def create(cls, data):
ValentineInfoCounter.increment()
instance = cls(**data)
instance.put()
return instance
@classmethod
def get_count(cls):
return ValentineInfoCounter.get_count()
class ValentineInfoCounter(ndb.Model):
"""Shards for the counter"""
count = ndb.IntegerProperty(default=0)
num_shards = 20
@classmethod
def get_count(cls):
total = 0
for counter in cls.query():
total += counter.count
return total
@classmethod
@ndb.transactional
def increment(cls):
"""Increment the value for a given sharded counter."""
shard_string_index = str(random.randint(0, cls.num_shards - 1))
counter = cls.get_by_id(shard_string_index)
if counter is None:
counter = cls(id=shard_string_index)
counter.count += 1
counter.put()