-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.py
executable file
·131 lines (104 loc) · 3.17 KB
/
util.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
129
130
131
import re
import logging
import random
import base64
import struct
import time
import urllib
from urllib import urlencode, unquote_plus
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.api import urlfetch
from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers
from google.appengine.api import users
from google.appengine.api import mail
from json import loads, dumps
from airspeed import CachingFileLoader
from generic import TemplatePage, JsonPage, GenericPage, FilePage
from models import *
def notify(apikey, channel, msg):
logging.debug('notify('+str(channel)+'): '+str(msg))
fields={
'apikey': apikey,
'channel': channel,
'data': msg
}
url='http://send.w2p.dtdns.net:9323/send'
result=urlfetch.fetch(url, payload=urlencode(fields), method=urlfetch.POST)
logging.debug('post result: '+str(result.status_code))
def sendMail(frm, to, subject, templateName, context):
loader = CachingFileLoader("templates")
templateNameHTML=templateName+".vm"
templateHTML = loader.load_template(templateNameHTML)
bodyHTML=templateHTML.merge(context, loader=loader)
templateNamePlain=templateName+"_plain.vm"
templatePlain = loader.load_template(templateNamePlain)
bodyPlain=templatePlain.merge(context, loader=loader)
# resp.headers['Content-Type']='text/html'
msg=mail.EmailMessage(sender=frm, to=to, subject=subject)
msg.html=bodyHTML
msg.body=bodyPlain
msg.send()
def generateId():
s=None
if not (s and s[0].isalpha()):
i=random.getrandbits(64)
s=base64.urlsafe_b64encode(struct.pack('L', i))[:-1]
while s[-1]=='A' or s[-1]=='=':
s=s[:-1]
return s
def newSession(user):
logging.error('ns user: '+str(user))
sessionid=generateId()
while Session.all().filter("sessionid =", sessionid).count()!=0:
sessionid=generateId()
logging.info('using '+str(sessionid))
session=Session(user=user, sessionid=sessionid)
session.save()
return session
def getDocs(db):
results={}
docs=Document.all().filter('database =', db).run()
for rdoc in docs:
results[rdoc.docid]=loads(rdoc.state)
logging.info('results: '+str(results))
return results
def listDocs(db):
results=[]
docs=Document.all().filter('database =', db).run()
for rdoc in docs:
results.append(rdoc.docid)
logging.info('results: '+str(results))
return results
def loadConfig(db):
configDoc=Document.all().filter("database =", db).filter("docid =", "_config").get()
if not configDoc:
return {}
else:
return loads(configDoc.state)
def resolveConfig(config, keys):
value=config
for key in keys:
if key in value:
value=value[key]
else:
return None
return value
def lookupDb(path):
if type(path)!=list:
path=[path]
dbid=path[0]
path=path[1:]
db=Database.all().filter("dbid =", dbid).get()
if not db:
logging.error('Database with that id does not exist '+str(dbid))
return None
while len(path)>0:
dbid=path[0]
path=path[1:]
db=Database.all().ancestor(db).filter("dbid =", viewId).get()
if not db:
logging.error('Database with that id does not exist '+str(dbid))
return None
return db