-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.py
58 lines (49 loc) · 1.7 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
import pymongo
from datetime import datetime
from bson.objectid import ObjectId
import hashlib
# MongoDB Connection
client = pymongo.MongoClient('localhost', 27017)
# Select Database name
db = client['test']
# Select Collection name from database
collection = db['test']
# Add new user to db
# take name:string ,email:string ,password:string
# hash input password then add user to database
# Return json message
def addUser(name, email, password):
user = collection.find_one({"email": email})
if user == None:
user = {"name": name, "email": email,
"password": hashlib.sha256(password.encode('utf-8')).hexdigest(), "photos": []}
collection.insert_one(user)
return {"message": "Created"}
else:
return {"message": "Already Exist"}
# Get user from db
# take email:string ,password:string
# hash input password then query db
# Return user data
# Erorr return json message
def getUser(email, password):
user = collection.find_one(
{"email": email, "password": hashlib.sha256(password.encode('utf-8')).hexdigest()})
if user == None:
return {"message": "Not Exist"}
else:
return user
# add new picture to user data
# take email:string ,password:string , filename:string
# hash input password then update user data
# Return json message
# Erorr return json message
def addphoto(email, password, photoName):
result = collection.update_one(
{"email": email, "password": hashlib.sha256(password.encode('utf-8')).hexdigest()}, {
"$push": {'photos': {"filename": photoName, "date": datetime.utcnow()}}}
).matched_count
if result == 1:
return {"message": "Added"}
else:
return {"message": "Error"}