-
Notifications
You must be signed in to change notification settings - Fork 7
/
bootstrap.py
83 lines (63 loc) · 2.21 KB
/
bootstrap.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" Bootstrap the application.
"""
import logging
import yaml
from weblayer import RequestHandler
import model
class Bootstrap(RequestHandler):
""" Delete all existing `model.Series` and `model.Quote` instances and create
new instances corresponding to `series.yaml` and `quotes.yaml`.
"""
def bootstrap_series(self):
# Delete existing (presumes there won't ever be more than 300).
existing = model.Series.all().fetch(300)
number_removed = len(existing)
model.db.delete(existing)
# Create new.
sock = open('series.yaml', 'r')
series = yaml.load(sock)
sock.close()
to_add = []
i = 0
for item in series:
kwargs = item.copy()
key_name = kwargs.pop('value')
instance = model.Series(key_name=key_name, order=i, **kwargs)
to_add.append(instance)
i = i + 1
number_added = len(to_add)
model.db.put(to_add)
# Return how many we created and deleted.
return 'Deleted %s and created %s `model.Series` instances.' % (
number_removed,
number_added
)
def bootstrap_quotes(self):
# Delete existing (presumes there won't ever be more than 300).
existing = model.Quote.all().fetch(300)
number_removed = len(existing)
model.db.delete(existing)
# Create new.
sock = open('quotes.yaml', 'r')
series = yaml.load(sock)
sock.close()
to_add = []
for item in series:
kwargs = item.copy()
key_name = kwargs.pop('org')
instance = model.Quote(key_name=key_name, **kwargs)
to_add.append(instance)
number_added = len(to_add)
model.db.put(to_add)
# Return how many we created and deleted.
return 'Deleted %s and created %s `model.Quote` instances.' % (
number_removed,
number_added
)
def get(self):
return '<br />%s' % (
#self.bootstrap_series(),
self.bootstrap_quotes()
)