def index(self): config = Config.get() app = config['app'] appStr = json.dumps(app, indent=4) if request.method == 'POST': content = request.form.get('config') newApp = json.loads(content) config['app'] = newApp Config.write(config) return redirect('admin/config') return self.render('admin/config.html', appStr = appStr)
def index(self): if request.method == 'POST': iid = request.form.get('iid') if not iid: return jsonify(code=10002, msg="IID Name cannot be empty") config = Config.get() tick = filter(lambda x: x not in '0123456789', iid) # add tick table if tick not in config['iids']: config['iids'].append(tick) Config.write(config) mysql = MySQL() sql = ''' CREATE TABLE IF NOT EXISTS `tick_%s` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `token` char(32) NOT NULL DEFAULT '', `iid` varchar(50) NOT NULL DEFAULT '', `time` char(17) NOT NULL DEFAULT '', `msec` int(11) NOT NULL DEFAULT '0', `price` decimal(10,2) NOT NULL DEFAULT '0.00', `volume` int(11) NOT NULL DEFAULT '0', `bid_price1` decimal(10,2) NOT NULL DEFAULT '0.00', `bid_volume1` int(11) NOT NULL DEFAULT '0', `ask_price1` decimal(10,2) NOT NULL DEFAULT '0.00', `ask_volume1` int(11) NOT NULL DEFAULT '0', `mtime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`), UNIQUE KEY idx_token(`token`), KEY idx_time(`time`) ) ENGINE=InnoDB CHARSET=utf8;''' % (tick) mysql.insert(sql) tickModel = createTickModel(tick) tmp_val = app._got_first_request app._got_first_request = False admin.add_view(TickView(tickModel, db.session, category='Tick')) app._got_first_request = tmp_val f = request.files['csv'] if not f: return jsonify(code=0, msg="create iid success") tmpPath = '/tmp/%s' % (f.filename) f.save(tmpPath) csv_reader = csv.reader(open(tmpPath)) isTitle = True total = succ = 0 mysql = MySQL() for row in csv_reader: if isTitle: if not self.checkTitle(row): return jsonify( code=10001, msg= "Error, title should be ['time', 'msec', 'price', 'volume', 'bid_price1', 'bid_volume1', 'ask_price1', 'ask_volume1']" ) isTitle = False continue total += 1 if self.insertTickData(mysql, iid, row): succ += 1 return jsonify(code=0, msg='ok', data={ 'total': total, 'succ': succ }) return self.render('admin/tick_upload.html')