-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·210 lines (182 loc) · 6.18 KB
/
main.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/usr/bin/env python
import logging
import os
from google.appengine.ext import db
from google.appengine.api import images
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp import util
kTemplatesDir = os.path.join(os.path.dirname(__file__), 'templates')
# TODO(jgessner): move these utility functions into their own little class.
def getTemplatePath(template):
return os.path.join(kTemplatesDir, template)
class Maker(db.Model):
user = db.UserProperty()
first_name = db.StringProperty()
last_name = db.StringProperty()
image = db.BlobProperty()
tags = db.StringListProperty()
badges = db.ListProperty(db.Key)
def getImgSrc(self):
if self.image:
return '<img src="/pic?maker=%s" width=200 height=200 />' % self.key()
else:
return ""
def getBadges(self):
return MeritBadge.get(self.badges)
class MeritBadge(db.Model):
name = db.StringProperty()
description = db.StringProperty()
requirements = db.StringProperty()
image = db.BlobProperty()
granters = db.ListProperty(db.Key)
def getImgSrc(self):
if self.image:
return '<img src="/pic?badge=%s" width=200 height=200 />' % self.key()
else:
return ""
class Index(webapp.RequestHandler):
def get(self):
template_values = {
}
self.response.out.write(template.render(getTemplatePath('index.html'),
template_values))
class NewMaker(webapp.RequestHandler):
def get(self):
badges = MeritBadge.all().fetch(100)
template_values = {
'badges': badges,
}
self.response.out.write(template.render(getTemplatePath('new_maker.html'),
template_values))
def post(self):
maker = Maker()
maker.first_name = self.request.get('first_name')
maker.last_name = self.request.get('last_name')
if self.request.get("image"):
maker.image = images.resize(self.request.get("image"), 200, 200)
tags = self.request.get("tags").split(",")
for tag in tags:
maker.tags.append(tag)
badges = self.request.str_params.getall("badges")
for badge in badges:
logging.info("Badge is %s" % badge)
meritBadge = MeritBadge.get(badge)
if meritBadge:
logging.info("Found a badge named %s" % meritBadge.name)
maker.badges.append(meritBadge.key())
maker.put()
self.redirect('/')
class NewBadge(webapp.RequestHandler):
def get(self):
template_values = {}
self.response.out.write(template.render(getTemplatePath('new_badge.html'),
template_values))
def post(self):
badge = MeritBadge.all()
badge.name = self.request.get('name')
badge.description = self.request.get('description')
badge.requirements = self.request.get('requirments')
if self.request.get("image"):
badge.image = images.resize(self.request.get("image"), 200, 200)
badge.put()
self.redirect('/badges')
class Makers(webapp.RequestHandler):
def get(self):
makers_query = Maker.all()
makers = makers_query.fetch(100)
template_values = {
'makers': makers,
'tag': self.request.get('tag')
}
self.response.out.write(template.render(getTemplatePath('makers.html'),
template_values))
class Pic(webapp.RequestHandler):
def detect_mime_type(self, image):
if image[1:4] == 'PNG': return 'image/png'
if image[0:3] == 'GIF': return 'image/gif'
if image[6:10] == 'JFIF': return 'image/jpeg'
return None
def get(self):
if self.request.get("maker"):
maker = Maker.get(self.request.get('maker'))
if maker.image:
filetype = self.detect_mime_type(maker.image)
self.response.headers['Content-Type'] = filetype
self.response.out.write(maker.image)
else:
self.response.out.write('no image found')
elif self.request.get("badge"):
badge = MeritBadge.get(self.request.get('badge'))
if badge.image:
filetype = self.detect_mime_type(badge.image)
self.response.headers['Content-Type'] = filetype
self.response.out.write(badge.image)
else:
self.response.out.write('no image found')
class Skills(webapp.RequestHandler):
def get(self):
skills = {}
makers_query = Maker.all()
makers = makers_query.fetch(100)
for maker in makers:
for skill in maker.tags:
if not skills.has_key(skill):
skills[skill] = 0
skills[skill] += 1
template_values = {
'skills': skills
}
self.response.out.write(template.render(getTemplatePath('skills.html'),
template_values))
class Badges(webapp.RequestHandler):
def get(self):
badges_query = MeritBadge.all()
badges = badges_query.fetch(100)
template_values = {
'badges': badges,
}
self.response.out.write(template.render(getTemplatePath('badges.html'),
template_values))
class Search(webapp.RequestHandler):
def get(self):
have_results = False
makers = []
skill = self.request.get("skill")
badge = self.request.get("badge")
meritbadge = MeritBadge()
if skill:
have_results = True
makers_query = Maker.all()
makers_query.filter("tags", skill)
makers = makers_query.fetch(100)
elif badge:
have_results = True
makers_query = Maker.all()
makers_query.filter("badges", db.Key(badge))
meritbadge = MeritBadge.get(badge)
makers = makers_query.fetch(100)
template_values = {
'makers': makers,
'skill': skill,
'badge': badge,
'badge_name' : meritbadge.name if badge else '',
'have_results': have_results,
}
self.response.out.write(template.render(getTemplatePath('search.html'),
template_values))
def main():
paths = [
('/', Index),
('/badges', Badges),
('/new_badge', NewBadge),
('/new_maker', NewMaker),
('/makers', Makers),
('/pic', Pic),
('/skills', Skills),
('/search', Search),
]
application = webapp.WSGIApplication(paths, debug=True)
util.run_wsgi_app(application)
if __name__ == '__main__':
main()