Esempio n. 1
0
 def update(cls, id, content, news_id, author_id):
     sql = '''update {} set content=%s, news_id=%s, author_id=%s where id=%s'''.format(cls.__table__)
     params = (content, news_id, author_id, id)
     try:
         store.execute(sql, params)
         _id = store.commit()
     except e:
         print "Error", e.args[0], e.args[1]
         store.rollback()
     return cls.get(_id) if _id else None
Esempio n. 2
0
 def create(cls, name):
     sql = 'insert into {}(name) values(%s)'.format(cls.__table__)
     params = (name, )
     try:
         store.execute(sql, params)
         _id = store.commit()
     except e:
         print "Error", e.args[0], e.args[1]
         store.rollback()
     return cls.get(_id) if _id else None
Esempio n. 3
0
 def update(cls, id, name):
     sql = 'update {} set name=%s where id=%s'.format(cls.__table__)
     params = (name, id)
     try:
         store.execute(sql, params)
         _id = store.commit()
     except e:
         print "Error", e.args[0], e.args[1]
         store.rollback()
     return cls.get(_id) if _id else None
Esempio n. 4
0
 def validate(cls, username, passwd):
     rs = store.execute('select salt from {} where username=%s'.format(cls.__table__), (username,))
     salt = rs[0]['salt'] if rs else None
     if not salt:
         return None
     print '##salt=' , salt
     passwd = md5.new(passwd+salt).hexdigest()
     sql = 'select * from {} where username=%s and passwd=%s'.format(cls.__table__)
     params = (username, passwd)
     rs = store.execute(sql, params)
     return cls(**rs[0]) if rs else None
Esempio n. 5
0
 def create(cls, content, news_id, author_id):
     sql = '''insert into {}(content, news_id, author_id, create_time)
              values(%s, %s, %s, %s)
           '''.format(cls.__table__)
     params = (content, news_id, author_id, now())
     try:
         store.execute(sql, params)
         _id = store.commit()
     except e:
         print "Error", e.args[0], e.args[1]
         store.rollback()
     return cls.get(_id) if _id else None
Esempio n. 6
0
 def delete(cls, ids):
     state = False
     sql = 'delete from {} where id=%s'.format(cls.__table__)
     ids = ids if isinstance(ids, list) else [ids]
     try:
         for id in ids:
             params = (id,)
             store.execute(sql, params)
         store.commit()
         state = True
     except:
         store.rollback()
     return state
Esempio n. 7
0
 def create(cls, title, content, cover_url, category_id, author_id,  alias_title=None):
     sql = '''insert into {}(title, alias_title, content, cover_url, category_id, author_id, create_time)
             values(%s, %s, %s, %s, %s, %s, %s)
         '''.format(cls.__table__)
     params = (title, alias_title, content, cover_url, category_id, author_id, now())
     try:
         store.execute(sql, params)
         _id = store.commit()
     except e:
         print "Error", e.args[0], e.args[1]
         store.rollback()
     print _id
     return cls.get(id=_id) if _id else None
Esempio n. 8
0
 def create(cls, username, passwd, nickname, gender, birthday, avatar_url):
     salt = ''.join(random.sample(string.ascii_letters, 6))
     passwd = md5.new(passwd + salt).hexdigest()
     sql = '''insert into {}(username, passwd, nickname, salt, gender, birthday, avatar_url, register_time) 
              values(%s, %s ,%s, %s, %s, %s, %s, %s)'''.format(cls.__table__)
     params = (username, passwd, nickname, salt, gender, birthday, avatar_url, now())
     try:
         store.execute(sql, params)
         _id = store.commit()
     except e:
         print "Error", e.args[0], e.args[1]
         store.rollback()
     print _id
     return cls.get(_id) if _id else None
Esempio n. 9
0
 def get_auth(cls, group_id, name):
     sql = 'select %s from {} where id=%s'.format(cls.__table__)
     params = (name, group_id)
     print sql % params
     rs = store.execute(sql, params)
     print rs
     return rs[0][name] if rs else None
Esempio n. 10
0
 def get_all(cls, start=0, limit=0):
     if start or limit:
         sql = 'select * from {} {}'.format(cls.__table__, 'limit %s,%s')
         params = (start, limit)
     else:
         sql = 'select * from {} '.format(cls.__table__)
         params = ()
     rs = store.execute(sql, params)
     return [cls(**r) for r in rs] if rs else None
Esempio n. 11
0
    def update(cls, id, keys, values):
        keys = keys if isinstance(keys, list) else [keys]
        values = values if isinstance(values, list) else [values]

        keyformat = '=%s,'.join(keys) + '=%s'
        sql = 'update {} set {} where id=%s'.format(cls.__table__, keyformat)
        values.append(int(id))
        params = tuple(values)
        rcnt = 0
        print sql % params
        try:
            rcnt = store.execute(sql, params, True)
            store.commit()
        except:
            print 'except:', sql % params
            store.rollback()
        print 'rcnt=', rcnt
        return cls.get(id=id) if rcnt > 0 else None
Esempio n. 12
0
 def get_all(cls):
     sql = 'select * from {} order by id asc'.format(cls.__table__)
     rs = store.execute(sql)
     return [cls(**r) for r in rs] if rs else []
Esempio n. 13
0
 def get_total(cls):
     sql = '''select count(*) as total from {}'''.format(cls.__table__)
     rs  = store.execute(sql)
     return rs[0]['total']
Esempio n. 14
0
 def get_by_alias(cls, alias):
     sql = 'select * from {} where alias_title=%s'.format(cls.__table__)
     params = (alias, )
     rs = store.execute(sql, params)
     return cls(**rs[0]) if rs else None
Esempio n. 15
0
 def get(cls, nid):
     sql = 'select * from {} where id=%s'.format(cls.__table__)
     params = (nid, )
     rs = store.execute(sql, params)
     return cls(**rs[0]) if rs else None
Esempio n. 16
0
 def get_by_category(cls, cid, order, start=0, limit=PAGE_LIMIT):
     sql = 'select * from {} where category_id=%s order by {} limit %s,%s'.format(cls.__table__, order)
     params = (cid, start, limit)
     rs = store.execute(sql, params)
     return [cls(**r).ldict() for r in rs] if rs else []
Esempio n. 17
0
 def get_all(cls, order, start=0, limit=PAGE_LIMIT):
     sql = 'select * from {} order by {} limit %s,%s'.format(cls.__table__, order)
     params = (start, limit)
     rs = store.execute(sql, params)
     return [cls(**r).ldict() for r in rs] if rs else []
Esempio n. 18
0
 def get_by_username(cls, username):
     sql = 'select * from {} where username=%s'.format(cls.__table__)
     params = (username,)
     rs = store.execute(sql, params)
     return cls(**rs[0]) if rs else None
Esempio n. 19
0
 def get_by_token(cls, token):
     sql = 'select * from {} where passwd=%s'.format(cls.__table__)
     params = (token, )
     rs = store.execute(sql, params)
     return cls(**rs[0]) if rs else None