forked from j2labs/listsurf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queries.py
160 lines (117 loc) · 3.51 KB
/
queries.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
#!/usr/bin/env python
import pymongo
from brubeck.models import User
###
### Database Connection Handling
###
def init_db_conn(**kwargs):
dbc = pymongo.Connection(**kwargs)
db_conn = dbc.linksurf
return db_conn
def end_request(db_conn):
"""Here as a visual reminder that this funciton must be called at the end
of a request to return the socket back to pymongo's built-in thread pooling.
"""
return db_conn.end_request()
###
### Index Handling
###
def apply_all_indexes(db, indexes, collection):
"""Takes a list of indexes and applies them to a collection.
Intended for use after functions that create/update/delete entire
documents.
"""
for index in indexes:
db[collection].ensure_index(index)
return True
###
### User Handling
###
USER_COLLECTION = 'users'
indexes_user = [
[('username', pymongo.ASCENDING)],
[('email', pymongo.ASCENDING)],
]
def load_user(db, username=None, email=None):
"""Loads a user document from MongoDB.
"""
query_dict = dict()
if username:
query_dict['username'] = '%s' % (username.lower())
elif email:
query_dict['email'] = email.lower()
else:
raise ValueError('Username or email field required')
user_dict = db[USER_COLLECTION].find_one(query_dict)
# In most cases, the python representation of the data is returned. User
# documents are instantiated to provide access to commonly needed User
# functions
if user_dict is None:
return None
else:
u = User(**user_dict)
return u
def save_user(db, user):
"""Loads a user document from MongoDB.
"""
user_doc = user.to_python()
uid = db[USER_COLLECTION].insert(user_doc)
user._id = uid
apply_all_indexes(db, indexes_user, USER_COLLECTION)
return uid
###
### UserProfile Handling
###
USERPROFILE_COLLECTION = 'userprofiles'
indexes_userprofile = [
[('owner', pymongo.ASCENDING)],
[('username', pymongo.ASCENDING)],
]
def load_userprofile(db, username=None, uid=None):
"""Loads a user document from MongoDB.
"""
query_dict = dict()
if username:
query_dict['username'] = username.lower()
elif uid:
query_dict['owner'] = uid
else:
raise ValueError('<username> or <email> field required')
userprofile_dict = db[USERPROFILE_COLLECTION].find_one(query_dict)
return userprofile_dict
def save_userprofile(db, userprofile):
"""Loads a user document from MongoDB.
"""
userprofile_doc = userprofile.to_python()
upid = db[USERPROFILE_COLLECTION].insert(userprofile_doc)
userprofile._id = upid
apply_all_indexes(db, indexes_userprofile, USERPROFILE_COLLECTION)
return upid
###
### ListItem Handling
###
LISTITEM_COLLECTION = 'listitems'
indexes_listitem = [
[('owner', pymongo.ASCENDING)],
[('username', pymongo.ASCENDING)],
]
def load_listitems(db, owner=None, username=None):
"""Loads a user document from MongoDB.
"""
query_dict = dict()
if username:
query_dict['username'] = username.lower()
elif owner:
query_dict['owner'] = owner
else:
raise ValueError('<owner> or <username> field required')
query_set = db[LISTITEM_COLLECTION].find(query_dict)
return query_set
def save_listitem(db, item):
"""Loads a user document from MongoDB.
"""
item_doc = item.to_python()
iid = db[LISTITEM_COLLECTION].insert(item_doc)
item._id = iid
apply_all_indexes(db, indexes_listitem, LISTITEM_COLLECTION)
return iid