def cash_resource(post_id): post = Post.query.get(post_id) # 根据文章ID查找 if current_user == post.author: flash('不能兑换自己的资源', category='info') return redirect(url_for('article_detail', post_id=post_id)) else: # 以下是结算的内容! cost = int(post.source_value) if current_user.all_coins < cost: flash('余额不足,不能兑换!', category='danger') return redirect(url_for('article_detail', post_id=post_id)) else: current_user.all_coins = current_user.all_coins - cost # 用户的总金币数减少 describe = f'购买{post.id}_{post.title}' operate = 1 # 减少 coin = Coin(cost=cost, describe=describe, operate=operate) current_user.user_coins.append(coin) post.author.all_coins = post.author.all_coins + cost describe = f'用户{current_user.id}_购买{post.id}' operate = 2 # 增加 coin = Coin(cost=cost, describe=describe, operate=operate) post.author.user_coins.append(coin) db.session.commit() flash('兑换成功,请查收邮箱!', category='info') send_recharge_source_mail(current_user, post) return redirect(url_for('article_detail', post_id=post_id))
def register(): if current_user.is_authenticated: return redirect(url_for('index')) form = RegisterForm() if form.validate_on_submit(): username = request.form['username'] email = request.form['email'] password = request.form['password'] password = bcrypt.generate_password_hash(password) # bcrypt.check_password_hash(password, originpassword) default_img = '/static/asset/user_avatar/default/default_' + str(random.randint(1, 5)) + '.jpg' user = User(username=username, email=email, password=password, avatar_img=default_img, status=True, all_coins=0) db.session.add(user) db.session.commit() cost = 20 describe = '新用户注册赠送20金币' operate = 2 # 新增 new_coin = Coin(cost=cost, describe=describe, operate=operate) user.user_coins.append(new_coin) now_coin = user.all_coins + cost user.all_coins = now_coin db.session.add(user) db.session.commit() flash('注册成功,请登录!', category='success') return redirect(url_for('login')) else: flash("注册失败!请重新输入", category="danger") return render_template('register.html', form=form)
def index(): option_list = get_all() if request.method == 'GET': if request.args.get('option') is not None: c = request.args.get('option').split("-") coinSymbol = c[0] coinId = get_stringid_from_symbol(coinSymbol) coinName = c[1] coinPrice = round(float(get_price_from_symbol(coinSymbol)), 2) coin = Coin(coinid=coinId, symbol=coinSymbol, name=coinName, price=coinPrice) Coin.query.filter_by(name='None').delete() db.session.commit() found = 0 for c in Coin.query.filter(Coin.user_id == current_user.id).all(): if c.symbol == coinSymbol: found = 1 if found == 0: if coinName != 'None': current_user.coins.append(coin) db.session.commit() flash('Your coin is now added!') elif found == 1: flash('You have this coin already!') owns = current_user.followed_coins() return render_template('index.html', coins=owns, option_list=option_list)
def register(): if current_user.is_authenticated: return redirect(url_for('index')) form = RegisterForm() if form.validate_on_submit(): username = request.form['username'] email = request.form['email'] password = request.form['password'] password = bcrypt.generate_password_hash(password) # bcrypt.check_password_hash(password, originpassword) user = User(username=username, email=email, password=password, status=True, all_coins=0) db.session.add(user) db.session.commit() cost = 20 describe = '新用户注册赠送20金币' operate = 'True' new_coin = Coin(cost=cost, describe=describe, operate=operate) user.user_coins.append(new_coin) now_coin = user.all_coins + cost user.all_coins = now_coin db.session.add(user) db.session.commit() flash('注册成功,请登录!', category='success') return redirect(url_for('login')) return render_template('register.html', form=form)
def job1(): with scheduler.app.app_context(): try: print("INTERVAL JOB 1 DONE") # Get a request from api data = cg.get_coins_markets(vs_currency='usd', order='market_cap_desc', per_page=250, price_change_percentage='24h,7d') # Much better request handling for i in range(len(data)): new_coin = Coin.query.filter_by( name=data[i].get('name')).first() # If coin is new, add to db if new_coin is None: new_coin = Coin( name=data[i].get('name'), coin_id=data[i].get('id'), symbol=data[i].get('symbol'), current_price=data[i].get('current_price'), market_cap_rank=data[i].get('market_cap_rank'), market_cap=data[i].get('market_cap'), price_change_24h=data[i].get( 'price_change_percentage_24h'), price_change_7d=data[i].get( 'price_change_percentage_7d_in_currency'), image=data[i].get('image')) print("Added : ", new_coin) print("{}".format(new_coin.coin_id)) db.session.add(new_coin) db.session.commit() else: # Otherwise update the numbers for coins setattr(new_coin, 'current_price', data[i].get('current_price')) setattr(new_coin, 'market_cap_rank', data[i].get('market_cap_rank')) setattr(new_coin, 'market_cap', data[i].get('market_cap')) setattr(new_coin, 'price_change_24h', data[i].get('price_change_percentage_24h')) setattr( new_coin, 'price_change_7d', data[i].get('price_change_percentage_7d_in_currency')) db.session.commit() except Exception as e: db.session.rollback() db.session.flush() failed = True
def add_coin(): form = EditCoinForm() coin = Coin() if form.validate_on_submit(): fill_model_from_form(coin, form) db.session.add(coin) db.session.commit() flash('Монета {} добавлена'.format(coin.name)) return redirect_to_coin(coin) return render_template('edit-coin.html', coin=coin, form=form, coin_group_data=json.dumps(CoinGroup.get_all_hierarchical(with_parent_duplication=True), ensure_ascii=False))
def seed_coins(): bitcoin = Coin(name='Bitcoin', symbol='BTC') ethereum = Coin(name='Ethereum', symbol='ETH') bitcoincash = Coin(name='BitcoinCash', symbol='BCH') chainlink = Coin(name='Chainlink', symbol='LINK') litecoin = Coin(name='Litecoin', symbol='LTC') monero = Coin(name='Monero', symbol='XMR') graph = Coin(name='The Graph', symbol='GRT') waves = Coin(name='Waves', symbol='WAVES') db.session.add(bitcoin) db.session.add(ethereum) db.session.add(bitcoincash) db.session.add(chainlink) db.session.add(litecoin) db.session.add(monero) db.session.add(graph) db.session.add(waves) db.session.commit()
def seed_coins(): bitcoin = Coin(name='Bitcoin', symbol='BTC', maxsupply=21000000, circsupply=18700475) db.session.add(bitcoin) ethereum = Coin(name='Ethereum', symbol='ETH', maxsupply=0, circsupply=115748819) db.session.add(ethereum) dogecoin = Coin(name='Dogecoin', symbol='DOGE', maxsupply=0, circsupply=129475242412) db.session.add(dogecoin) ripple = Coin(name='Ripple', symbol='XRP', maxsupply=100000000000, circsupply=46030731641) db.session.add(ripple) cardano = Coin(name='Cardano', symbol='ADA', maxsupply=45000000000, circsupply=32061503736) db.session.add(cardano) uniswap = Coin(name='Uniswap Protocol Token', symbol='UNI', maxsupply=1000000000, circsupply=558378475) db.session.add(uniswap) litecoin = Coin(name='Litecoin', symbol='LTC', maxsupply=84000000, circsupply=66752415) db.session.add(litecoin) stellar = Coin(name='Stellar', symbol='XLM', maxsupply=50000000000, circsupply=23010708009) db.session.add(stellar) ethclassic = Coin(name='Ethereum Classic', symbol='ETC', maxsupply=210700000, circsupply=126909629) db.session.add(ethclassic) tron = Coin(name='TRON', symbol='TRX', maxsupply=100850743812, circsupply=71660220128) db.session.add(tron) aave = Coin(name='Aave', symbol='AAVE', maxsupply=16000000, circsupply=12735637) db.session.add(aave) cosmos = Coin(name='Cosmos', symbol='ATOM', maxsupply=0, circsupply=238526147) db.session.add(cosmos) db.session.commit()