GAE - Retrieving Database content via commandline - google-app-engine

I have the following GQL database model:
class Post(db.Model):
subject = db.StringProperty(required = True)
content = db.TextProperty(required = True)
created = db.DateTimeProperty(auto_now_add = True)
And this is the POST request used to store content to the database
def post(self):
subject = self.request.get('subject')
content = self.request.get('content')
if subject and content:
a = Post(subject = subject, content = content)
a.put()
self.redirect("/")
else:
error = "subject and content can neither be empty"
self.Render_NewPost(subject, content, error)
If I POST content to the database it works alright since i don't get the error. However I don't get the contents to show on the page it is suppose to show.
I'm interested in knowing the command line instruction I can use to check the database to be sure if the contents been posted are actually in the database or not so I can know where to figure out the problem of the content not showing on my Homepage as I hoped.
Thanks

Wait 30 seconds and refresh the page /. You may be running into the issue of "eventual consistency", where the data will "eventually" be put into the physical datastore you've queried, but may not be there yet.

Related

How to reference GET data in a POST request?

I am writing a POST request where a user tags a picture, therefore an instance of the Tagging table should be created. The picture is sent to the user through the GET request. How to I access for example the resource object that was sent through the GET request in the POST request so that I assign a Tag to a specific resource?
This is what I have tried so far:
views.py
if not isinstance(request.user, CustomUser):
current_user_id = 1
else:
current_user_id = request.user.pk
random_resource = request.GET.data.get('resource')
tag = request.data.get('tag')
created = datetime.now()
language = request.data.get('language')

webapp2 post operation not updating datastore

I am developing a web application using google appengine.
I am trying to update an existing entry in the table but my post operation does not seem to be working.
post script:
r = requests.post("http://localhost:8080", data={'type': 'user', 'id':
'11111', 'name': 'test'})
When running the script there are no errors in the console and when prining r.text I can see the updated name but the page on localhost does not show the updated name and the datastore user still has its previous name.
My model for the main page is:
class Page(webapp2.RequestHandler):
def get(self):
...
def post(self):
user = User().get_user(self.request.get('id')) // query user from data store
user.name = self.request.get('name')
user.put()
// update jinja template
self.request.get('id') is a string. You want to use an integer for the id, or build a key (I am assuming you are using ndb):
user = ndb.Key('User', int(self.request.get('id'))).get()

GAE counting visitors using cookie

This codes count the number of time we have visited the page, until browser is closed, using cookies. Which I am not getting. Please help
class MainPage(Handler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
visits = self.request.cookies.get('visits','0')
if visits.isdigit():
visits = int(visits) + 1
else:
visits= 0
self.response.headers.add_header('Set-Cookie', 'visits=%s' %visits)
self.write("you've been here %s times" %visits)
I just want to know what is happening in these two lines
visits = self.request.cookies.get('visits','0')
and
self.response.headers.add_header('Set-Cookie', 'visits=%s' %visits)
Rather than just give you the answer, I'll help you figure out how to get it.
self.request and self.response are properties of the MainPage class. To figure out what these two things are doing you need to find out where they were defined.
The MainPage class is a subclass of the Handler class. You don't show the definition of the Handler class but somewhere in your code you will find that it is a subclass of webapp2.RequestHandler.
To find what the two lines in your code are doing, you should go read the online documentation for webapp2.
Now I understand these two lines:
visits = self.request.cookies.get('visits','0')
self.request= requesting from the browser
self.request.cookies = requesting cookies[basically a dictionary] from the browser
self.request.cookies.get('visits')= looking for cookie whose key is visits
self.request.cookies.get("visits",0)= if key not found make this key value 0 and return
so now visits in LHS equals to 0 , as for now cookies not contain visits cookie
self.response.headers.add_header('Set-Cookie', 'visits=%s' %visits)
self.response= sending from the server to browser
self.response.headers.add_header('Set-Cookie', 'visits=%s' %visits)=adding cookie to header as it is defined in header, and setting visits in the cookie

Multiple calls to put() method when updating ndb on google app engine

I have a web app on GAE which uses a ndb database where each entity has as properties user informations and two string, the Entity class is like the one below
class UserPlus(ndb.Model):
user = ndb.UserProperty()
dogName = ndb.StringProperty(indexed=False)
catName = ndb.StringProperty(indexed=False)
The Main Page check if there's already an entity corresponding to that user, and if yes displays the value of the strings dogName and catName.
Then there's a form where users can update the values of dogName and catName . This performs a POST request to another page, the method below update the entiy
def post(self):
currentUser = users.get_current_user()
up = UserPlus.query(UserPlus.user==currentUser).get()
up.dogName = self.request.get('dog_name')
up.catName = self.request.get('cat_name')
weatherUser.put()
self.redirect('/')
But when I'm redirected to the Main Page, the values of dogName and catName are not updated until I refresh the page. I found that by calling the put() method two times instead of one, in the same position, this doesn't occur anymore, but I don't have clear why.
Am I doing something wrong or it's how ndb is supposed to work?
As Guido suspects and bossylobster/Fred Saur answered on my old question here - Should I expect stale results after redirect on local environment? - most likely eventual consistency problem.

Strange memcache behaviour on GAE

I'm doing a simple CRUD system with memcache to make reads of tables faster, but I'm a bit confused with the behaviour.
Basically, this is the important code:
class Book(db.Model):
title = db.StringProperty(required=True)
#classmethod
def getAll(cls):
key = 'booklist'
books = memcache.get(key)
if books is None:
logging.info('DB access for key %s.', key)
books = Book.all().order("title").fetch(100)
if not memcache.set(key, books, 300):
logging.error('Memcache set failed for key %s.', key)
else:
logging.info('Memcache for key %s.', key)
return books
# Save a Book and return it
#classmethod
def save(cls, title):
book = Book(title=title)
book.put()
# Flush memcache for this key
if memcache.delete('booklist') != 2:
logging.error('Memcache delete failed')
return book
In the app code I can add a book, so, once I get the title from the form, I do this:
book = Book.save(title)
if book is None:
logging.error('Book save error.')
self.redirect("/showBooks")
The last redirect gets me to this code:
def get(self):
books = Book.getAll()
self.renderPage("listBooks.htm", data=books)
Which only get all the books and display it on a table. The problem is that the steps are executed so quickly, or something like that, because the save function flushes the cache (it appears in the log), the getAll function writes in the log 'DB access for key booklist' but nothing appears in the table, like if GAE were still saving the data in the datastore and the new element have no time to be readed.
And, if you just wait 15 seconds and refresh the page, then the data is there.
I've tried other thing: instead of save and inmediately refresh the page with the redirection, I made an "intermediate info page" with the message "successfully saved". If I go to the listing page from this intermediate page, without touching any other code, all works fine (the new data appears in the table, like was expected). Again, this makes me to think that GAE needs time to save the object and my immediate read I was doing originally is faster.
Obviously, if I flush manually the memcache, in the next page refresh all the new data appears.
Can this be true? My read is faster than the GAE write and is this the problem I'm having?

Resources