def add_contest(uid, title, desc, stime, etime, money, logo): """ 創建一個比賽: :param uid: :param title: 比賽標題 :param desc: 描述 :param stime: 開始時間:不能是當天或以前 :param etime: 結束時間,不能小於開始時間 :param money: 初始資金 :param logo: logo 圖片地址 :return: """ contest = Contest() contest.u_id = uid contest.c_title = title contest.c_explain = desc contest.c_start_date = stime contest.c_end_date = etime contest.c_logo_url = logo contest.c_default_capital = money * 10000 try: dbsession.add(contest) dbsession.commit() except Exception as e: dbsession.rollback() raise e return contest
def add_stock(uid, stock_code, stock_name): if uid == None or uid == '': logging.info("uid is None..") return if len(stock_code) == 0 or len(stock_name) == 0: logging.info("stock_code, stock_name 不合法..") return try: stock = dbsession.query(UserFavoriteStocks).filter_by( u_id=uid).filter_by(s_stock_code=stock_code).one() except NoResultFound as e: stock = None if stock is not None: return "exist" stock = UserFavoriteStocks() stock.u_id = uid stock.s_stock_code = stock_code stock.s_stock_name = stock_name dbsession.add(stock) dbsession.commit()
def add_user(user=None, **kwargs): if user is None: user = {} user.update(kwargs) dbsession.add(user) dbsession.commit()
def add_stock(uid, stock_code, stock_name, stock_count, stock_price): """ no return val raise 參數錯誤異常 :param uid: :param stock_code: :param stock_name: :param stock_count: :param stock_price: 成本價 :return: """ if not all([uid, stock_code, stock_name, stock_count, stock_price]): logging.error("參數錯誤:", uid, stock_code, stock_name, stock_count, stock_price) raise Exception("參數錯誤!") ### 查詢是否已經買過該股票,是:添加持股數,否添加紀錄 stock = None try: stock = dbsession.query(UserStock).filter_by(u_id=uid).filter_by(s_stock_code=stock_code).one() except NoResultFound as e: dbsession.rollback() logging.error(e) # 沒記錄,沒買過 if stock is None: stock = UserStock() stock.u_id = uid stock.s_stock_price = stock_price stock.s_stock_code = stock_code stock.s_stock_name = stock_name stock.s_stock_count = stock_count try: dbsession.add(stock) dbsession.commit() except Exception as e: dbsession.rollback() logging.error(e) raise e else: price = ((float)(stock.s_stock_price) * stock.s_stock_count + stock_price*stock_count) / (stock.s_stock_count+stock_count) stock.s_stock_count += stock_count stock.s_stock_price = price try: dbsession.merge(stock) dbsession.commit() except Exception as e: dbsession.rollback() logging.error(e) raise e
def upcountArticle(aid, uid): """ 給文章點贊, 如果電讚了得 就取消點讚 :param id: :param uid: :return: """ recode = None try: recode = dbsession.query(ArticleUpcounts).filter( and_(ArticleUpcounts.a_id == aid, ArticleUpcounts.u_id == uid)).one() except NoResultFound as e: pass except Exception as e: dbsession.rollback() raise e if not recode: recode = ArticleUpcounts() recode.u_id = uid recode.a_id = aid try: dbsession.add(recode) dbsession.commit() except Exception as e: dbsession.rollback() raise e try: dbsession.query(Article).filter(Article.id == aid).update( {Article.a_upcounts: Article.a_upcounts + 1}) dbsession.commit() except Exception as e: dbsession.rollback() raise e else: try: dbsession.delete(recode) dbsession.commit() dbsession.query(Article).filter(Article.id == aid).update( {Article.a_upcounts: Article.a_upcounts - 1}) dbsession.commit() except Exception as e: dbsession.rollback()
def upcountReply(uid, rid): """ 給評論點贊, 如果電讚了得 就取消點讚 :param id: :param uid: :return: """ recode = None try: recode = dbsession.query(ReplyUpcounts).filter( and_(ReplyUpcounts.r_id == rid, ReplyUpcounts.u_id == uid)).one() except NoResultFound as e: pass except Exception as e: dbsession.rollback() raise e if not recode: recode = ReplyUpcounts() recode.u_id = uid recode.r_id = rid try: dbsession.add(recode) dbsession.commit() except Exception as e: dbsession.rollback() raise e try: dbsession.query(Reply).filter(Reply.id == rid).update( {Reply.r_upcounts: Reply.r_upcounts + 1}) dbsession.commit() except Exception as e: dbsession.rollback() raise e else: try: dbsession.delete(recode) dbsession.commit() dbsession.query(Reply).filter(Reply.id == rid).update( {Reply.r_upcounts: Reply.r_upcounts - 1}) dbsession.commit() except Exception as e: dbsession.rollback()
def upcountArticleOnly(aid, uid): """ 給文章點贊:只點贊,不存在重複點贊 :param aid: :param uid: :return: success:return true,"" fail: return false,"失敗原因" :exception: exception returned if sql err """ recode = None try: recode = dbsession.query(ArticleUpcounts).filter( and_(ArticleUpcounts.a_id == aid, ArticleUpcounts.u_id == uid)).one() except NoResultFound as e: pass except Exception as e: dbsession.rollback() raise e if not recode: recode = ArticleUpcounts() recode.u_id = uid recode.a_id = aid try: dbsession.add(recode) dbsession.commit() except Exception as e: dbsession.rollback() raise e try: dbsession.query(Article).filter(Article.id == aid).update( {Article.a_upcounts: Article.a_upcounts + 1}) dbsession.commit() except Exception as e: dbsession.rollback() raise e return True, "" else: return False, "你已經點過贊了"
def addReplyReply(uid, aid, rid, content): """ 回復別人的回復消息 :param uid: :param aid: :param rid: :return: """ reply = Reply() reply.r_reply_id = rid reply.u_id = uid reply.a_id = aid reply.r_content = content try: dbsession.add(reply) dbsession.commit() except Exception as e: dbsession.rollback() raise e
def join_contest(uid, id): """ 用戶參加比賽:根據id獲取比賽的相關信息,例如初始資金,將數據填入tb_contest_detail中 :param uid: :param id: :return: """ test = None user_contest_detail = None try: test = dbsession.query(Contest).filter_by(id=id).one() except Exception as e: dbsession.rollback() raise e try: user_contest_detail = dbsession.query(ContestDetail).filter( and_(ContestDetail.u_id == uid, ContestDetail.c_id == test.id)).one() except NoResultFound as e: pass except Exception as e: dbsession.rollback() raise e # 已經存在數據,説明已經加入比賽 if user_contest_detail: raise Exception("已經加入該比賽!") user_contest_detail = ContestDetail() user_contest_detail.c_join_time = datetime.now() user_contest_detail.c_money = test.c_default_capital # 初始資金 user_contest_detail.c_id = test.id user_contest_detail.u_id = uid try: dbsession.add(user_contest_detail) dbsession.commit() except Exception as e: dbsession.rollback() raise e
def addReply(uid, aid, content): """ 添加一個回復 :param uid: :param aid: :return: """ reply = Reply() reply.u_id = uid reply.a_id = aid reply.r_content = content try: dbsession.add(reply) dbsession.commit() except Exception as e: dbsession.rollback() raise e return reply.id
def addArticle(uid, title, content): """ 添加一個話題 :param uid: :param title: :param content: :return: """ article = Article() article.u_id = uid article.a_title = title article.a_content = content try: dbsession.add(article) dbsession.commit() except Exception as e: dbsession.rollback() raise e return article.id
def add_user(**kwargs): user = User() user.u_name = kwargs.get('u_name') user.u_passwd = kwargs.get('u_passwd') user.u_email = kwargs.get('u_email') user.u_sex = kwargs.get('u_sex') user.u_mobilephone = kwargs.get('u_mobilephone') dbsession.add(user) dbsession.commit() logging.debug('SVC_userinfo 注册用户:%s', user) # 处理用户资产信息 user_money = user.u_money user_mv = 0 uid = user.id sql = 'insert into tb_user_assets ' \ 'set user_id=%s, user_money=%.2f, user_mv=%.2f' \ % (uid, user_money, user_mv) dbsession.execute(sql) dbsession.commit() return user