-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsave_handler.py
66 lines (57 loc) · 2.24 KB
/
save_handler.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
import datetime
import logging
import time
from xml.dom.minidom import *
from google.appengine.ext import db
from google.appengine.ext import ndb
import webapp2
import models
class SaveHandler(webapp2.RequestHandler):
def post(self):
dom = parseString(self.request.body)
wsRoot = dom.getElementsByTagName('workspace').item(0)
wsName = wsRoot.getAttribute('name')
try:
nextNoteNum = int(wsRoot.getAttribute('nextNoteNum'))
except:
nextNoteNum = 0
notesJsonArray = []
nlNotes = wsRoot.getElementsByTagName('note')
for i in range(nlNotes.length):
node = nlNotes.item(i)
note = {}
for j in range(node.attributes.length):
attr = node.attributes.item(j)
if attr.name in models.Notes.DBKEYS: # only use valid attributes
note[models.Notes.DB2JS[attr.name]] = attr.nodeValue
note['text'] = node.firstChild.nodeValue.strip()
notesJsonArray.append(note)
def txn():
nowtime = datetime.datetime.now().replace(microsecond=0)
workspace = models.Workspace.get_by_wsName(wsName)
if not workspace:
workspace = models.Workspace.create(
wsName, nextNoteNum, nowtime)
workspace.put()
workspace.time = nowtime
workspace.nextNoteNum = nextNoteNum
workspace.put()
notes = models.Notes(
workspaceKey=workspace.key,
time=nowtime,
notesJsonArray=notesJsonArray)
notes.put()
for i in range(5):
try:
ndb.transaction(txn, xg=True)
break
except db.TransactionFailedError:
time.sleep(1.1)
origin = self.request.headers.get('Origin', '')
logging.info(origin)
if origin in ('http://localhost:8080', 'http://www.aypwip.org', 'https://www.aypwip.org'):
self.response.headers['Access-Control-Allow-Origin'] = origin
self.response.content_type = 'text/xml'
self.response.write(
'<return><status value="ok" update="%s"/></return>'
% '2017-11-01 12:12:12')