Skip to content

Commit

Permalink
Merge pull request #176 from sympy/use-datastore-remove-ndb
Browse files Browse the repository at this point in the history
Use datastore directly and remove ndb
  • Loading branch information
aktech authored Apr 7, 2021
2 parents b592156 + 637c3ea commit 7b0f114
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
9 changes: 2 additions & 7 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@
# https://github.com/googleapis/python-ndb/issues/249#issuecomment-560957294
six.moves.reload_module(six)

from google.cloud import ndb
from google.cloud import datastore

ndb_client = ndb.Client(project=os.environ['PROJECT_ID'])


class Query(ndb.Model):
text = ndb.StringProperty()
date = ndb.DateTimeProperty(auto_now_add=True)
user_id = ndb.StringProperty()
datastore_client = datastore.Client(project=os.environ['PROJECT_ID'])
33 changes: 19 additions & 14 deletions app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@

import logging

from google.cloud import datastore

ndb_client = models.ndb_client

datastore_client = models.datastore_client


class MobileTextInput(forms.widgets.TextInput):
Expand Down Expand Up @@ -69,8 +71,12 @@ def index(request):


def input_exists(input):
with ndb_client.context():
return models.Query.query(models.Query.text == input).get()
logging.info(f'Checking if input exists...: {input}')
query = datastore_client.query(kind='Query')
query = query.add_filter('text', '=', input)
result = list(query.fetch(limit=1))
logging.info(f'Input result: {result}')
return result


@app_meta
Expand All @@ -95,12 +101,16 @@ def input(request):
}]

if not input_exists(input):
logging.info('Input does not exists')
with ndb_client.context():
query = models.Query(text=input, user_id=None)
logging.info('query: %s' % query)
query.put()

logging.info('Input does not exists, inserting into datastore..')
entity = datastore.Entity(key=datastore_client.key('Query'))
entity.update({
"text": input,
"user_id": None,
"date": datetime.datetime.utcnow(),
})
logging.info(f'Inserting entity: {entity}')
put_result = datastore_client.put(entity)
logging.info(f'Input insert result: {put_result}')
# For some reason the |random tag always returns the same result
return ("result.html", {
"input": input,
Expand Down Expand Up @@ -237,11 +247,6 @@ def get_card_full(request, card_name):
return response


def find_text_query(query):
with ndb_client.context():
return models.Query.query(models.Query.text == query.text)


@app_meta
def view_404(request, exception):
return "404.html", {}
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Django==3.0.8
numpy==1.18.4
grpcio==1.29.0
googleapis_common_protos==1.51.0
google-cloud-ndb==1.3.0
google-cloud-datastore==2.1.0
requests-toolbelt==0.9.1
protobuf==3.12.2
enum34==1.1.10
Expand Down

0 comments on commit 7b0f114

Please sign in to comment.