Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

RE: Issue #117 #162

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions examples/structured_property/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
runtime: python27
api_version: 1
threadsafe: true

skip_files:
- ^(.*/)?.*~$
- ^(.*/)?.*\.py[co]$
- ^(.*/)?\..*$
- ^(.*/)?test$
- ^(.*/)?examples$

libraries:
# Needed for endpoints/users_id_token.py.
- name: pycrypto
version: 2.6
- name: ssl
version: 2.7.11

handlers:
- url: /_ah/api/.*/product/create
script: main.application
# login: admin
- url: /_ah/api/.*/products
script: main.application
10 changes: 10 additions & 0 deletions examples/structured_property/appengine_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import os
import sys

from google.appengine.ext import vendor

ENDPOINTS_PROJECT_DIR = os.path.join(os.path.dirname(__file__),
'endpoints-proto-datastore')
sys.path.append(ENDPOINTS_PROJECT_DIR)

vendor.add('lib')
1 change: 1 addition & 0 deletions examples/structured_property/endpoints_proto_datastore
80 changes: 80 additions & 0 deletions examples/structured_property/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import endpoints

from google.appengine.ext import ndb
from protorpc import remote

from endpoints_proto_datastore.ndb import EndpointsModel \
, EndpointsAliasProperty
import re
import logging
_logger = logging.getLogger( __name__)


class Interests( EndpointsModel):
science = ndb.BooleanProperty()
technology = ndb.BooleanProperty()
math = ndb.BooleanProperty()
politics = ndb.BooleanProperty()


_messages = []
def validate_edu( prop, value):
if not re.match( r'\w+school|\w+college|b[sa]|masters|phd', value):
_messages.append( 'The value for \'education\' is invalid.')
return None # changes nothing.
def validate_gender( prop, value):
if not re.match( 'm|f|mf|lgbtq', value):
_messages.append( 'The value for \'gender\' is invalid.')
return None # changes nothing.
class Demographic( EndpointsModel):
gender = ndb.StringProperty( validator=validate_gender) # was: choices=( 'm', 'f', 'mf', 'lgbtq')
education = ndb.StringProperty( validator=validate_edu)
interests = ndb.LocalStructuredProperty( Interests)


class Product( EndpointsModel):
title = ndb.StringProperty( required=True)
creation_date = ndb.DateTimeProperty( auto_now_add=True)
target_demographic = ndb.StructuredProperty( Demographic)

def SetId( self, value):
if not isinstance( value, basestring):
raise TypeError( '\'title\' must be of type String.')
key = ndb.Key( Product, value)
self.UpdateFromKey( key)

@EndpointsAliasProperty( setter=SetId)
def id( self):
if self.key is not None:
return self.key.string_id()

@EndpointsAliasProperty( repeated=True)
def messages( self):
return _messages or [ 'Okay']


@endpoints.api( name='products'
, version='v1'
, description='Products API')
class ProductsAPI( remote.Service):
_logger.info( 'Entering ProductsAPI')

@Product.query_method( path='products'
, name='products.list')
def ProductsList( self, query):
_logger.info( 'Entering ProductsList.')
return query

@Product.method( path='product/create'
, http_method='POST'
, request_fields=( 'title'
, 'target_demographic')
, response_fields=( 'messages', )
, name='product.create')
def ProductCreate( self, product):
_logger.info( 'Entering ProductCreate')
product.id = product.title
product.put()
return product

application = endpoints.api_server( [ProductsAPI], restricted=False)
12 changes: 12 additions & 0 deletions examples/structured_property/product.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"title": "My First Product",
"target_demographic": {
"gender": "z",
"education" : "some",
"interests" : {
"technology" : true,
"math" : false,
"politics" : true
}
}
}