-
Notifications
You must be signed in to change notification settings - Fork 10
/
model.py
78 lines (64 loc) · 2.46 KB
/
model.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
#!/usr/bin/env python
# Copyright (C) 2014-2015 Thomas Huang
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from hashlib import sha224
from datetime import datetime
class User(object):
def __init__(self, username, email, real_name, password, bio, status, role='user', uid=None):
"""If the user load from database, if will intialize the uid and secure password.
Otherwise will hash encrypt the real password
arg role enum: the string in ('user', 'editor', 'administrator')
arg status enum: the string in ('actived', 'inactive')
arg password fix legnth string: the use sha224 password hash
"""
self.username = username
self.email = email
self.real_name = real_name
self.bio = bio
self.status = status
self.role = role
if uid:
self.uid = uid
self.password = password
else:
self.password = self.secure_password(password)
def check(self, password):
"""Check the password"""
return self.password == self.secure_password(password)
def secure_password(self, password):
"""Encrypt password to sha224 hash"""
return sha224(password).hexdigest()
def as_json(self):
data = self.__dict__.copy()
del data['password']
return data
class Post(object):
def __init__(self, title, slug, description, html, css, js, category, status,
comments, author=None, created=datetime.now(), pid=None):
self.title = title
self.slug = slug
self.description = description
self.created = created
self.html = html
self.css = css
self.js = js
self.category = category
self.status = status
self.comments = comments
self.author = author
self.created = created
self.pid = pid
def as_json(self):
data = self.__dict__.copy()
return data