-
Notifications
You must be signed in to change notification settings - Fork 0
/
User.py
58 lines (46 loc) · 1.74 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
import functions
from var import *
from bson.objectid import ObjectId
class User(object):
"""MongoDB-Backed User"""
def __init__(self, username, userid=None):
self.alt = None
if userid:
self.obj = functions.users.find_one({"_id": ObjectId(userid)})
self.username = self.obj["username"]
else:
self.username = username
self.obj = functions.users.find_one({"username": username})
if self.obj == None:
self.obj = functions.users.find_one({"emails":{"$in":[username]}})
if self.obj != None:
self.alt = username
self.username = self.obj["username"]
def is_valid(self):
return self.obj != None
def get(self,attr):
if attr == 'mailing':
return self.alt if self.alt else self.username
try:
return self.obj[attr]
except KeyError:
return None
def check_pass(self,passwd):
return self.check_pass_hash(functions.hash_password(passwd))
def get_token(self):
return functions.gen_auth_token(str(self.obj["_id"]))
def check_token(self,token):
return functions.check_auth_token(token,str(self.obj["_id"]))
def check_pass_hash(self,passwd):
return passwd == self.obj["password"]
def add_email(self,email):
if functions.users.find({"emails":{"$in":[email]}}).count() == 0:
return functions.users.update({"username": self.username}, {'$push': {'emails': email}}, True)
def get_letters(self):
return letters.find({"job.from_address.email": self.get("username")})
def get_letters_count(self):
return int(letters.find({"job.from_address.email": self.get("username")}).count())
def get_letters_reversed(self):
return letters.find({"job.from_address.email": self.get("username")}).sort("job.date_created", -1)
def get_postcards(self):
return postcards.find({"job.from_address.email": self.get("username")})