forked from BobRupholdt/Sample-Python-Application
-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.py
164 lines (138 loc) · 5.46 KB
/
events.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python
import cgi
import logging
import oauth2
from google.appengine.dist import use_library
use_library('django', '1.2')
from models import Event
from models import CompanySubscription
from models import User
from marshall import EventXml
from google.appengine.api.datastore_errors import BadArgumentError
from xml.dom import minidom
consumer_key = 'sample-python-application-480'
consumer_secret = 'fakefakefake'
eventUrlTemplate = "https://www.appdirect.com/rest/api/events/%s";
errorTemplate = """
<result>
<success>false</success>
<errorCode>%s</errorCode>
<message>%s</message>
</result>
"""
successMessage = """<result><success>true</success></result>"""
def FetchEvent(token):
consumer = oauth2.Consumer(consumer_key, consumer_secret)
client = oauth2.Client(consumer)
eventUrl = eventUrlTemplate % token
resp, content = client.request(eventUrl)
event = Event()
event.token = token
event.status = int(resp['status'])
if event.status == 200:
xmlDocument = minidom.parseString(content)
eventXml = EventXml(xmlDocument)
event.result = cgi.escape(xmlDocument.toprettyxml())
event.put()
return HandleEvent(eventXml)
else:
message = "HTTP response %d" % event.status
logging.error(message)
event.put()
return errorTemplate % ( "UNKNOWN_ERROR", message)
def HandleEvent(eventXml):
logging.info("Recevied event type %s" % eventXml.eventType)
if eventXml.eventType == "SUBSCRIPTION_ORDER":
return CreateOrder(eventXml)
elif eventXml.eventType == "SUBSCRIPTION_CHANGE":
return ChangeOrder(eventXml)
elif eventXml.eventType == "SUBSCRIPTION_CANCEL":
return CancelOrder(eventXml)
elif eventXml.eventType == "USER_ASSIGNMENT":
return AssignUser(eventXml)
elif eventXml.eventType == "USER_UNASSIGNMENT":
return UnassignUser(eventXml)
else:
message = "Event type %s is not configured" % eventXml.eventType
return errorTemplate % ( "CONFIGURATION_ERROR", message)
return successMessage
# Datatstore utility methods
def GetSubscription(accountIdentifier):
subscription = None
try:
subscription = CompanySubscription.get_by_id(int(accountIdentifier))
logging.info("Found subscription: %s", accountIdentifier)
except ValueError:
logging.error("Bad account identifier %s" % accountIdentifier)
except BadArgumentError:
logging.error("Bad account identifier %s" % accountIdentifier)
return subscription
def GetUsers(subscription, openid = None):
userQuery = User.all()
userQuery.filter('subscription =', subscription)
if (openid != None):
userQuery.filter('openid = ', openid)
return userQuery
# Event handling methods
def CreateOrder(eventXml):
logging.info("Read %s %s %s" % (eventXml.payload.company.name,\
eventXml.payload.company.website,\
eventXml.payload.order.edition))
companySubscription = eventXml.payload.CreateSubscription()
companySubscription.put()
creator = eventXml.creator.CreateUserModel(companySubscription)
creator.put()
return "<result><success>true</success><accountIdentifier>%s</accountIdentifier></result>" % companySubscription.key().id()
def ChangeOrder(eventXml):
accountId = eventXml.payload.account.accountIdentifier
subscription = GetSubscription(accountId)
if subscription == None:
message = "Account %s not found" % accountIdentifier
logging.error(message)
return errorTemplate % ("ACCOUNT_NOT_FOUND", message)
# Update the edition code
subscription.edition = eventXml.payload.order.edition
subscription.put()
return successMessage
def CancelOrder(eventXml):
accountId = eventXml.payload.account.accountIdentifier
subscription = GetSubscription(accountId)
if subscription == None:
message = "Account %s not found" % accountId
logging.error(message)
return errorTemplate % ("ACCOUNT_NOT_FOUND", message)
# Delete users associated with this subscription
userQuery = GetUsers(subscription)
for user in userQuery:
logging.info("Removing user: %s " % user.email)
user.delete()
subscription.delete()
return successMessage
def AssignUser(eventXml):
accountId = eventXml.payload.account.accountIdentifier
subscription = GetSubscription(accountId)
if subscription == None:
message = "Account %s not found" % accountIdentifier
logging.error(message)
return errorTemplate % ("ACCOUNT_NOT_FOUND", message)
user = eventXml.payload.user.CreateUserModel(subscription)
logging.info("Assigning user %s to account %s" % (user.email, accountId))
user.put()
return successMessage
def UnassignUser(eventXml):
accountId = eventXml.payload.account.accountIdentifier
subscription = GetSubscription(accountId)
if subscription == None:
message = "Account %s not found" % accountIdentifier
logging.error(message)
return errorTemplate % ("ACCOUNT_NOT_FOUND", message)
openid = eventXml.payload.user.openid
users = GetUsers(subscription, openid)
if users.count() == 0:
logging.error("User not found: %s" % openid)
return errorTemplate % ("USER_NOT_FOUND", openid)
user = users.get()
logging.info("Unassigning user %s from account %s" %\
(user.email, accountId))
user.delete()
return successMessage