-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.py
134 lines (114 loc) · 4.02 KB
/
user.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
import json
from bson import json_util, ObjectId
import database
from datetime import datetime
def addUser(pseudo):
dbname = database.get_database()
now = datetime.now()
timestamp = datetime.timestamp(now)
User = {
"pseudo": pseudo,
"posts": [],
"subscribe": [],
"follower": [],
"notification": [],
"lastConnection": timestamp,
"likedPost": []
}
dbname.User.insert_one(User)
return dbname.User.find({"pseudo": pseudo})[0]
def find(pseudo):
dbname = database.get_database()
return json.loads(json_util.dumps(dbname.User.find({"pseudo": pseudo}, {"_id" : 0})[0]))
def findAll(research):
dbname = database.get_database()
data = dbname.User.find({"pseudo": {'$regex': '^' + research, '$options': 'i'}},{"_id":0})
response = {
"users" : json.loads(json_util.dumps(data))
}
return response
def findOne(pseudo):
dbname = database.get_database()
now = datetime.now()
timestamp = datetime.timestamp(now)
dbname.User.update_one({"pseudo": pseudo}, {'$set': {"lastConnection": timestamp}})
data = dbname.User.find({"pseudo": pseudo})
return json.loads(json_util.dumps(data))
def followOne(followerPseudo, followedPseudo):
try:
dbname = database.get_database()
follower = dbname.User.find({"pseudo": followerPseudo})[0]
followed = dbname.User.find({"pseudo": followedPseudo})[0]
subscribes = follower["subscribe"]
follows = followed["follower"]
subscribes.append(followed["pseudo"])
follows.append(follower["pseudo"])
dbname.User.update_one({"pseudo": followerPseudo}, {'$set': {"subscribe": subscribes}})
dbname.User.update_one({"pseudo": followedPseudo}, {'$set': {"follower": follows}})
response = {
"result": True
}
return response
except:
response = {
"result": False
}
return response
def unfollowOne(followerPseudo, followedPseudo):
response = {
"result": True
}
try:
dbname = database.get_database()
follower = dbname.User.find({"pseudo": followerPseudo})[0]
followed = dbname.User.find({"pseudo": followedPseudo})[0]
subscribes = follower["subscribe"]
follows = followed["follower"]
subscribes.remove(followed["pseudo"])
follows.remove(follower["pseudo"])
dbname.User.update_one({"pseudo": followerPseudo}, {'$set': {"subscribe": subscribes}})
print(dbname.User.find({"pseudo": followerPseudo})[0])
dbname.User.update_one({"pseudo": followedPseudo}, {'$set': {"follower": follows}})
print(dbname.User.find({"pseudo": followedPseudo})[0])
return response
except:
response = {
"result": False
}
return response
def verifyFollow(followerPseudo, followedPseudo):
response = {
"result": False
}
try:
dbname = database.get_database()
follower = dbname.User.find({"pseudo": followerPseudo})[0]
followed = dbname.User.find({"pseudo": followedPseudo})[0]
subscribes = follower["subscribe"]
if followed["pseudo"] in subscribes:
response["result"] = True
return response
else:
return response
except:
return response
def findIDbyPseudo(pseudo):
dbname = database.get_database()
return dbname.User.find({"pseudo": pseudo})[0]["_id"]
def findPseudoByID(id):
dbname = database.get_database()
return dbname.User.find({"pseudo": id})[0]["pseudo"]
def getPostfromUser(pseudo):
try:
dbname = database.get_database()
postsID = dbname.User.find({"pseudo": pseudo})[0]["posts"]
postObjectId = []
for elem in postsID:
postObjectId.append(ObjectId(elem))
posts = dbname.Post.find({"_id": {"$in": postObjectId}}).sort("creation")
response = {
"posts" : json.loads(json_util.dumps(posts))[::-1]
}
return response
except:
return "error"