-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtest.py
220 lines (173 loc) · 5.67 KB
/
test.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
from pymongo.mongo_client import MongoClient
import logging
import humongolus as orm
import datetime
import humongolus.field as field
import humongolus.widget as widget
from tests.states import states
conn = MongoClient()
FORMAT = '%(asctime)-15s %(message)s'
logging.basicConfig(format=FORMAT)
logger = logging.getLogger("humongolus")
orm.settings(logger=logger, db_connection=conn)
class Car(orm.Document):
_db = "test"
_collection = "cars"
owner = field.DynamicDocument()
make = field.Char()
model = field.Char()
year = field.Date()
silly_date = field.TimeStamp()
Car.__remove__()
class Address(orm.EmbeddedDocument):
street = field.Char()
street2 = field.Char()
zip = field.Char()
class Location(orm.EmbeddedDocument):
city = field.Char(required=True)
state = field.Char()
address = Address()
class Job(orm.EmbeddedDocument):
employer = field.Char()
title = field.Char(required=True)
locations = orm.List(type=Location)
class Human(orm.Document):
_db = "test"
_collection = "humans"
human_id = field.AutoIncrement(collection="human")
name = field.Char(required=True, min=2, max=25)
age = field.Integer(required=True, min=0, max=3000)
height = field.Float(min=1, max=100000)
weight = field.Float(min=1, max=30000)
jobs = orm.List(type=Job)
genitalia = field.Char()
location = Location()
car = field.ModelChoice(type=Car)
color = field.Choice(choices=[{'value':'red', 'display':'Red'},{'value':'blue', 'display':'Blue'},{'value':'green', 'display':'Green'}])
state = field.CollectionChoice(db='test', collection='states', sort=[('fullname',1)])
email = field.Email()
class Female(Human):
genitalia = field.Char(default='inny')
class Male(Human):
genitalia = field.Char(default='outy')
class CarDisplay(orm.Widget):
#ideally you're using some sort of templating engine, I prefer Mako.
def render(self, *args, **kwargs):
return """
<ul class='%s'>
<li>Make: %s</li>
<li>Model: %s</li>
<li>Year: %s</li>
</ul>
""" % (kwargs.get("cls", ""), self._object.make, self._object.model, self._object.year)
Human.cars = orm.Lazy(type=Car, key='owner._id')
chris = Male()
chris.name = "Chris"
chris.age = 31
chris.height = 100
chris.weight = 180
chris.location.city = "Chicago"
chris.location.state = "IL"
chris.location.address.zip = 60626
job = Job()
job.employer = "Entropealabs"
job.title = "President"
loc = Location()
loc.city = "Chicago"
loc.state = "IL"
job.locations.append(loc)
chris.jobs.append(job)
print(chris._json())
_id = chris.save()
print(_id)
car = Car()
car.owner = chris
car.make = "Isuzu"
car.model = "Rodeo"
car.year = datetime.datetime(1998, 1, 1)
print(car)
c_id = car.save()
car2 = Car()
car2.owner = chris
car2.make = "Mercedes"
car2.model = "Baby C"
car2.year = datetime.datetime(1965, 1, 1)
print(car2)
c_id = car2.save()
print(car._get("owner")().name)
def car_disp(car):
return {"value":car._id, "display":"%s %s %s" % (car.make, car.model, car.year)}
def coll_display(doc):
return {'value':doc.get('abbrv'), 'display':doc.get('fullname', None)}
def job_list(obj):
ar = []
for i in obj:
ar.append({"value":i.title, "display":"%s: %s" % (i.employer, i.title)})
return ar
class AddressForm(widget.FieldSet):
_fields = ["street", "street2", "zip"]
street = widget.Input(cls="woot")
street2 = widget.TextArea(cls='test', rows=100, cols=30)
zip = widget.Input()
class LocationForm(widget.FieldSet):
_fields = ["city", "state", "address"]
cls = "location"
city = widget.Input()
state = widget.Input()
address = AddressForm()
class PersonForm(widget.Form):
action = '/save_person'
id = "person_%s" % chris._id
_prepend = "test"
#if anyone knows a better way to maintain the order of the fields, please let me know!
_fields = ["human_id", "name", "age", "car", "location", "jobs", "email"]
human_id = widget.Input(label="ID")
name = widget.Input(label="Name")
age = widget.Input(label="Age", description="This is today minus the date you were born in seconds.")
car = widget.Select(label="Car", item_render=car_disp)
location = LocationForm(label="Location")
jobs = widget.MultipleSelect(label="Jobs", item_render=job_list)
email = widget.Input(label="Email")
submit = {
"test_name":"None",
"test_human_id":"32226",
"test_age":None,
"test_weight":"175",
"test_car":"ffed81a42000002",
"test_location-city":"Chicago",
"test_location-state":"IL",
"test_location-address-street":"549 Randolph",
"test_location-address-street2":"450",
#"location-address-zip":"60626"
}
print(states)
conn['test']['states'].remove()
for k,v in states.items():
conn['test']['states'].insert({"abbrv":k, 'fullname':v})
class SelectorForm(widget.Form):
_fields = ['car', 'color', 'state']
car = widget.Select(label="Car", item_render=car_disp)
color = widget.Select(label="Color")
state = widget.Select(label="State", item_render=coll_display)
print("SELECTS:")
a = SelectorForm(object=chris)
print(a.render())
form = PersonForm(object=chris, data=submit)
print(form.car.render(cls="try-this"))
print(form.render())
try:
form.validate()
print("Validated")
except orm.DocumentException as e:
print(e)
for f in form:
if f.errors:
print(f.attributes.name)
if f.attributes.description: print(f.attributes.description)
print(f.errors)
print(form.errors)
print(e.errors)
except Exception as e:
print(e)
form2 = PersonForm(object=Human(), prepend=None)
print(form2.render())