forked from bzamansky/Crushlists
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo.py
133 lines (111 loc) · 3.57 KB
/
mongo.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
from pymongo import Connection
import re
def conn():
connection = Connection('mongo2.stuycs.org')
db = connection.admin
res = db.authenticate('ml7','ml7')
db = connection['crushlists']
return db
def clearDB():
db = conn()
db.students.remove()
def addUserInfo(name,username,password):
db = conn()
regx = re.compile("^"+name,re.IGNORECASE)
r = [x for x in db.students.find({'name':regx})]
if r[0].has_key('username'):
return False
else:
db.students.update({"name": name}, {"$set": {"username": username,"password":password}})
return True
def addPerson(name,crushlist,*args):
db = conn()
if len(args) > 0:
d = {'name':name, 'crushlist':crushlist, 'username':args[0], 'password':args[1]}
else:
d = {'name':name, 'crushlist':crushlist}
regx = re.compile("^"+name,re.IGNORECASE)
r = [x for x in db.students.find({'name':regx})]
if len(r) > 0:
if d.has_key('username'):
db.students.update({'name':name},d)
else:
db.students.update({'name':name},{'$set' : {'crushlist':crushlist}})
else:
db.students.insert(d)
def getPeopleYouLike(name):
db = conn()
regx = re.compile("^"+name,re.IGNORECASE)
r = [x for x in db.students.find({'name':regx})]
if len(r) == 0:
return ["You have not submitted a crushlist","Or you might need to add a last name or try capitalizing/uncapitalizing the name"]
else:
a = r[0]
return a['crushlist']
def getPeopleWhoLikeYou(name):
db = conn()
r = [x for x in db.students.find()]
yours = []
for person in r:
for x in person['crushlist']:
if x.lower() == name.lower():
yours.append(person['name'])
if len(yours) == 0:
return ["No one has put you on their crushlist", "Or you might need to type your name differently or try capitalizing/uncapitalizing the name"]
return yours
def getName(username,password):
db = conn()
r = [x for x in db.students.find()]
for person in r:
if person.has_key('username'):
if person['username'] == username:
if person['password'] == password:
return person['name']
return 0
def getAllPeople():
db = conn()
r = [x for x in db.students.find()]
l = [x['name'] for x in r]
return l
def printAll():
db = conn()
r = [x for x in db.students.find()]
l = [[x['name'],x['crushlist']] for x in r]
for x in l:
print x[0]
print x[1]
def printUserInfo(username):
db = conn()
r = db.students.find_one({'name':username})
print r
def removeUser(name):
db = conn()
db.students.remove({'name':name})
#### Re-hashing the db
def addUser(name,username,password):
db = conn()
regx = re.compile("^"+name,re.IGNORECASE)
d = {'name':regx,'username':username,'password':password}
r = [x for x in db.users.find({'name':name})]
if len(r) == 0:
db.users.insert(d)
else:
return "User Already Exists"
def addPerson2(name,crush,year):
db = conn()
d = {'name':name,'crush':crush,'year':year}
r = [x for x in db.people.find({'name':name})]
print r
def getPeopleYouLike2(name):
db = conn()
regx = re.compile("^"+name,re.IGNORECASE)
r = [x for x in db.people.find({'name':regx})]
l = [[x['crush'],x['year']] for x in r]
return l
def getPeopleWhoLikeYou2(name):
db=conn()
regx = re.compile("^"+name,re.IGNORECASE)
r = [x for x in db.people.find({'crush':regx})]
l = [x['name'] for x in r]
return l
#printAll()