diff --git a/app/models.py b/app/models.py index ddf25a8d..f1624ed6 100644 --- a/app/models.py +++ b/app/models.py @@ -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']) diff --git a/app/views.py b/app/views.py index f90fea80..175a3581 100644 --- a/app/views.py +++ b/app/views.py @@ -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): @@ -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 @@ -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, @@ -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", {} diff --git a/requirements.txt b/requirements.txt index 4bc1c050..c3441c5c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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