Esempio n. 1
0
def new_trade_csv():
    ntcForm = NewTradeCsvForm()
    if ntcForm.validate_on_submit():
        for row in ntcForm.csv.data.splitlines():
            split = [i.title() for i in row.split(',')]
            t_moves = split[11:]
            for i in range(4 - len(t_moves)):
                t_moves.append(None)
            species_list = [i[0].split(',')[1].title() for i in national_dex]
            data = {
                'dex_no': species_list.index(split[0]) + 1,
                'species': split[0],
                'male': split[1] == 'True',
                'female': split[2] == 'True',
                'nature': split[3],
                'ability': split[4],
                'iv_hp': split[5],
                'iv_atk': split[6],
                'iv_def': split[7],
                'iv_spa': split[8],
                'iv_spd': split[9],
                'iv_spe': split[10],
                'move1': t_moves[0],
                'move2': t_moves[1],
                'move3': t_moves[2],
                'move4': t_moves[3]
            }
            trade = Trade(owner=g.user, data=data)
            td = trade.__dict__.copy()
            del td['_sa_instance_state']
            if Trade.query.filter_by(**td).first() is None:
                db.session.add(trade)
                db.session.commit()
                flash('Your {} was successfully added'.format(split[0]), 'success')
                status = '{0} just added a {1} {2} {3} ({4}) {5}'.format(
                    g.user.nickname,
                    trade.nature,
                    trade.ability,
                    trade.species,
                    trade.ivSpread(),
                    url_for('user', nickname=g.user.nickname, _external=True)
                )
                api.PostUpdate(status)
            else:
                flash('You have already added this trade', 'error')
    return redirect(request.args.get('next') or url_for('user', nickname=g.user.nickname))
Esempio n. 2
0
def new_trade():
    ntForm = NewTradeForm()
    if ntForm.validate_on_submit():
        trade = Trade(owner=g.user, data=ntForm.data)
        td = trade.__dict__.copy()
        del td['_sa_instance_state']
        if Trade.query.filter_by(**td).first() is None:
            db.session.add(trade)
            db.session.commit()
            flash('Your {} was successfully added'.format(ntForm.species.data.split(',')[1]), 'success')
            status = '{0} just added a {1} {2} {3} ({4}) {5}'.format(
                g.user.nickname,
                trade.nature,
                trade.ability,
                trade.species,
                trade.ivSpread(),
                url_for('user', nickname=g.user.nickname, _external=True)
            )
            api.PostUpdate(status)
        else:
            flash('You have already added this trade', 'error')
    return redirect(request.args.get('next') or url_for('user', nickname=g.user.nickname))