-
Notifications
You must be signed in to change notification settings - Fork 10
/
orm.py
155 lines (117 loc) · 5.75 KB
/
orm.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# !/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/>.
import logging
import db
from model import User, Post
logger = logging.getLogger(__name__)
class BaseMapper(object):
def load(self, data, o):
return o(*data)
class PrimaryTrait(object):
primary_id = 'id'
def find(self, id):
q = db.select(self.table).condition(self.primary_id, id)
data = q.query()
if data:
return self.load(data[0], self.model)
class UserMapper(BaseMapper, PrimaryTrait):
model = User
table = 'users'
def find(self, uid):
"""Find and load the user from database by uid(user id)"""
data = (db.select(self.table).select('username', 'email', 'real_name',
'password', 'bio', 'status', 'role', 'uid').
condition('uid', uid).execute()
)
if data:
logger.info('data %s', data)
return self.load(data[0], self.model)
def find_by_username(self, username):
"""Return user by username if find in database otherwise None"""
data = (db.select(self.table).select('username', 'email', 'real_name',
'password', 'bio', 'status', 'role', 'uid').
condition('username', username).execute()
)
if data:
return self.load(data[0], self.model)
def create(self, user):
return db.execute("INSERT INTO users(username, email, real_name, password, bio, status, role) \
VALUES(%s, %s, %s, %s, %s, %s, %s)",
(user.username, user.email, user.real_name, user.password, user.bio, user.status, user.role))
def search(self, **kw):
"""Find the users match the condition in kw"""
q = db.select(self.table).condition('status', 'active')
for k, v in kw:
q.condition(k, v)
data = q.execute()
users = []
for user in data:
users.append(self.load(user, self.model))
return users
def count(self):
return db.query('SELECT COUNT(*) FROM ' + self.table)[0][0]
def paginate(self, page=1, perpage=10):
count = self.count()
q = db.select(self.table).select('username', 'email', 'real_name',
'password', 'bio', 'status', 'role', 'uid')
results = q.limit(perpage).offset((page - 1) * perpage).order_by('real_name', 'desc').execute()
return [self.load(user, self.model) for user in results]
def save(self, user):
q = db.update(self.table)
data = dict((_, getattr(user, _)) for _ in ('username', 'email', 'real_name',
'password', 'bio', 'status', 'role'))
q.mset(data)
return q.condition('uid', user.uid).execute()
def delete(self, user):
return db.delete(self.table).condition('uid', user.uid).execute()
class PostMapper(BaseMapper):
table = 'posts'
model = Post
def find(self, pid):
data = db.select(self.table).fields('title', 'slug', 'description', 'html', 'css', 'js',
'category', 'status', 'comments', 'author', 'created', 'pid').condition('pid', pid).execute()
if data:
return self.load(data[0], self.model)
def count(self):
return db.select(self.table).fields(db.expr('COUNT(*)')).execute()[0][0]
def create(self, post):
row = []
for _ in ('title', 'slug', 'description', 'created', 'html', 'css', 'js',
'category', 'status', 'comments', 'author'):
row.append(getattr(post, _))
return db.insert(self.table).columns('title', 'slug', 'description', 'created', 'html', 'css', 'js',
'category', 'status', 'comments', 'author').values(row).execute()
def paginate(self, page=1, perpage=10, category=None):
"""Paginate the posts"""
q = db.select(self.table).fields('title', 'slug', 'description', 'html', 'css', 'js',
'category', 'status', 'comments', 'author', 'created', 'pid')
if category:
q.condition('category', category)
results = (q.limit(perpage).offset((page - 1) * perpage)
.order_by('created', 'DESC').execute())
return [self.load(data, self.model) for data in results]
def save(self, page):
q = db.update(self.table)
data = dict((_, getattr(page, _)) for _ in ('title', 'slug', 'description', 'html', 'css', 'js',
'category', 'status', 'comments'))
q.mset(data)
return q.condition('pid', page.pid).execute()
def delete(self, page_id):
return db.delete(self.table).condition('pid', page_id).execute()
def category_count(self, category_id):
return db.select(self.table).fields(db.expr('count(*)',
'total')).condition('category', category_id).condition('status', 'published').execute()[0][0]
__backends = {}
__backends['post'] = PostMapper()
__backends['user'] = UserMapper()
def Backend(name):
return __backends.get(name)