Skip to content

Commit

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

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

import json

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


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


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


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

def save_word_datastore(user_id, save_word):
try:
key = CLIENT.key(CLIENT_KEY, user_id)
user = CLIENT.get(key)
user['words'] += u',' + save_word
user['count'] += 1
user = User.get_by_id(user_id)
user.words += u',' + save_word
user.count += 1
except Exception:
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)
user = User(id=user_id)
user.words = save_word
user.count = 1
user.last_word = save_word
user.put()


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

0 comments on commit cfb8ded

Please sign in to comment.