|
| 1 | +# Copyright 2016 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 notestore_imports] |
| 16 | +from google.appengine.ext import ndb |
| 17 | +from google.appengine.ext.ndb import msgprop |
| 18 | +# [END notestore_imports] |
| 19 | +from protorpc import messages |
| 20 | + |
| 21 | + |
| 22 | +class Account(ndb.Model): |
| 23 | + username = ndb.StringProperty() |
| 24 | + userid = ndb.IntegerProperty() |
| 25 | + email = ndb.StringProperty() |
| 26 | + |
| 27 | + |
| 28 | +class Employee(ndb.Model): |
| 29 | + full_name = ndb.StringProperty('n') |
| 30 | + retirement_age = ndb.IntegerProperty('r') |
| 31 | + |
| 32 | + |
| 33 | +class Article(ndb.Model): |
| 34 | + title = ndb.StringProperty() |
| 35 | + stars = ndb.IntegerProperty() |
| 36 | + tags = ndb.StringProperty(repeated=True) |
| 37 | + |
| 38 | + |
| 39 | +def create_article(): |
| 40 | + article = Article( |
| 41 | + title='Python versus Ruby', |
| 42 | + stars=3, |
| 43 | + tags=['python', 'ruby']) |
| 44 | + article.put() |
| 45 | + return article |
| 46 | + |
| 47 | + |
| 48 | +class Address(ndb.Model): |
| 49 | + type = ndb.StringProperty() # E.g., 'home', 'work' |
| 50 | + street = ndb.StringProperty() |
| 51 | + city = ndb.StringProperty() |
| 52 | + |
| 53 | + |
| 54 | +class Contact(ndb.Model): |
| 55 | + name = ndb.StringProperty() |
| 56 | + addresses = ndb.StructuredProperty(Address, repeated=True) |
| 57 | + |
| 58 | + |
| 59 | +class ContactWithLocalStructuredProperty(ndb.Model): |
| 60 | + name = ndb.StringProperty() |
| 61 | + addresses = ndb.LocalStructuredProperty(Address, repeated=True) |
| 62 | + |
| 63 | + |
| 64 | +def create_contact(): |
| 65 | + guido = Contact( |
| 66 | + name='Guido', |
| 67 | + addresses=[ |
| 68 | + Address( |
| 69 | + type='home', |
| 70 | + city='Amsterdam'), |
| 71 | + Address( |
| 72 | + type='work', |
| 73 | + street='Spear St', |
| 74 | + city='SF')]) |
| 75 | + |
| 76 | + guido.put() |
| 77 | + return guido |
| 78 | + |
| 79 | + |
| 80 | +def create_contact_with_local_structured_property(): |
| 81 | + guido = ContactWithLocalStructuredProperty( |
| 82 | + name='Guido', |
| 83 | + addresses=[ |
| 84 | + Address( |
| 85 | + type='home', |
| 86 | + city='Amsterdam'), |
| 87 | + Address( |
| 88 | + type='work', |
| 89 | + street='Spear St', |
| 90 | + city='SF')]) |
| 91 | + |
| 92 | + guido.put() |
| 93 | + return guido |
| 94 | + |
| 95 | + |
| 96 | +class SomeEntity(ndb.Model): |
| 97 | + name = ndb.StringProperty() |
| 98 | + name_lower = ndb.ComputedProperty(lambda self: self.name.lower()) |
| 99 | + |
| 100 | + |
| 101 | +def create_some_entity(): |
| 102 | + entity = SomeEntity(name='Nick') |
| 103 | + entity.put() |
| 104 | + return entity |
| 105 | + |
| 106 | + |
| 107 | +class Note(messages.Message): |
| 108 | + text = messages.StringField(1, required=True) |
| 109 | + when = messages.IntegerField(2) |
| 110 | + |
| 111 | + |
| 112 | +class NoteStore(ndb.Model): |
| 113 | + note = msgprop.MessageProperty(Note, indexed_fields=['when']) |
| 114 | + name = ndb.StringProperty() |
| 115 | + |
| 116 | + |
| 117 | +def create_note_store(): |
| 118 | + my_note = Note(text='Excellent note', when=50) |
| 119 | + |
| 120 | + ns = NoteStore(note=my_note, name='excellent') |
| 121 | + key = ns.put() |
| 122 | + |
| 123 | + new_notes = NoteStore.query(NoteStore.note.when >= 10).fetch() |
| 124 | + return new_notes, key |
| 125 | + |
| 126 | + |
| 127 | +class Notebook(messages.Message): |
| 128 | + notes = messages.MessageField(Note, 1, repeated=True) |
| 129 | + |
| 130 | + |
| 131 | +class SignedStorableNotebook(ndb.Model): |
| 132 | + author = ndb.StringProperty() |
| 133 | + nb = msgprop.MessageProperty( |
| 134 | + Notebook, indexed_fields=['notes.text', 'notes.when']) |
| 135 | + |
| 136 | + |
| 137 | +class Color(messages.Enum): |
| 138 | + RED = 620 |
| 139 | + GREEN = 495 |
| 140 | + BLUE = 450 |
| 141 | + |
| 142 | + |
| 143 | +class Part(ndb.Model): |
| 144 | + name = ndb.StringProperty() |
| 145 | + color = msgprop.EnumProperty(Color, required=True) |
| 146 | + |
| 147 | + |
| 148 | +def print_part(): |
| 149 | + p1 = Part(name='foo', color=Color.RED) |
| 150 | + print p1.color # prints "RED" |
0 commit comments