How to limit Django-nonrel query result set within Google App Engine? - google-app-engine

I have a simple model:
class News(models.Model):
title = models.CharField(max_length=255, verbose_name=_("title"))
content = models.TextField(default='', verbose_name=_("content"))
panel = models.CharField(max_length=50, default='', verbose_name=_("panel"))
created = TzDateTimeProperty(auto_now_add=True, verbose_name=_("date created"))
lastmodified = TzDateTimeProperty(auto_now=True, verbose_name=_("date modified"))
I want to get the 5 recent news records, and I know that with Google App Engine DB queryset I can get 5 recent records in the following easy way:
results = News.all().filter(panel = panel).order('created').fetch(5)
With Django running in Google App Engine I would need to do:
results = News.objects.filter(panel = panel).order_by('created')[:5]
But it will throw exception if there are no news records. I could wrap it within catch exception, but what is the proper and optimized way to limit query results within Django?

You can do something like this
results = News.objects.filter(panel = panel).order_by('created')
if results is not None:
new_results = results[:5]

Related

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()

Export Data From Cloud Datastore

Many years ago I wrote an app on Google App Engine. I saved over 100,000 entries.
Here's the code I used to store it in the blob. Now I want to export all the data. Ideally, I would have prefered to download all these entries as a csv but there is no option on the backend. How can I easily download all the data I have saved over the years?
import webapp2
from google.appengine.ext import ndb
class Database(ndb.Model):
"""Models an individual user entry with email, app, and date."""
email = ndb.StringProperty()
app = ndb.StringProperty()
platform = ndb.StringProperty()
date = ndb.DateTimeProperty(auto_now_add=True)
class add(webapp2.RequestHandler):
def post(self):
e = self.request.get("email")
a = self.request.get("app")
p = self.request.get("platform")
b = e+', '+a+', '+p
u = Database()
u.email = e
u.app = a
u.platform = p
u.put()
Is there a way to get all of my data? I cannot output all the data on a webpage. It crashes.

Unable to output result of query on web framework using Google App Engine Datastore on Python

I am making an app on Google App Engine and I have managed to store data on it's Datastore. However, I am not able to output any of my queries from the Datastore. My code is as follows.
class twitternames(db.Model):
retweetersids = db.IntegerProperty(required = True)
retweetersnames = db.StringProperty(required = True)
user_input = db.StringProperty(required= True)
datetimeadded = db.DateTimeProperty(auto_now_add = True)
q = twitternames.all()
q.filter ("retweetersnames=", "eriklikestorawr")
for p in q.run():
self.response.write(p.retweetersids)
I am trying to give retweetersnames and get retweetersids. The following is a snapshot of stored data on the datastore.
Your help much appreciated.
Thank you
Managed to solve it. I was using post request instead of get.

GAE - Retrieving Database content via commandline

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.

User photo google appengine

I would like to display user images in my web app from a users contacts. I have the following:
qry = gdata.contacts.client.ContactsQuery(max_results=10)
feed = gd_client.get_contacts(query=qry)
for cont in feed.entry:
self.response.out.write(cont.name)
photo = cont.GetPhotoLink().href
self.response.out.write("<img src='"+photo+"'>")
I have no idea why this isn't working as the links it produces seem right per this guide https://developers.google.com/google-apps/contacts/v3/#retrieving_a_contacts_photo
https://www.google.com/m8/feeds/photos/media/currentlyautheduser%40gmail.com/27e1e1d70bb51465
and the gd_client seems to be authed correctly (I can see the contacts other details...)
the code for my contacts client is as following
def AuthedContactsClient():
current_user = users.GetCurrentUser()
gd_client = gdata.contacts.client.ContactsClient(source=SETTINGS['APP_NAME'])
gd_client.ssl=True
access_token_key = 'access_token_%s' % current_user.user_id()
gd_client.auth_token = gdata.gauth.ae_load(access_token_key)
return gd_client
Any ideas? Thanks!

Resources