-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hack20Firestore.py
44 lines (39 loc) · 1.33 KB
/
Hack20Firestore.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
from google.cloud import firestore
# Project ID is determined by the GCLOUD_PROJECT environment variable
db = firestore.Client()
#doc_ref = db.collection(u'users').document()
#doc_ref.set({
# u'Name': u'William',
# u'Classes': ['MATH 308'],
# u'Interests': ['Chess'],
#})
def addUser(email, name=None, classes=None, interests=None, year=None, major=None):
db = firestore.Client()
doc_ref = db.collection(u'users').document()
doc_ref.set({
u'Name': name,
u'Classes': classes,
u'Interests': interests,
u'Year': year,
u'EMail': email,
u'Major': major
})
def getUser(email):
users_ref = db.collection(u'users')
docs = db.collection(u'users').where(u'EMail', u'==', email).stream()
for doc in docs:
return doc.to_dict()
def getUserMatches(email):
users_ref = db.collection(u'users')
docs = db.collection(u'users').where(u'EMail', u'==', email).stream()
matches = dict()
for doc in docs:
docDict = doc.to_dict()
matches = {}
interests = docDict["Interests"]
classes = docDict["Classes"]
docs2 = db.collection(u'users').where(u'Interests', u'array_contains_any', interests).stream()
for doc2 in docs2:
matches[doc2.to_dict()["EMail"]] = doc2.to_dict()
matches.pop(email)
return matches