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
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
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
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
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
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
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
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
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
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
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
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 []
def get_total(cls): sql = '''select count(*) as total from {}'''.format(cls.__table__) rs = store.execute(sql) return rs[0]['total']
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
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
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 []
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 []
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
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