-
Notifications
You must be signed in to change notification settings - Fork 700
/
Copy pathtpl.py
80 lines (67 loc) · 2.71 KB
/
tpl.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
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# vim: set et sw=4 ts=4 sts=4 ff=unix fenc=utf8:
# Author: Binux<[email protected]>
# http://binux.me
# Created on 2014-08-07 22:27:07
import time
import logging
import umsgpack
import config
from libs import utils
from .basedb import BaseDB
class TPLDB(BaseDB):
'''
tpl db
id, userid, siteurl, sitename, banner, disabled, public, fork, har, tpl, variables, interval, note, ctime, mtime, atime, last_success
'''
__tablename__ = 'tpl'
def __init__(self, host=config.mysql.host, port=config.mysql.port,
database=config.mysql.database, user=config.mysql.user, passwd=config.mysql.passwd):
import mysql.connector
self.conn = mysql.connector.connect(user=user, password=passwd, host=host, port=port,
database=database, autocommit=True)
def add(self, userid, har, tpl, variables, interval=None):
now = time.time()
insert = dict(
userid = userid,
siteurl = None,
sitename = None,
banner = None,
disabled = 0,
public = 0,
fork = None,
har = har,
tpl = tpl,
variables = variables,
interval = interval,
ctime = now,
mtime = now,
atime = now,
last_success = None,
)
return self._insert(**insert)
def mod(self, id, **kwargs):
return self._update(where="id=%s" % self.placeholder, where_values=(id, ), **kwargs)
def get(self, id, fields=None):
for tpl in self._select2dic(what=fields, where='id=%s' % self.placeholder, where_values=(id, )):
return tpl
def delete(self, id):
self._delete(where="id=%s" % self.placeholder, where_values=(id, ))
def incr_success(self, id):
self._execute('UPDATE %s SET success_count=success_count+1, last_success=%d WHERE `id`=%d' % (
self.escape(self.__tablename__), time.time(), int(id)))
def incr_failed(self, id):
self._execute('UPDATE %s SET failed_count=failed_count+1 WHERE `id`=%d' % (
self.escape(self.__tablename__), int(id)))
def list(self, fields=None, limit=None, **kwargs):
where = '1=1'
where_values = []
for key, value in kwargs.items():
if value is None:
where += ' and %s is %s' % (self.escape(key), self.placeholder)
else:
where += ' and %s = %s' % (self.escape(key), self.placeholder)
where_values.append(value)
for tpl in self._select2dic(what=fields, where=where, where_values=where_values, limit=limit):
yield tpl