def user_loader(email): db_user = db_helper.query('SELECT * FROM users WHERE username = ?', [email], one=True) if not db_user: return user = User() user.id = db_user.get('username') user.role = db_user.get('role') user.customer_id = db_user.get('customer_id') print(user) return user
def report(): if request.method == 'GET': context = { 'timestamp': datetime.now(), 'customers': db_helper.query('SELECT * FROM customers ORDER BY name'), } if 'message' in session: context['message'] = session.pop('message') if 'error' in session: context['error'] = session.pop('error') return render_template('report-create.html', **context) customer_id = request.form['customer-id'] units = request.form['units'] if customer_id and units: db_helper.execute('INSERT INTO reports (customer_id, units, datetime) VALUES (?, ?, datetime(CURRENT_TIMESTAMP,"localtime"))', [customer_id, units]) customer_name = db_helper.query('SELECT name FROM customers WHERE id = ?', [customer_id])[0].get('name') session['message'] = "Added %s units to %s" % (units, customer_name) return redirect(url_for('report')) else: session['error'] = "Both customer and units must be chosen" return redirect(url_for('report'))
def reports(): context = { 'customers': db_helper.query('SELECT * FROM customers ORDER BY name') } print("a message? '%s'" % context.get("message")) if request.method == 'GET' and current_user.can_list_all_reports(): return render_template('report-list.html', **context) if current_user.can_list_own_reports(): customer_id = current_user.customer_id else: customer_id = request.form['customer-id'] reports = db_helper.query('SELECT * FROM reports WHERE customer_id = ? ORDER BY datetime DESC', [customer_id]) meta = db_helper.query('SELECT COUNT(1) AS count, SUM(units) as total FROM reports WHERE customer_id = ?', [customer_id], one=True) customer_name = db_helper.query('SELECT name FROM customers WHERE id = ?', [customer_id], one=True) context['reports'] = reports context['customer_name'] = customer_name.get("name") context['total'] = meta.get("total") context['count'] = meta.get("count") if not len(reports): context['message'] = "No reports to display" return render_template('report-list.html', **context)
def queryrecord(opcode, tcode, qiu, dname): idatestr = input('请输入日期 (格式为2019-06-01 00:00:00) ,当前时间请输入d ,不输入时间默认为0点:') if idatestr.upper() == 'D': idate = datetime.now().strftime('%Y-%m-%d %H:%M:%S') elif idatestr.upper() == 'Y': a = datetime.now().strftime('%Y-%m-%d %H:%M:%S') idate = a.replace(year=a.year, month=1, day=1) else: it = bool(re.search('\d{1,2}:\d{1,2}:\d{1,2}&', idatestr)) if it == False: idate = datetime.strptime(idatestr, '%Y-%m-%d').strftime( '%Y-%m-%d') + ' 00:00:00' #time.strptime(idatestr,'%Y-%m-%d') else: idate = datetime.strptime( idatestr, '%Y-%m-%d %H:%M:%S').strftime('%Y-%m-%d %H:%M:%S') if opcode == 0: endate = None else: if (qiu == 'Q' or qiu == 'D'): endt = input( '请输入截止日期 (格式为2019-12-01默认截止到该日期23:59:59) ,输入d当前日期,查询可用请按0跳过:\n>>>>' ) if endt == 'd': endate = datetime.now().strftime('%Y-%m-%d') + ' 23:59:59' elif endt == '0': endate = None else: endate = datetime.strptime( endt, '%Y-%m-%d').strftime('%Y-%m-%d') + ' 23:59:59' if qiu == 'I': endate = None col, data = query(date=idate, edate=endate, ctype=opcode, itype=tcode, dbname=dname) return (col, data, idate, endate)
def get_customers(): customers = db_helper.query('SELECT * FROM customers') return str(customers)