-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstorage.py
128 lines (111 loc) · 3.76 KB
/
storage.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/local/bin/python3
# -*- coding: utf-8 -*-
from google.appengine.ext import ndb
from google.appengine.api import users
import logging
import time
from datetime import datetime
from common import redFont0, fontClose
def getHashForUser(user):
return 'new hash'
try:
from myhashlib import getHashForUser
except (ImportError):
print 'myhashlib ImportError'
class User(ndb.Model):
email = ndb.StringProperty()
confirmed = ndb.BooleanProperty()
reqCount = ndb.IntegerProperty(default=1)
account = ndb.UserProperty(required=True)
reqProps = ndb.StringProperty(repeated=True)
date = ndb.DateTimeProperty(auto_now_add=True)
premium = ndb.BooleanProperty()
active = ndb.BooleanProperty()
accHash = ndb.StringProperty()
reservedS = ndb.StringProperty()
reservedI = ndb.IntegerProperty()
class Req(ndb.Model):
reqProps = ndb.StringProperty()
date = ndb.DateTimeProperty(auto_now_add=True)
def addUserTrainReq(reqProps):
user = users.get_current_user()
if user:
items = reqProps.split('|')
ditems = items[4].split('.')
d1 = datetime(year=int(ditems[2]), month=int(ditems[1]), day=int(ditems[0]), hour=int(ditems[3]))
q = User.query()
logging.info('fetch user: ' + user.email())
q = q.filter(User.account == user)
r = q.fetch(1)
if not len(r):
u = User(account=user,
date = d1,
reqProps = items, active = True, accHash = getHashForUser(user))
u.put()
return 'request registred'
else:
logging.info('update already exist user: ' + user.email())
currentUserInfo = r[0]
if not currentUserInfo.active:
currentUserInfo.active = True
currentUserInfo.reqCount = currentUserInfo.reqCount + 1
currentUserInfo.date = d1
currentUserInfo.reqProps = items
currentUserInfo.put()
return 'request registred'
else:
logging.info('can\'t update already have active req: ' + user.email())
return 'can\'t register request, already have active request'
def getMailPlan():
d0 = datetime.fromtimestamp(time.time() + 14400)
q = User.query().filter(User.active == True)
sendList = []
for result in q:
if (result.date < d0):
result.active = False
logging.info('disable user activity by time: ' + result.account.email())
result.put()
else:
sendList.append(result)
return sendList
def disableTrainReq(h):
logging.info('disable hash: %s' % h)
q = User.query().filter(User.accHash == h)
r = q.fetch(1)
if len(r):
logging.info('found hash')
currentUserInfo = r[0]
currentUserInfo.active = False
currentUserInfo.put()
return 'request disabled'
def getUsers():
#clearStorage() # !!!!
q = User.query().fetch()
sOut = ''
for result in q:
if result.active:
sOut += "%s active=%s%s%s reqCount=%d <br>" % (result.account.email(), redFont0, str(result.active), fontClose, result.reqCount)
else:
sOut += "%s active=%s reqCount=%d <br>" % (result.account.email(), str(result.active), result.reqCount)
return sOut
def addReq(req):
q = Req.query()
req = req.lower()
q = q.filter(Req.reqProps == req)
r = q.fetch(1)
if not len(r):
u = Req(reqProps = req,
date = datetime.fromtimestamp(time.time() + 14400))
u.put()
def getReq():
#clearStorage()
q = Req.query().fetch()
sOut = ''
for result in q:
sOut += "%s %s<br>" % (result.reqProps, str(result.date))
return sOut
def clearReq():
ndb.delete_multi(Req.query().fetch(keys_only=True))
def clearStorage():
ndb.delete_multi(User.query().fetch(keys_only=True))
ndb.delete_multi(Req.query().fetch(keys_only=True))