139 lines
3.3 KiB
Python
139 lines
3.3 KiB
Python
|
import sqlite3
|
||
|
from datetime import datetime, timedelta
|
||
|
|
||
|
from flask import request
|
||
|
|
||
|
db_path = './data.sqlite3'
|
||
|
|
||
|
db = sqlite3.connect(db_path)
|
||
|
cursor = db.cursor()
|
||
|
try:
|
||
|
cursor.execute('''
|
||
|
create table if not exists user_menu
|
||
|
(
|
||
|
user text,
|
||
|
menu text,
|
||
|
datestr text,
|
||
|
primary key (user, datestr)
|
||
|
)
|
||
|
''')
|
||
|
cursor.execute('''
|
||
|
create table if not exists roll_result
|
||
|
(
|
||
|
datestr text primary key,
|
||
|
value text
|
||
|
)
|
||
|
''')
|
||
|
cursor.execute('''
|
||
|
create table if not exists menu
|
||
|
(
|
||
|
name text primary key,
|
||
|
label text
|
||
|
)
|
||
|
''')
|
||
|
db.commit()
|
||
|
finally:
|
||
|
cursor.close()
|
||
|
db.close()
|
||
|
|
||
|
|
||
|
def get_user():
|
||
|
client_ip = request.remote_addr
|
||
|
return client_ip
|
||
|
|
||
|
|
||
|
def get_user_menu():
|
||
|
user = get_user()
|
||
|
db = sqlite3.connect(db_path)
|
||
|
cursor = db.cursor()
|
||
|
datestr = datetime.now().strftime('%Y-%m-%d')
|
||
|
try:
|
||
|
cursor.execute("select menu from user_menu where user = ? and datestr=?", (user, datestr))
|
||
|
row = cursor.fetchone()
|
||
|
if row:
|
||
|
return row[0]
|
||
|
else:
|
||
|
cursor.execute("select menu from user_menu where user = ? order by datestr desc limit 1", (user,))
|
||
|
row = cursor.fetchone()
|
||
|
if row:
|
||
|
return row[0]
|
||
|
return ''
|
||
|
finally:
|
||
|
cursor.close()
|
||
|
db.close()
|
||
|
|
||
|
|
||
|
def set_user_menu(menu, user=None):
|
||
|
datestr = datetime.now().strftime('%Y-%m-%d')
|
||
|
if user is None:
|
||
|
user = get_user()
|
||
|
db = sqlite3.connect(db_path)
|
||
|
cursor = db.cursor()
|
||
|
try:
|
||
|
cursor.execute("insert or replace into user_menu(user,menu,datestr) values(?,?,?)",
|
||
|
(user, menu, datestr))
|
||
|
db.commit()
|
||
|
finally:
|
||
|
cursor.close()
|
||
|
db.close()
|
||
|
|
||
|
|
||
|
def fetch_all_today_menu():
|
||
|
datestr = datetime.now().strftime('%Y-%m-%d')
|
||
|
db = sqlite3.connect(db_path)
|
||
|
cursor = db.cursor()
|
||
|
try:
|
||
|
cursor.execute("select user, menu from user_menu where menu != '' and datestr=? order by user",
|
||
|
(datestr,))
|
||
|
row = cursor.fetchone()
|
||
|
while row:
|
||
|
yield row[0], row[1]
|
||
|
row = cursor.fetchone()
|
||
|
finally:
|
||
|
cursor.close()
|
||
|
db.close()
|
||
|
|
||
|
|
||
|
def fetch_all_menu():
|
||
|
db = sqlite3.connect(db_path)
|
||
|
cursor = db.cursor()
|
||
|
try:
|
||
|
cursor.execute("select name, label from menu order by order_num")
|
||
|
row = cursor.fetchone()
|
||
|
while row:
|
||
|
yield row[0], row[1]
|
||
|
row = cursor.fetchone()
|
||
|
finally:
|
||
|
cursor.close()
|
||
|
db.close()
|
||
|
|
||
|
|
||
|
def fetch_roll_result(interval=0):
|
||
|
date = datetime.now() + timedelta(days=interval)
|
||
|
datestr = date.strftime('%Y-%m-%d')
|
||
|
db = sqlite3.connect(db_path)
|
||
|
cursor = db.cursor()
|
||
|
try:
|
||
|
cursor.execute("select value from roll_result where datestr=?",
|
||
|
(datestr,))
|
||
|
row = cursor.fetchone()
|
||
|
if row:
|
||
|
return row[0]
|
||
|
return None
|
||
|
finally:
|
||
|
cursor.close()
|
||
|
db.close()
|
||
|
|
||
|
|
||
|
def set_roll_result(value):
|
||
|
datestr = datetime.now().strftime('%Y-%m-%d')
|
||
|
db = sqlite3.connect(db_path)
|
||
|
cursor = db.cursor()
|
||
|
try:
|
||
|
cursor.execute("insert or replace into roll_result(datestr, value) values(?,?)",
|
||
|
(datestr, value))
|
||
|
db.commit()
|
||
|
finally:
|
||
|
cursor.close()
|
||
|
db.close()
|