This repository was archived by the owner on May 28, 2022. It is now read-only.
Repository navigation
Files Expand file tree
master
/
models.py
executable file
·155 lines (125 loc) · 4.38 KB
master
/
models.py
File metadata and controls
- Code
- Blame
executable file
·155 lines (125 loc) · 4.38 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# -*- coding: UTF-8 -*-
from datetime import datetime
from werkzeug.security import generate_password_hash, check_password_hash
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from flask.ext.login import UserMixin, AnonymousUserMixin
from flask import current_app
from . import mongo, login_manager
class Permission:
FOLLOW = 0x01
POST = 0x02
MODERATE = 0x04
ADMINISTER = 0x08
class Role:
USER = Permission.FOLLOW |\
Permission.POST
MODERATOR = Permission.FOLLOW |\
Permission.POST |\
Permission.MODERATE
ADMINISTRATOR = 0xff
class User(UserMixin):
def __init__(self, username):
self.username = username
def get_id(self):
return self.username
def can(self, permissions):
return self.role is not None and \
(int(self.role) & permissions) == permissions
def is_administrator(self):
return self.can(Permission.ADMINISTER)
@property
def role(self):
doc = mongo.db.users.find_one({'_id': self.username},
{'_id':0, 'role': 1})
try:
return doc['role']
except KeyError, e:
return None
@role.setter
def role(self, role):
mongo.db.users.update_one({'_id': self.username},
{'$set': { 'role': role}})
@property
def confirmed(self):
doc = mongo.db.users.find_one({'_id': self.username},
{'_id':0, 'confirmed': 1})
try:
return doc['confirmed']
except KeyError, e:
return False
@property
def email(self):
doc = mongo.db.users.find_one({'_id': self.username},
{'_id':0, 'email': 1})
try:
return doc['email']
except KeyError, e:
return False
@staticmethod
def get_user(username):
user = mongo.db.users.find_one({'_id': username})
return user if user else None
@staticmethod
def add_user(username, email, password, role):
mongo.db.users.insert_one({'_id': username, 'email': email,
'password_hash': generate_password_hash(password),
'role': role})
<
748D
div>
@staticmethod
def email_exists(email):
user = mongo.db.users.find_one({'email': email})
return True if user else False
@staticmethod
def validate_login(password_hash, password):
return check_password_hash(password_hash, password)
@staticmethod
def generate_confirmation_token(username=None, expiration=3600):
s = Serializer(current_app.config['SECRET_KEY'], expiration)
return s.dumps({'confirm': username})
@staticmethod
def confirm(username, token):
s = Serializer(current_app.config['SECRET_KEY'])
try:
data = s.loads(token)
except:
return False
if data.get('confirm') != username:
return False
mongo.db.users.update_one({'_id': username},
{'$set': { 'confirmed': True}})
return True
class Saying(object):
tags = [('funny', 'مضحك'),
('adult', '18+'),
]
@staticmethod
def add_saying(text, added_by, origins=[], tags=[]):
doc = { 'text' : text,
'added_by': added_by,
'date_created': datetime.utcnow(),
'origins': origins,
'tags': tags,
'approved': False}
mongo.db.sayings.insert_one(doc)
@staticmethod
def get_country_names(language='english'):
countries = list(mongo.db.countries.aggregate([
{ '$unwind' : '$country' },
{ '$match' : { 'country.language': language } },
{ '$group' : { '_id' : '$country.name'} }
]))
return [(x['_id'], x['_id']) for x in countries]
@staticmethod
def get_tags():
return Saying.tags
class AnonymousUser(AnonymousUserMixin):
def can(self, permissions):
return False
def is_administrator(self):
return False
login_manager.anonymous_user = AnonymousUser
@login_manager.user_loader
def load_user(username):
u = mongo.db.users.find_one({"_id": username})
if not u:
return None
return User(u['_id'])