Skip to content

Commit

Permalink
fix(infra.py): fix to used datestore. ref #402 @0.5h
Browse files Browse the repository at this point in the history
  • Loading branch information
hayatedayon committed Aug 4, 2018
1 parent 3620f58 commit 265bd7b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
37 changes: 19 additions & 18 deletions infra.py
Expand Up @@ -18,33 +18,30 @@
from __future__ import division
from __future__ import print_function

from google.appengine.ext import ndb
from google.cloud import datastore

import json

with open('data/dict.json') as json_file:
json_dic = json.load(json_file)


class User(ndb.Model):
words = ndb.TextProperty()
last_word = ndb.TextProperty()
count = ndb.IntegerProperty()
date = ndb.DateTimeProperty(auto_now_add=True)
CLIENT = datastore.Client()
CLIENT_KEY = 'User'


def reset_datastore(user_id):
try:
user = User.get_by_id(user_id)
user.key.delete()
key = CLIENT.key(CLIENT_KEY, user_id)
CLIENT.delete(key)
except Exception:
pass


def check_word_datastore(user_id, check_word):
try:
user = User.get_by_id(user_id)
words = user.words.split(u',')
key = CLIENT.key(CLIENT_KEY, user_id)
words = CLIENT.get(key)['words'].split(u',')
if check_word in words:
return False
return True
Expand All @@ -54,15 +51,19 @@ def check_word_datastore(user_id, check_word):

def save_word_datastore(user_id, save_word):
try:
user = User.get_by_id(user_id)
user.words += u',' + save_word
user.count += 1
key = CLIENT.key(CLIENT_KEY, user_id)
user = CLIENT.get(key)
user['words'] += u',' + save_word
user['count'] += 1
except Exception:
user = User(id=user_id)
user.words = save_word
user.count = 1
user.last_word = save_word
user.put()
key = CLIENT.key(CLIENT_KEY, user_id)
user = datastore.Entity(key=key)
user.update({
'words': save_word,
'count': 1,
})
user['last_word'] = save_word
CLIENT.put(user)


def search_reading_from_dic(search_word):
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
@@ -1,5 +1,5 @@
flake8
google-appengine
google-cloud-datastore
pylint
pyyaml
webtest
Expand Down

0 comments on commit 265bd7b

Please sign in to comment.