forked from zhangluoma/Dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
69 lines (51 loc) · 1.67 KB
/
db.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
import datetime
from flask import url_for
from pymongo import MongoClient
from flask import Flask
client = MongoClient('mongodb://localhost:27017/')
db = client['test_database']
collection = db['test-collection']
#given username, check if this user exists
def check_user_exist(a):
return collection.find_one({'username':a})!=None
def check_user_password_right(a,b):
return collection.find_one({'username':a,'password':b})!=None
def add_user(a,b):
if check_user_exist(a):
return False
collection.insert({'username':a,'password':b,'friends':[],'movies':[]})
return True
def get_friends(a):
if not check_user_exist(a):
return False
tmp_user=collection.find_one({'username':a})
return tmp_user['friends']
def friend_exist(a,b):
return b in collection.find_one({'username':a})['friends']
def add_friend(a,b):
if not check_user_exist(a) or not check_user_exist(b) or friend_exist(a,b):
return False
collection.update({'username':a},{'$push':{'friends':b} } )
return True
def remove_friend(a,b):
if not check_user_exist(a) or not friend_exist(a,b):
return False
collection.update({'username':a},{'$pull':{'friends':b} } )
return True
def movie_exist(a,b):
return b in collection.find_one({'username':a})['movies']
def get_movies(a):
if not check_user_exist(a):
return False
tmp_user=collection.find_one({'username':a})
return tmp_user['movies']
def add_movie(a,b):
if not check_user_exist(a) or movie_exist(a,b):
return False
collection.update({'username':a},{'$push':{'movies':b} } )
return True
def remove_movie(a,b):
if not check_user_exist(a) or not movie_exist(a,b):
return False
collection.update({'username':a},{'$pull':{'movies':b} } )
return True