Support the host property and a /logout URL on WSGI servers.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@965145 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
c8dd567ff6
commit
c59c7ef6db
8 changed files with 53 additions and 10 deletions
|
|
@ -86,6 +86,9 @@ def result(e, r, st, h = (), b = None):
|
|||
r("200 OK", list(h + (("Etag", md), ("Expires", "Tue, 01 Jan 1980 00:00:00 GMT"))))
|
||||
return b
|
||||
|
||||
if st == 301:
|
||||
r("301 Moved Permanently", list(h))
|
||||
|
||||
return failure(e, r, 500)
|
||||
|
||||
# Return an HTTP failure result
|
||||
|
|
@ -115,6 +118,14 @@ def postArgs(a):
|
|||
l = car(a);
|
||||
return cons(l, postArgs(cdr(a)))
|
||||
|
||||
# Return the URL of the logout page
|
||||
def logout(ruri):
|
||||
try:
|
||||
from google.appengine.api import users
|
||||
return users.create_logout_url(ruri)
|
||||
except:
|
||||
return None
|
||||
|
||||
# WSGI application function
|
||||
def application(e, r):
|
||||
m = requestMethod(e)
|
||||
|
|
@ -132,6 +143,14 @@ def application(e, r):
|
|||
return fileresult(e, r, "application/x-javascript", fpath)
|
||||
if fpath.endswith(".png"):
|
||||
return fileresult(e, r, "image/png", fpath)
|
||||
if fpath == "/":
|
||||
return result(e, r, 301, (("Location", "/index.html"),))
|
||||
|
||||
# Logout
|
||||
if fpath == "/logout":
|
||||
redir = logout("/")
|
||||
if redir:
|
||||
return result(e, r, 301, (("Location", redir),))
|
||||
|
||||
# Find the requested component
|
||||
path = tokens(fpath)
|
||||
|
|
|
|||
|
|
@ -132,9 +132,11 @@ def jsonResultValue(s):
|
|||
|
||||
# Return a portable function name from a JSON-RPC function name
|
||||
def funcName(f):
|
||||
if len(f) > 7 and f.find("system.") == 0:
|
||||
if f.startswith("."):
|
||||
return f[1:]
|
||||
if f.startswith("system."):
|
||||
return f[7:]
|
||||
if len(f) > 8 and f.find("Service.") == 0:
|
||||
if f.startswith("Service."):
|
||||
return f[8:]
|
||||
return f
|
||||
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@
|
|||
|
||||
from xml.etree.cElementTree import iterparse
|
||||
from sys import stderr
|
||||
from os import environ
|
||||
from util import *
|
||||
from httputil import *
|
||||
|
||||
|
|
@ -186,9 +187,11 @@ def evalReference(r, comps):
|
|||
return nameToComponent(t, comps)
|
||||
|
||||
# Evaluate a property, return a lambda function returning the property
|
||||
# value. The user and email properties are configured with the values
|
||||
# from the HTTP request, if any
|
||||
# value. The host, user and email properties are configured with the
|
||||
# values from the HTTP request, if any
|
||||
def evalProperty(p):
|
||||
if car(p) == "host":
|
||||
return lambda: hostProperty(cadr(p), environ)
|
||||
if car(p) == "user":
|
||||
return lambda: userProperty(cadr(p))
|
||||
if car(p) == "email":
|
||||
|
|
@ -206,6 +209,9 @@ def userProperty(v):
|
|||
user = currentUser()
|
||||
return user.user_id() if user else v
|
||||
|
||||
def hostProperty(v, e):
|
||||
return e.get("HTTP_HOST", e.get("SERVER_NAME", v)).split(":")[0]
|
||||
|
||||
def emailProperty(v):
|
||||
user = currentUser()
|
||||
return user.email() if user else v
|
||||
|
|
|
|||
|
|
@ -52,6 +52,7 @@
|
|||
<reference name="cache">
|
||||
<t:binding.http uri="https://sca-store-backend.appspot.com/cache"/>
|
||||
</reference>
|
||||
<property name="host">localhost</property>
|
||||
<property name="email">anonymous@example.com</property>
|
||||
</component>
|
||||
|
||||
|
|
|
|||
|
|
@ -50,6 +50,7 @@
|
|||
<t:binding.jsonrpc uri="total"/>
|
||||
</service>
|
||||
<reference name="cache" target="Cache"/>
|
||||
<property name="host">localhost</property>
|
||||
<property name="email">anonymous@example.com</property>
|
||||
</component>
|
||||
|
||||
|
|
|
|||
|
|
@ -50,6 +50,7 @@
|
|||
<t:binding.jsonrpc uri="total"/>
|
||||
</service>
|
||||
<reference name="cache" target="Cache"/>
|
||||
<property name="host">localhost</property>
|
||||
<property name="email">anonymous@example.com</property>
|
||||
</component>
|
||||
|
||||
|
|
|
|||
|
|
@ -51,6 +51,14 @@ function catalog_getcatalogResponse(items, exception) {
|
|||
catalogItems = items;
|
||||
}
|
||||
|
||||
function shoppingCart_gethostResponse(host, exception) {
|
||||
if (exception) {
|
||||
alert(exception.message);
|
||||
return;
|
||||
}
|
||||
document.getElementById('host').innerHTML = host;
|
||||
}
|
||||
|
||||
function shoppingCart_getemailResponse(email, exception) {
|
||||
if (exception) {
|
||||
alert(exception.message);
|
||||
|
|
@ -131,6 +139,7 @@ function init() {
|
|||
try {
|
||||
catalog.apply("getcatalog", catalog_getcatalogResponse);
|
||||
shoppingCart.apply("getemail", shoppingCart_getemailResponse);
|
||||
shoppingCart.apply("gethost", shoppingCart_gethostResponse);
|
||||
shoppingCart.get("", shoppingCart_getResponse);
|
||||
} catch(e){
|
||||
alert(e);
|
||||
|
|
@ -141,7 +150,7 @@ function init() {
|
|||
|
||||
<body onload="init()">
|
||||
<h1>Store</h1>
|
||||
<p>You're signed in as: <span id="email"></span><br/><a href="/_ah/login?continue=/store.html&action=Logout">Sign out</a></p>
|
||||
<p>Welcome to: <span id="host"></span>, you're signed in as: <span id="email"></span><br/><a href="/logout">Sign out</a></p>
|
||||
<div id="store">
|
||||
<h2>Catalog</h2>
|
||||
<form name="catalogForm">
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ def getcart(id, cache):
|
|||
return cart
|
||||
|
||||
# Post a new item to the cart, create a new cart if necessary
|
||||
def post(collection, item, cache, email):
|
||||
def post(collection, item, cache, host, email):
|
||||
id = str(uuid.uuid1())
|
||||
cart = ((item[0], id, item[2]),) + getcart(cartId, cache)
|
||||
cache("put", (cartId,), cart)
|
||||
|
|
@ -46,13 +46,13 @@ def find(id, cart):
|
|||
return find(id, cart[1:])
|
||||
|
||||
# Get items from the cart
|
||||
def get(id, cache, email):
|
||||
def get(id, cache, host, email):
|
||||
if id == ():
|
||||
return ("Your Cart", cartId) + getcart(cartId, cache)
|
||||
return find(id[0], getcart(cartId, cache))
|
||||
|
||||
# Delete items from the cart
|
||||
def delete(id, cache, email):
|
||||
def delete(id, cache, host, email):
|
||||
if id == ():
|
||||
return cache("delete", (cartId,))
|
||||
return True
|
||||
|
|
@ -68,11 +68,15 @@ def sum(items):
|
|||
return price(items[0]) + sum(items[1:])
|
||||
|
||||
# Return the total price of the items in the cart
|
||||
def gettotal(cache, email):
|
||||
def gettotal(cache, host, email):
|
||||
cart = getcart(cartId, cache)
|
||||
return sum(cart)
|
||||
|
||||
# Return the email of the cart owner
|
||||
def getemail(cache, email):
|
||||
def getemail(cache, host, email):
|
||||
return email()
|
||||
|
||||
# Return the host that the app is running on
|
||||
def gethost(cache, host, email):
|
||||
return host()
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue