-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeeds.py
196 lines (144 loc) · 5.28 KB
/
feeds.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import os
import simplejson
import pprint
import logging
from google.appengine.api import memcache, users
from google.appengine.ext import db, webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
from models import *
class RegionsFeed(webapp.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'application/json'
cacheKey = "regionFeed"
output = memcache.get(cacheKey)
if output is None:
regions = Region.gql("ORDER BY name")
finalRegions = []
for region in regions:
finalRegions.append({
"id": region.eveId,
"name": region.name
})
output = simplejson.dumps(finalRegions)
if not memcache.add(cacheKey, output, 3600):
logging.error("Storing regionFeed in memcache failed.")
self.response.out.write(output)
class ConstellationsFeed(webapp.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'application/json'
regionId = self.request.get('regionId')
cacheKey = "constellationFeed" + regionId
output = memcache.get(cacheKey)
if output is None:
query = Region.gql("WHERE eveId = :regionId",
regionId=int(regionId))
if query.count() == 0:
logging.error("Unknown region id " + regionId + " supplied");
return
region = query.get()
constellations = Constellation.gql("WHERE region = :region ORDER BY name",
region=region)
finalConstellations = []
for constellation in constellations:
finalConstellations.append({
"id": constellation.eveId,
"name": constellation.name,
"regionId": constellation.region.eveId
})
output = simplejson.dumps(finalConstellations)
if not memcache.add(cacheKey, output, 3600):
logging.error("Storing constellationFeed in memcache failed.")
self.response.out.write(output)
class SystemsFeed(webapp.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'application/json'
mapType = self.request.get('mapType')
if len(mapType) == 0:
self.response.out.write(simplejson.dumps('No map type supplied'))
return
includeAllSystems = self.request.get('all')
constellationId = self.request.get('constellationId')
cacheKey = "systemsFeed" + mapType + constellationId + includeAllSystems
output = memcache.get(cacheKey)
if output is None:
query = Constellation.gql("WHERE eveId = :constellationId",
constellationId=int(constellationId))
if query.count() == 0:
logging.error("Unknown constellation id " + constellationId + " supplied");
return
constellation = query.get()
systems = System.gql("WHERE constellation = :constellation ORDER BY name",
constellation=constellation)
finalSystems = []
for system in systems:
propertyName = mapType.lower() + 'Location'
if getattr(system, propertyName) is None:
if includeAllSystems == 'true':
finalSystems.append(system.getJsonObject())
else:
finalSystems.append(system.getJsonObject())
output = simplejson.dumps(finalSystems)
if not memcache.add(cacheKey, output, 3600):
logging.error("Storing systemsFeed in memcache failed.")
self.response.out.write(output)
class SystemSearch(webapp.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'application/json'
searchTerm = self.request.get('term')
mapType = self.request.get('mapType')
if len(mapType) == 0:
self.response.out.write(simplejson.dumps('No map type supplied'))
return
includeAllSystems = self.request.get('all')
cacheKey = 'systemSearch-' + searchTerm + mapType + includeAllSystems
output = memcache.get(cacheKey)
if output is None:
if len(searchTerm) == 0:
output = simplejson.dumps([]);
else:
query = System.gql('WHERE searchableName >= :1 AND searchableName < :2',
searchTerm.lower(),
searchTerm.lower() + u"\ufffd")
propertyName = mapType.lower() + 'Location'
searchResults = [];
for system in query:
if getattr(system, propertyName) is not None or includeAllSystems == 'true':
searchResults.append({
'label': system.name,
'value': system.name
});
output = simplejson.dumps(searchResults)
if not memcache.add(cacheKey, output, 3600):
logging.error("Storing system search '" + searchTerm + "' in memcache failed.")
self.response.out.write(output);
class SystemDetails(webapp.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'application/json'
systemName = self.request.get('name')
if len(systemName) == 0:
self.response.out.write(simplejson.dumps('No system name to lookup'))
return
cacheKey = "systemDetails" + systemName
output = memcache.get(cacheKey)
if output is None:
systems = System.gql("WHERE searchableName = :1",
systemName.lower())
system = systems.get()
output = simplejson.dumps(system.getJsonObject())
if not memcache.add(cacheKey, output, 3600):
logging.error("Storing systemsFeed in memcache failed.")
self.response.out.write(output)
application = webapp.WSGIApplication(
[
('/feeds/regions', RegionsFeed),
('/feeds/constellations', ConstellationsFeed),
('/feeds/systems', SystemsFeed),
('/feeds/systemSearch', SystemSearch),
('/feeds/systemDetails', SystemDetails)
],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()