Skip to content

Commit 765ba16

Browse files
committed
Added samples from datastore-samples
1 parent ec2f327 commit 765ba16

11 files changed

Lines changed: 460 additions & 0 deletions

File tree

datastore/ndb/overview/README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
## NDB Overview Sample
2+
3+
This is a sample app for Google App Engine that exercises the [NDB Python API](https://cloud.google.com/appengine/docs/python/ndb/).
4+
5+
See our other [Google Cloud Platform github
6+
repos](https://github.com/GoogleCloudPlatform) for sample applications and
7+
scaffolding for other python frameworks and use cases.
8+
9+
## Run Locally
10+
1. Install the [Google Cloud SDK](https://cloud.google.com/sdk/), including the [gcloud tool](https://cloud.google.com/sdk/gcloud/), and [gcloud app component](https://cloud.google.com/sdk/gcloud-app).
11+
2. Setup the gcloud tool.
12+
13+
```
14+
gcloud components update app
15+
gcloud auth login
16+
gcloud config set project <your-app-id>
17+
```
18+
You don't need a valid app-id to run locally, but will need a valid id to deploy below.
19+
20+
1. Clone this repo.
21+
22+
```
23+
git clone https://github.com/GoogleCloudPlatform/datastore-pthon-samples.git
24+
cd datastore-python-samples/ndb-overview
25+
```
26+
1. Run this project locally from the command line.
27+
28+
```
29+
gcloud preview app run ./
30+
```
31+
32+
1. Visit the application at [http://localhost:8080](http://localhost:8080).
33+
34+
## Deploying
35+
36+
1. Use the [Cloud Developer Console](https://console.developer.google.com) to create a project/app id. (App id and project id are identical)
37+
2. Configure gcloud with your app id.
38+
39+
```
40+
gcloud config set project <your-app-id>
41+
```
42+
1. Use the [Admin Console](https://appengine.google.com) to view data, queues, and other App Engine specific administration tasks.
43+
1. Use gcloud to deploy your app.
44+
45+
```
46+
gcloud preview app deploy ./
47+
```
48+
49+
1. Congratulations! Your application is now live at your-app-id.appspot.com
50+
51+
## Contributing changes
52+
53+
* See [CONTRIBUTING.md](../../CONTRIBUTING.md)
54+
55+
## Licensing
56+
57+
* See [LICENSE](../../LICENSE)

datastore/ndb/overview/app.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# This file specifies your Python application's runtime configuration
2+
# including URL routing, versions, static file uploads, etc. See
3+
# https://developers.google.com/appengine/docs/python/config/appconfig
4+
# for details.
5+
6+
version: 1
7+
runtime: python27
8+
api_version: 1
9+
threadsafe: yes
10+
11+
# Handlers define how to route requests to your application.
12+
handlers:
13+
14+
# This handler tells app engine how to route requests to a WSGI application.
15+
# The script value is in the format <path.to.module>.<wsgi_application>
16+
# where <wsgi_application> is a WSGI application object.
17+
- url: .* # This regex directs all routes to main.app
18+
script: main.app
19+
20+
libraries:
21+
- name: webapp2
22+
version: "2.5.2"

datastore/ndb/overview/favicon.ico

8.15 KB
Binary file not shown.

datastore/ndb/overview/index.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
indexes:
2+
3+
# AUTOGENERATED
4+
5+
# This index.yaml is automatically updated whenever the dev_appserver
6+
# detects that a new type of query is run. If you want to manage the
7+
# index.yaml file manually, remove the above marker line (the line
8+
# saying "# AUTOGENERATED"). If you want to manage some indexes
9+
# manually, move them above the marker line. The index.yaml file is
10+
# automatically uploaded to the admin console when you next deploy
11+
# your application using appcfg.py.
12+
13+
- kind: Greeting
14+
ancestor: yes
15+
properties:
16+
- name: date
17+
direction: desc

datastore/ndb/overview/main.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Copyright 2015 Google Inc. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START all]
16+
import cgi
17+
import urllib
18+
19+
import webapp2
20+
21+
from google.appengine.ext import ndb
22+
23+
24+
# [START greeting]
25+
class Greeting(ndb.Model):
26+
"""Models an individual Guestbook entry with content and date."""
27+
content = ndb.StringProperty()
28+
date = ndb.DateTimeProperty(auto_now_add=True)
29+
# [END greeting]
30+
31+
# [START query]
32+
@classmethod
33+
def query_book(cls, ancestor_key):
34+
return cls.query(ancestor=ancestor_key).order(-cls.date)
35+
36+
37+
class MainPage(webapp2.RequestHandler):
38+
def get(self):
39+
self.response.out.write('<html><body>')
40+
guestbook_name = self.request.get('guestbook_name')
41+
ancestor_key = ndb.Key("Book", guestbook_name or "*notitle*")
42+
greetings = Greeting.query_book(ancestor_key).fetch(20)
43+
44+
for greeting in greetings:
45+
self.response.out.write('<blockquote>%s</blockquote>' %
46+
cgi.escape(greeting.content))
47+
# [END query]
48+
49+
self.response.out.write("""
50+
<form action="/sign?%s" method="post">
51+
<div><textarea name="content" rows="3" cols="60"></textarea></div>
52+
<div><input type="submit" value="Sign Guestbook"></div>
53+
</form>
54+
<hr>
55+
<form>Guestbook name: <input value="%s" name="guestbook_name">
56+
<input type="submit" value="switch"></form>
57+
</body>
58+
</html>""" % (urllib.urlencode({'guestbook_name': guestbook_name}),
59+
cgi.escape(guestbook_name)))
60+
61+
62+
# [START submit]
63+
class SubmitForm(webapp2.RequestHandler):
64+
def post(self):
65+
# We set the parent key on each 'Greeting' to ensure each guestbook's
66+
# greetings are in the same entity group.
67+
guestbook_name = self.request.get('guestbook_name')
68+
greeting = Greeting(parent=ndb.Key("Book", guestbook_name or "*notitle*"),
69+
content=self.request.get('content'))
70+
greeting.put()
71+
# [END submit]
72+
self.redirect('/?' + urllib.urlencode({'guestbook_name': guestbook_name}))
73+
74+
75+
app = webapp2.WSGIApplication([
76+
('/', MainPage),
77+
('/sign', SubmitForm)
78+
])
79+
# [END all]
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
## NDB Transactions Sample
2+
3+
This is a sample app for Google App Engine that exercises the [NDB Transactions Python API](https://cloud-dot-devsite.googleplex.com/appengine/docs/python/ndb/transactions)
4+
5+
This app presents a list of notes. After you submit a note with a particular title, you may not change that note or submit a new note with the same title. There are multiple note pages available.
6+
7+
See our other [Google Cloud Platform github
8+
repos](https://github.com/GoogleCloudPlatform) for sample applications and
9+
scaffolding for other python frameworks and use cases.
10+
11+
## Run Locally
12+
1. Install the [Google Cloud SDK](https://cloud.google.com/sdk/), including the [gcloud tool](https://cloud.google.com/sdk/gcloud/), and [gcloud app component](https://cloud.google.com/sdk/gcloud-app).
13+
2. Setup the gcloud tool.
14+
15+
```
16+
gcloud components update app
17+
gcloud auth login
18+
gcloud config set project <your-app-id>
19+
```
20+
You don't need a valid app-id to run locally, but will need a valid id to deploy below.
21+
22+
1. Clone this repo.
23+
24+
```
25+
git clone https://github.com/GoogleCloudPlatform/datastore-samples.git
26+
cd datastore-samples/python/ndb/transactions
27+
```
28+
1. Run this project locally from the command line.
29+
30+
```
31+
pip install -r requirements.txt -t lib/
32+
gcloud preview app run ./app.yaml
33+
```
34+
35+
1. Visit the application at [http://localhost:8080](http://localhost:8080).
36+
37+
## Deploying
38+
39+
1. Use the [Cloud Developer Console](https://console.developer.google.com) to create a project/app id. (App id and project id are identical)
40+
2. Configure gcloud with your app id.
41+
42+
```
43+
gcloud config set project <your-app-id>
44+
```
45+
1. Use the [Admin Console](https://appengine.google.com) to view data, queues, and other App Engine specific administration tasks.
46+
1. Use gcloud to deploy your app.
47+
48+
```
49+
pip install -r requirements.txt -t lib/
50+
gcloud preview app deploy ./app.yaml
51+
```
52+
53+
1. Congratulations! Your application is now live at your-app-id.appspot.com
54+
55+
## Contributing changes
56+
57+
* See [CONTRIBUTING.md](../../../CONTRIBUTING.md)
58+
59+
## Licensing
60+
61+
* See [LICENSE](../../../LICENSE)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# This file specifies your Python application's runtime configuration
2+
# including URL routing, versions, static file uploads, etc. See
3+
# https://developers.google.com/appengine/docs/python/config/appconfig
4+
# for details.
5+
6+
version: 1
7+
runtime: python27
8+
api_version: 1
9+
threadsafe: yes
10+
11+
# Handlers define how to route requests to your application.
12+
handlers:
13+
14+
# This handler tells app engine how to route requests to a WSGI application.
15+
# The script value is in the format <path.to.module>.<wsgi_application>
16+
# where <wsgi_application> is a WSGI application object.
17+
- url: .* # This regex directs all routes to main.app
18+
script: main.app
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"""
2+
`appengine_config.py` is automatically loaded when Google App Engine
3+
starts a new instance of your application. This runs before any
4+
WSGI applications specified in app.yaml are loaded.
5+
"""
6+
7+
from google.appengine.ext import vendor
8+
9+
# Third-party libraries are stored in "lib", vendoring will make
10+
# sure that they are importable by the application.
11+
vendor.add('lib')
8.15 KB
Binary file not shown.

0 commit comments

Comments
 (0)