def add_user(cls, username, password): """add new user should use transaction register_info should only contain the following field: email, username, (country_code, phone) """ session = SQL_Session() return_user = None register_info = {'username': username, 'password': password} try: user = User(**register_info) user.password = password session.add(user) # if have two records after committing, should also rollback. if cls.record_count({'username': username}) > 1: print '记录多于1条, rollback', {'username': username} session.rollback() else: session.commit() return_user = user except Exception: print 'in register_user (will rollback)', traceback.print_exc() session.rollback() return return_user
def add_sns_account(cls, user_id, open_id, platform): """这三个参数确定的信息必须唯一. 而且需要保证 openid 和 platform确定的record唯一。因为只能对应同一个账号。 """ info = { 'user_id': int(user_id), 'openid': str(open_id), 'platform': int(platform) } info_unique = { 'openid': str(open_id), 'platform': int(platform) } session = SQL_Session() return_acc = None try: acc = SNSAccount(**info) session.add(acc) # if have two records after committing, should also rollback. if cls.record_count(info_unique) > 1: print '记录多于1条, rollback', info session.rollback() else: session.commit() return_acc = acc except Exception: print 'in register_user (will rollback)', traceback.print_exc() session.rollback() return return_acc
def update_user_info(cls, user, update_info): re = False try: for key, value in update_info.items(): setattr(user, key, value) SQL_Session().commit() re = True except Exception: SQL_Session().rollback() return re
def update_last_login_time(cls, user): """update last login time""" re = False try: user.last_login_time = timestamp_by_13() SQL_Session().commit() re = True except Exception: SQL_Session().rollback() return re
def add_post_history(cls, post_id, content): """1. new a version 2. new a cls() """ version = cls.new_version() record = cls(**{ 'post_id': post_id, 'version': version, 'content': content }) re_record = None try: SQL_Session().add(record) SQL_Session().commit() re_record = cls.find_one({ 'post_id': post_id, 'version': version }) except Exception: SQL_Session().rollback() return re_record
def add_post( cls, user_id, available_to_other=False, comment_permission=COMMENT_PERIMISSION.ANY_USER ): post = cls(**{ 'user_id': user_id, 'available_to_other': bool(available_to_other), 'comment_permission': int(comment_permission), }) session = SQL_Session() return_post = None try: session.add(post) session.commit() return_post = post except Exception: session.rollback() return return_post
def prepare(self): super(BaseView, self).prepare() SQL_Session()
def finish(self, chunk=None): SQL_Session.remove() super(BaseView, self).finish(chunk)