Skip to content

Commit

Permalink
Use a hash of the name as workspace key.
Browse files Browse the repository at this point in the history
Save Workspace and Notes in a transaction.

Both of these prevent there being Workspaces
with duplicate names.
  • Loading branch information
idealisms committed Nov 14, 2017
1 parent 8abd137 commit 2a2a931
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion getdates_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def get(self, **kwargs):
offset = self.request.get('offset', '0')
if not name:
return
workspace = models.Workspace.query(models.Workspace.name==name).get()
workspace = models.Workspace.get_by_wsName(name)
if not workspace:
return

Expand Down
2 changes: 1 addition & 1 deletion getrecent_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def get(self, **kwargs):
name = self.request.get('name', '')
if not name:
return
workspace = models.Workspace.query(models.Workspace.name==name).get()
workspace = models.Workspace.get_by_wsName(name)
if not workspace:
return
self.response.write(workspace.localtime_str())
3 changes: 1 addition & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ def get(self, **kwargs):
notes = []
logging.info(name)

workspace = models.Workspace.query(models.Workspace.name==name).order(
-models.Workspace.time).get()
workspace = models.Workspace.get_by_wsName(name)
if workspace:
nextNoteNum = workspace.nextNoteNum
lasttime = workspace.localtime_str()
Expand Down
15 changes: 14 additions & 1 deletion models.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
import datetime
import hashlib

import pytz
from google.appengine.ext import ndb

TIMEZONE = pytz.timezone('US/Eastern')

class Workspace(ndb.Model):
name = ndb.StringProperty(required=True)
name = ndb.StringProperty()
nextNoteNum = ndb.IntegerProperty(required=True)
time = ndb.DateTimeProperty(required=True)

@staticmethod
def get_by_wsName(wsName):
key_id = hashlib.sha1(wsName).hexdigest()
return Workspace.get_by_id(key_id)

@staticmethod
def create(wsName, nextNoteNum, time):
key_id = hashlib.sha1(wsName).hexdigest()

return Workspace(id=key_id, name=wsName, nextNoteNum=nextNoteNum,
time=time)

def localtime_str(self):
local_datetime = pytz.utc.localize(self.time).astimezone(TIMEZONE)
return local_datetime.strftime('%Y-%m-%d %H:%M:%S')
Expand Down
12 changes: 4 additions & 8 deletions save_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,12 @@ def post(self):
note['text'] = node.firstChild.nodeValue.strip()
notesJsonArray.append(note)

#@ndb.transactional
def txn():
nowtime = datetime.datetime.now().replace(microsecond=0)
workspace = models.Workspace.query(
models.Workspace.name==wsName).get()
workspace = models.Workspace.get_by_wsName(wsName)
if not workspace:
workspace = models.Workspace(
name=wsName,
nextNoteNum=nextNoteNum,
time=nowtime)
workspace = models.Workspace.create(
wsName, nextNoteNum, nowtime)
workspace.put()

workspace.time = nowtime
Expand All @@ -50,7 +46,7 @@ def txn():
time=nowtime,
notesJsonArray=notesJsonArray)
notes.put()
#ndb.transaction(txn, xg=True)
ndb.transaction(txn, xg=True)
txn()

origin = self.request.headers.get('Origin', '')
Expand Down

0 comments on commit 2a2a931

Please sign in to comment.