From 271180a50e889f1e8b8c04264d093f01bd622e3b Mon Sep 17 00:00:00 2001 From: schneefux Date: Fri, 10 Jun 2016 19:23:12 +0200 Subject: move methods around --- app.py | 98 +++++++++++++++++++++++++++++++++--------------------------------- 1 file changed, 49 insertions(+), 49 deletions(-) diff --git a/app.py b/app.py index 5dd3b3c..ffebdc5 100644 --- a/app.py +++ b/app.py @@ -39,6 +39,35 @@ class dbProxy(object): self.tagmark = "_" + def close(self): + self.conn.close() + + def database_v(self): + versions = { + 'jokes': '0', + 'v1_jokes': '1', + 'v1a_jokes': '1a', + 'v1b_jokes': '1b' + } + for ver in versions: + if self.c.execute("SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name=?", (ver,)).fetchone()['COUNT(*)'] == 1: + return versions[ver] + return '-1' + + def dict_factory(self, cursor, row): + d = {} + for idx, col in enumerate(cursor.description): + d[col[0]] = row[idx] + return d + + def prettifyText(self, text): + html = re.sub('<[^<]+?>', '', text) + html = re.sub(r"/(\w+)/", '\\1', html) + html = re.sub(r"\*(\w+)\*", '\\1', html) + html = re.sub(r"#(\w+)", '#\\1', html) + html = html.replace("\n", "
") + return html + def create_v0(self): app.logger.warning("creating new v0 database") self.c.execute("CREATE TABLE jokes(id INTEGER PRIMARY KEY NOT NULL, text TEXT, upvotes INTEGER, downvotes INTEGER, reports INTEGER)") @@ -62,24 +91,6 @@ class dbProxy(object): def create_v1b(self): create_v1a() - def migrate_v1ato1b(self): - app.logger.warning("migrating database from v1a to v1b") - self.c.execute("ALTER TABLE v1a_jokes RENAME TO v1b_jokes") - self.c.execute("ALTER TABLE v1a_votes RENAME TO v1b_votes") - self.c.execute("ALTER TABLE v1a_users RENAME TO v1b_users") - self.c.execute("UPDATE v1b_jokes SET format='prettytext' WHERE format='markdown'") - self.c.execute("UPDATE v1b_votes SET type='down' WHERE type='report'") - self.conn.commit() - - def migrate_v1to1a(self): - app.logger.warning("migrating database from v1 to v1a") - self.c.execute("ALTER TABLE v1_jokes RENAME TO v1a_jokes") - self.c.execute("ALTER TABLE v1_votes RENAME TO v1a_votes") - self.c.execute("CREATE TABLE v1a_users(id INTEGER PRIMARY KEY, identifier TEXT, role TEXT DEFAULT 'guest', password TEXT DEFAULT '', salt TEXT DEFAULT '')") - self.c.execute("INSERT INTO v1a_users(id, identifier) SELECT id, identifier FROM v1_users") - self.c.execute("DROP TABLE v1_users") - self.conn.commit() - def migrate_v0to1(self): app.logger.warning("migrating database from v0 to v1") self.create_v1() @@ -133,34 +144,23 @@ class dbProxy(object): # write self.conn.commit() - def database_v(self): - versions = { - 'jokes': '0', - 'v1_jokes': '1', - 'v1a_jokes': '1a', - 'v1b_jokes': '1b' - } - for ver in versions: - if self.c.execute("SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name=?", (ver,)).fetchone()['COUNT(*)'] == 1: - return versions[ver] - return '-1' - - def close(self): - self.conn.close() - - def dict_factory(self, cursor, row): - d = {} - for idx, col in enumerate(cursor.description): - d[col[0]] = row[idx] - return d + def migrate_v1to1a(self): + app.logger.warning("migrating database from v1 to v1a") + self.c.execute("ALTER TABLE v1_jokes RENAME TO v1a_jokes") + self.c.execute("ALTER TABLE v1_votes RENAME TO v1a_votes") + self.c.execute("CREATE TABLE v1a_users(id INTEGER PRIMARY KEY, identifier TEXT, role TEXT DEFAULT 'guest', password TEXT DEFAULT '', salt TEXT DEFAULT '')") + self.c.execute("INSERT INTO v1a_users(id, identifier) SELECT id, identifier FROM v1_users") + self.c.execute("DROP TABLE v1_users") + self.conn.commit() - def prettifyText(self, text): - html = re.sub('<[^<]+?>', '', text) - html = re.sub(r"/(\w+)/", '\\1', html) - html = re.sub(r"\*(\w+)\*", '\\1', html) - html = re.sub(r"#(\w+)", '#\\1', html) - html = html.replace("\n", "
") - return html + def migrate_v1ato1b(self): + app.logger.warning("migrating database from v1a to v1b") + self.c.execute("ALTER TABLE v1a_jokes RENAME TO v1b_jokes") + self.c.execute("ALTER TABLE v1a_votes RENAME TO v1b_votes") + self.c.execute("ALTER TABLE v1a_users RENAME TO v1b_users") + self.c.execute("UPDATE v1b_jokes SET format='prettytext' WHERE format='markdown'") + self.c.execute("UPDATE v1b_votes SET type='down' WHERE type='report'") + self.conn.commit() def getJokes(self, user=None, filter=None, sortby='rank'): # user: return with user-specific attributes, also return deleted jokes @@ -314,10 +314,6 @@ def close_db(exception): if db is not None: db.close() -@app.route('/') -def root(): - return page(0) - def userid(): if 'userlogin' in session: uid = db().getUser(name=session['userlogin']) @@ -327,6 +323,10 @@ def userid(): session['guestlogin'] = os.urandom(32).hex() return db().getUser(cookie=session['guestlogin']) +@app.route('/') +def root(): + return page(0) + @app.route('/page/') def page(num): filter = request.args.get('filter') -- cgit v1.3.1