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