-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmodel.py
468 lines (361 loc) · 14.4 KB
/
model.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" Database ``Model`` classes.
"""
import itertools
import hashlib
import logging
import urllib
import urllib2
from cgi import escape
from google.appengine.api import users
from google.appengine.ext import blobstore
from google.appengine.ext import db
from weblayer.utils import json_decode
from utils import get_exchange_rate_to_gbp
from utils import render_number_with_commas
CAMPAIGNS = [(
'sketchup2',
10000,
'SketchUp',
'Plugin',
'SketchUp Plugin',
'Thanks for funding the most recent version of the WikiHouse SketchUp plugin! Your continued\
support will help us make it easy for you to customise designs to your needs.'
), (
'hardware',
20000,
'Hardware',
'Full House',
'Hardware',
'A complete test module tiny-house prototype, with footings, skin and\
insulation. Sensors to monitor performance. Documented and shared so\
anyone can build it for themselves.'
), (
'platform',
20000,
'WikiHouse',
'Platform',
'Platform',
'Help create the Wikipedia of things by funding an improved community\
sharing site, with collaboration and easy to use documentation tools\
making it easy to download and document designs.'
)
]
CAMPAIGN_KEYS = {}
for _item in CAMPAIGNS:
CAMPAIGN_KEYS[_item[0]] = db.Key.from_path('Campaign', _item[0])
del _item
class User(db.Model):
""" Encapsulate a `User`, wrap the `google.appengine.api.users.User` with
some additional properties.
"""
v = db.IntegerProperty(default=1) # version
c = db.DateTimeProperty(auto_now_add=True) # created
m = db.DateTimeProperty(auto_now=True) # modified
google_user = db.UserProperty()
google_user_id = db.StringProperty()
avatar = db.StringProperty()
has_real_avatar = db.BooleanProperty(default=False)
@property
def id(self):
return self.key().id()
@property
def nickname(self):
return self.google_user.nickname()
@property
def email(self):
return self.google_user.email()
@classmethod
def get_by_user_id(cls, user_id):
user_id = str(user_id)
query = cls.all().filter('google_user_id =', user_id)
return query.get()
@classmethod
def get_all(cls):
return cls.all().order('-m')
@classmethod
def get_with_real_avatars(cls, limit=40):
query = cls.all().filter('has_real_avatar =', True)
return query.order('-m').fetch(limit)
@property
def designs(self):
return Design.all_listings(filter_by=[("user =", self.key())])
def get_gravatar(self, size=80):
""" Get URL to gravatar image, using `d=mm` to fallback on the
"mystery man" image if no gravatar is registered.
"""
hash_ = hashlib.md5(self.email.strip().lower()).hexdigest()
return 'https://secure.gravatar.com/avatar/%s?s=%s&d=mm' % (hash_, size)
def set_avatar(self, size=80):
""" Try and scrape the profile image from Google+ using YQL, falling back
on `Gravatar`_ if this fails.
The scrape hack relies on either `https://profiles.google.com/${username}`
or `http://gplus.to/${username}` redirecting to the right Google+ url.
_`Gravatar`: http://en.gravatar.com/site/implement/images/
"""
avatar = None
has_real_avatar = False
# If we can get a google username from the email (quite likely) then try
# scraping the profile image from Google+ (nasty).
google_username = None
parts = self.nickname.split('@')
if len(parts) == 1:
google_username = self.nickname
elif parts[1] == 'gmail.com' or parts[1] == 'googlemail.com':
google_username = parts[0]
if google_username is not None:
urls = [
'https://profiles.google.com/%s' % google_username,
'http://gplus.to/%s' % google_username
]
for url in urls:
# Build YQL query.
table = 'http://yqlblog.net/samples/data.html.cssselect.xml'
what = 'data.html.cssselect'
query = 'use "%s" as %s; select * from %s where url="%s" and css=".photo"' % (
table,
what,
what,
url
)
# Make YQL query.
sock = urllib.urlopen(
'https://query.yahooapis.com/v1/public/yql',
data=urllib.urlencode({'q': query, 'format': 'json'})
)
text = sock.read()
sock.close()
# Parse the response.
data = json_decode(text)
results = data['query']['results']['results']
if results is not None:
src = results['img']['src']
avatar = src.replace('?sz=200', '?sz=%s' % size)
has_real_avatar = True
break
# Fall back on Gravatar.
if avatar is None:
avatar = self.get_gravatar(size=size)
# Calling the thing with `d=404` to determine whether the avatar is real
# or not.
try:
sock = urllib2.urlopen(avatar.replace('&d=mm', '&d=404'))
except urllib2.URLError, e:
has_real_avatar = False
else:
has_real_avatar = True
# Set `self.avatar`.
self.avatar = avatar
self.has_real_avatar = has_real_avatar
class Series(db.Model):
""" A WikiHouse standard design series.
"""
v = db.IntegerProperty(default=1) # version
c = db.DateTimeProperty(auto_now_add=True) # created
m = db.DateTimeProperty(auto_now=True) # modified
order = db.IntegerProperty()
title = db.StringProperty()
description = db.TextProperty(default='')
@classmethod
def get_all(cls):
return cls.all().order('order')
@property
def designs(self):
return Design.all_listings(filter_by=[("series =", self.key())])
class Design(db.Model):
""" A design in the WikiHouse library.
"""
v = db.IntegerProperty(default=1) # version
c = db.DateTimeProperty(auto_now_add=True) # created
m = db.DateTimeProperty(auto_now=True) # modified
deleted = db.BooleanProperty(required=True, default=False)
component = db.BooleanProperty(required=True, default=False)
title = db.StringProperty(required=True)
description = db.TextProperty(required=True)
url = db.LinkProperty()
series = db.ListProperty(db.Key)
user = db.ReferenceProperty(User)
google_user_id = db.StringProperty()
country = db.StringProperty() # X-AppEngine-Country
grid = db.StringProperty(
choices=[
u'450mm',
u'550mm',
u'600mm',
u'900mm',
u'other'
],
default=u'550mm'
)
status = db.StringProperty(
choices=[
u'pending',
u'approved',
u'rejected'
],
default=u'pending'
)
verification = db.StringProperty(
choices=[
u'unverified',
u'verified',
u'built'
],
default=u'unverified'
)
notes = db.TextProperty()
sketchup_version = db.StringProperty()
plugin_version = db.StringProperty()
model = blobstore.BlobReferenceProperty()
model_preview = blobstore.BlobReferenceProperty()
model_preview_reverse = blobstore.BlobReferenceProperty()
model_preview_serving_url = db.StringProperty()
model_preview_reverse_serving_url = db.StringProperty()
sheets = blobstore.BlobReferenceProperty()
sheets_preview = blobstore.BlobReferenceProperty()
embeddable_sheets_preview = blobstore.BlobReferenceProperty()
@classmethod
def all_listings(cls, filter_by=None, limit=99):
""" Return all designs that are either approved or by the current user.
"""
google_user = users.get_current_user()
google_user_id = google_user and str(google_user.user_id()) or ''
# Get the approved designs.
query = cls.all().order('-m').filter("deleted =", False)
if filter_by is not None:
for k, v in filter_by:
query.filter(k, v)
approved = query.filter("status =", u'approved')
approved_results = approved.fetch(limit)
# Get the user's designs.
query = cls.all().order('-m').filter("deleted =", False)
if filter_by is not None:
for k, v in filter_by:
query.filter(k, v)
owned_results = []
if google_user is not None:
owned = query.filter("google_user_id =", google_user_id)
owned_results = owned.fetch(limit)
# Merge the two.
if len(owned_results) == 0:
results = approved_results
else:
chained = itertools.chain(approved_results, owned_results)
results = dict((str(item.key()), item) for item in chained).values()
# Sorting by modified date.
results = sorted(results, key=lambda item: item.m)
results.reverse()
return results[:limit]
class Quote(db.Model):
""" A press quote, uses `org` as key name.
"""
v = db.IntegerProperty(default=1) # version
c = db.DateTimeProperty(auto_now_add=True) # created
m = db.DateTimeProperty(auto_now=True) # modified
name = db.StringProperty()
content = db.TextProperty(default='')
href = db.LinkProperty()
@classmethod
def get_all(cls):
return cls.all().order('m')
class Avatars(db.Model):
"""
"""
twitter_followers = db.StringListProperty(indexed=False)
disqus_commenters = db.StringListProperty(indexed=False)
class Campaign(db.Model):
"""Appengine model class encapsultating a fundable campaign.
The key is the campaign id.
"""
v = db.IntegerProperty(default=0) # version
c = db.DateTimeProperty(auto_now_add=True) # created
m = db.DateTimeProperty(auto_now=True) # modified
funder_count = db.IntegerProperty(default=0)
total_gbp = db.IntegerProperty(default=0)
total_eur = db.IntegerProperty(default=0)
total_usd = db.IntegerProperty(default=0)
@classmethod
def get_campaign_items(cls):
"""Return a list of campaign data where the dict keys are the campaign
instance key names and the data is:
[
{
'category': 'software',
'title': 'Plugin',
'description': '...',
'num_backers': 32,
'percentage_raised': 50, # always an int
'total_raised': 5000,
'target': 10000
},
...
]
"""
# We build an items list as return value.
items = []
# Get the campaigns from the db -- creating them if they
# don't exist.
entities = {}
def get_or_insert(campaign_id):
"""We do this manually rather than use Model.get_or_insert
because otherwise we get keys generated that DONT MATCH
THE ``CAMPAIGN_KEYS`` CREATED AT MODULE IMPORT TIME.
"""
campaign_key = CAMPAIGN_KEYS[campaign_id]
campaign = Campaign.get(campaign_key)
if campaign is None:
campaign = Campaign(key=campaign_key)
campaign.put()
return campaign
for campaign_id in CAMPAIGN_KEYS:
campaign = db.run_in_transaction(get_or_insert, campaign_id)
entities[campaign_id] = campaign
# Iterate through the campaigns, building the data for the page.
for campaign in CAMPAIGNS:
campaign_id = campaign[0]
ent = entities[campaign_id]
target = campaign[1]
total = ent.total_gbp
if ent.total_eur:
total += (ent.total_eur * get_exchange_rate_to_gbp('EUR'))
if ent.total_usd:
total += (ent.total_usd * get_exchange_rate_to_gbp('USD'))
raised = int(total/100)
if raised >= target:
pct = 100
else:
pct = (raised * 100) / target
item = dict(
category=campaign_id,
title1=escape(campaign[2]),
title2=escape(campaign[3]),
title3=escape(campaign[4]),
description=escape(campaign[5]),
num_backers=ent.funder_count,
percentage_raised=pct * 1,
total_raised=render_number_with_commas(raised),
target=render_number_with_commas(target)
)
items.append(item)
return items
class PayPalTransaction(db.Model):
"""Appengine model class encapsultating a donation made to a campaign.
The key is the paypal transaction id.
"""
v = db.IntegerProperty(default=0)
c = db.DateTimeProperty(auto_now_add=True)
m = db.DateTimeProperty(auto_now=True)
campaign_id = db.StringProperty(default='', indexed=False)
payer_email = db.StringProperty(default='', indexed=False)
fee = db.StringProperty(default='', indexed=False)
gross = db.StringProperty(default='', indexed=False)
is_handled = db.BooleanProperty(default=False)
info_payload = db.TextProperty()
net = db.StringProperty(default='', indexed=False)
payer_name = db.StringProperty(default='', indexed=False)
currency = db.StringProperty(default='', indexed=False)
class TransactionReceipt(db.Model):
"""Stub entity to synchronise accounted transactions.
The parent is the campaign_key, key is the txn_key.
"""