moving to p3

This commit is contained in:
Ville Rantanen
2020-03-27 11:47:33 +02:00
parent 766aa35826
commit c042cdd04d

45
shop.py
View File

@@ -24,18 +24,18 @@ app.config.from_object(__name__)
app.config['SESSION_COOKIE_NAME'] = os.getenv('SESSION_COOKIE_NAME', 'mdshop') app.config['SESSION_COOKIE_NAME'] = os.getenv('SESSION_COOKIE_NAME', 'mdshop')
app.config['register'] = os.getenv('ENABLE_REGISTER', 'true') != 'false' app.config['register'] = os.getenv('ENABLE_REGISTER', 'true') != 'false'
print(app.config) print(app.config)
app.wsgi_app = ReverseProxied(app.wsgi_app) app.wsgi_app = ReverseProxied(app.wsgi_app)
def connect_db(): def connect_db():
if not os.path.exists(app.config['DATABASE']): if not os.path.exists(app.config['DATABASE']):
db=sqlite3.connect(app.config['DATABASE']) db=sqlite3.connect(app.config['DATABASE'])
for command in open('schema.sql','rt').read().split(';'): for command in open('schema.sql','rt').read().split(';'):
db.execute(command) db.execute(command)
db.commit() db.commit()
return sqlite3.connect(app.config['DATABASE']) return sqlite3.connect(app.config['DATABASE'])
def password_hash(s): def password_hash(s):
return hashlib.sha224(s).hexdigest() return hashlib.sha224(s.encode('utf8')).hexdigest()
def get_username(id): def get_username(id):
cur = g.db.execute('select * from users') cur = g.db.execute('select * from users')
@@ -105,6 +105,7 @@ def scan_for_new_documents(id):
g.db.commit() g.db.commit()
def markdown_parse(s): def markdown_parse(s):
s = s.decode('utf8')
s=BOLDFINDER.sub(r'*<span class="md_bold">\1</span>*',s) s=BOLDFINDER.sub(r'*<span class="md_bold">\1</span>*',s)
s=CODEFINDER.sub(r'`<span class="md_code">\1</span>`',s) s=CODEFINDER.sub(r'`<span class="md_code">\1</span>`',s)
return s return s
@@ -147,8 +148,8 @@ def show_shop(shopid):
if not os.path.exists(data_file): if not os.path.exists(data_file):
open(data_file, 'wt').close() open(data_file, 'wt').close()
entries=[] entries=[]
content=open(data_file, 'rt').read().decode('utf-8') content=open(data_file, 'rt').read()#.decode('utf-8')
for i,row in enumerate(open( data_file, 'rt').read().decode('utf-8').split("\n")): for i,row in enumerate(open( data_file, 'rt').read().split("\n")):
# any parsing magick would be here # any parsing magick would be here
row=row.rstrip() row=row.rstrip()
if row=="": if row=="":
@@ -180,10 +181,10 @@ def show_shop(shopid):
session['sort_update'] = time.time() session['sort_update'] = time.time()
return render_template( return render_template(
'show_shop.html', 'show_shop.html',
entries = entries, entries = entries,
shop = shopname, shop = shopname,
shopid = shopid, shopid = shopid,
content = content, content = content,
shares = shared_to, shares = shared_to,
date = get_shop_date(shopid), date = get_shop_date(shopid),
@@ -208,7 +209,7 @@ def list_shops():
if row[0] in shares: # Has been shared to if row[0] in shares: # Has been shared to
date=get_shop_date(row[0]) date=get_shop_date(row[0])
entries.append( dict(shop=row[1], shopid=row[0], owner=get_username(row[2]),date=date) ) entries.append( dict(shop=row[1], shopid=row[0], owner=get_username(row[2]),date=date) )
return render_template('list_shops.html', entries=entries) return render_template('list_shops.html', entries=entries)
@app.route('/add', methods=['POST']) @app.route('/add', methods=['POST'])
@@ -227,7 +228,7 @@ def add_items():
if row.strip()=="": if row.strip()=="":
continue continue
count+=1 count+=1
contents_file.write("[ ] %s\n"%row.strip().encode('utf-8')) contents_file.write("[ ] %s\n"%row.strip())
contents_file.close() contents_file.close()
flash('Added %d items'%(count)) flash('Added %d items'%(count))
return redirect(url_for('show_shop',shopid=shopid)) return redirect(url_for('show_shop',shopid=shopid))
@@ -249,7 +250,7 @@ def edit_md():
while contents_list[-1].strip()=='': while contents_list[-1].strip()=='':
contents_list.pop() contents_list.pop()
for row in contents_list: for row in contents_list:
contents_file.write("%s\n"%row.encode('utf-8')) contents_file.write("%s\n"%(row,))
contents_file.close() contents_file.close()
flash('Saved new file.') flash('Saved new file.')
return redirect(url_for('show_shop',shopid=shopid)) return redirect(url_for('show_shop',shopid=shopid))
@@ -299,7 +300,7 @@ def toggle_item():
data_file=os.path.join(data_dir, shopname+".md") data_file=os.path.join(data_dir, shopname+".md")
backup=data_file+".bkp" backup=data_file+".bkp"
contents_file=open(data_file,'rt') contents_file=open(data_file,'rt')
contents=contents_file.read().decode('utf-8').split("\n") contents=contents_file.read().split("\n")
contents_file.close() contents_file.close()
changed=False changed=False
for i,row in enumerate(contents): for i,row in enumerate(contents):
@@ -315,7 +316,7 @@ def toggle_item():
if req_row==-1 or req_row==-2: if req_row==-1 or req_row==-2:
copyfile(data_file, backup) copyfile(data_file, backup)
contents_file=open(data_file,'wt') contents_file=open(data_file,'wt')
contents_file.write("\n".join(contents).encode('utf-8')) contents_file.write("\n".join(contents))
contents_file.close() contents_file.close()
return redirect(url_for('show_shop',shopid=shopid)) return redirect(url_for('show_shop',shopid=shopid))
@@ -333,7 +334,7 @@ def remove_toggled():
contents_file=open(data_file,'rt') contents_file=open(data_file,'rt')
contents=[] contents=[]
changed=False changed=False
for i,row in enumerate(contents_file.read().decode('utf-8').split("\n")): for i,row in enumerate(contents_file.read().split("\n")):
if '[x]' not in row: if '[x]' not in row:
contents.append(row) contents.append(row)
else: else:
@@ -342,7 +343,7 @@ def remove_toggled():
if changed: if changed:
copyfile(data_file, backup) copyfile(data_file, backup)
contents_file=open(data_file,'wt') contents_file=open(data_file,'wt')
contents_file.write("\n".join(contents).encode('utf-8')) contents_file.write("\n".join(contents))
contents_file.close() contents_file.close()
#~ flash('successfully posted %s (%d)'%(row,req_row)) #~ flash('successfully posted %s (%d)'%(row,req_row))
return redirect(url_for('show_shop',shopid=shopid)) return redirect(url_for('show_shop',shopid=shopid))
@@ -417,7 +418,7 @@ def remove_share():
if session.get('user')!=ownerid: if session.get('user')!=ownerid:
flash('Not your shop!') flash('Not your shop!')
return redirect(url_for('show_shop',shopid=shopid)) return redirect(url_for('show_shop',shopid=shopid))
g.db.execute('delete from shares where shopid=? and userid=?', g.db.execute('delete from shares where shopid=? and userid=?',
[shopid, userid]) [shopid, userid])
g.db.commit() g.db.commit()
@@ -432,10 +433,10 @@ def remove_shop():
shopname=g.db.execute('select shop from shops where id=?',(request.form['shopid'],)).fetchall()[0][0] shopname=g.db.execute('select shop from shops where id=?',(request.form['shopid'],)).fetchall()[0][0]
ownername=get_username(ownerid) ownername=get_username(ownerid)
data_dir=os.path.join(DATADIR, ownername) data_dir=os.path.join(DATADIR, ownername)
data_file=os.path.join(data_dir, shopname+".md") data_file=os.path.join(data_dir, shopname+".md")
if session.get('user')!=ownerid: if session.get('user')!=ownerid:
flash('Not your shop!') flash('Not your shop!')
return redirect(url_for('show_shop',shopid=shopid)) return redirect(url_for('show_shop',shopid=shopid))
# remove shop DB # remove shop DB
g.db.execute('delete from shops where id=?', g.db.execute('delete from shops where id=?',
[shopid]) [shopid])
@@ -462,7 +463,7 @@ def sort_flip():
shopid = int(request.form['shopid']) shopid = int(request.form['shopid'])
return redirect( return redirect(
url_for('show_shop', shopid = shopid) url_for('show_shop', shopid = shopid)
) )
@app.route('/login', methods=['GET', 'POST']) @app.route('/login', methods=['GET', 'POST'])
def login(): def login():
@@ -487,7 +488,7 @@ def register():
if request.method == 'POST': if request.method == 'POST':
import re, string import re, string
pattern = re.compile('[\W]+') pattern = re.compile('[\W]+')
username=pattern.sub('', request.form['username']) username=pattern.sub('', request.form['username'])
password=password_hash(request.form['password']) password=password_hash(request.form['password'])
if len(username)==0: if len(username)==0:
error="No username given" error="No username given"
@@ -535,5 +536,5 @@ def logout():
return render_template('login.html', error=None) return render_template('login.html', error=None)
if __name__ == '__main__': if __name__ == '__main__':
app.run() app.run()