From 19f86600957d952963d20586ff9ea15c4639dfce Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sat, 7 Aug 2010 17:10:15 +0200 Subject: [PATCH] Create a global webapp database connection and create a config class. --- www/webapp/datastore/__init__.py | 7 ++++++- www/webapp/datastore/config.py | 17 ++++++++++++++++- www/webapp/datastore/connections.py | 13 +++++++++---- www/webapp/handlers.py | 23 ++++++----------------- 4 files changed, 37 insertions(+), 23 deletions(-) diff --git a/www/webapp/datastore/__init__.py b/www/webapp/datastore/__init__.py index de5051fd..224f3242 100644 --- a/www/webapp/datastore/__init__.py +++ b/www/webapp/datastore/__init__.py @@ -15,8 +15,12 @@ class Databases(object): def __init__(self, application): self.application = application + self.webapp = WebappConnection() + + self.config = self.webapp self.hashes = HashConnection() - self.planet = PlanetConnection() + self.mirrors = self.webapp + self.planet = self.webapp self.tracker = TrackerConnection() @@ -29,6 +33,7 @@ class DataStore(object): def reload(self): self.banners = banners.Banners(self.application, "banners.json") + self.config = config.Config(self.application) # self.info = info.Info(self.application, "info.json") self.menu = menu.Menu(self.application, "menu.json") # self.mirrors = mirrors.Mirrors(self.application, "mirrors.json") diff --git a/www/webapp/datastore/config.py b/www/webapp/datastore/config.py index b44d0fa6..961164f3 100644 --- a/www/webapp/datastore/config.py +++ b/www/webapp/datastore/config.py @@ -1,4 +1,19 @@ #/usr/bin/python class Config(object): - pass + def __init__(self, application): + self.application = application + + @property + def db(self): + return self.application.db.config + + def delete(self, key): + self.db.execute("DELETE FROM settings WHERE key = %s", key) + + def get(self, key, default=None): + return self.db.get("SELECT key FROM settings WHERE key = %s", key) or default + + def set(self, key, value): + self.delete(key) + self.db.execute("INSERT INTO settings(key, value) VALUES(%s, %s)", key, value) diff --git a/www/webapp/datastore/connections.py b/www/webapp/datastore/connections.py index 34a2089e..259dc39b 100644 --- a/www/webapp/datastore/connections.py +++ b/www/webapp/datastore/connections.py @@ -4,16 +4,21 @@ import tornado.database MYSQL_SERVER = "mysql.ipfire.org" -class HashConnection(tornado.database.Connection): +class WebappConnection(tornado.database.Connection): def __init__(self): - tornado.database.Connection.__init__(self, MYSQL_SERVER, "hashes", user="webapp") + tornado.database.Connection.__init__(self, MYSQL_SERVER, "webapp", user="webapp") -class PlanetConnection(tornado.database.Connection): +class HashConnection(tornado.database.Connection): def __init__(self): - tornado.database.Connection.__init__(self, MYSQL_SERVER, "planet", user="webapp") + tornado.database.Connection.__init__(self, MYSQL_SERVER, "hashes", user="webapp") class TrackerConnection(tornado.database.Connection): def __init__(self): tornado.database.Connection.__init__(self, MYSQL_SERVER, "tracker", user="webapp") + + +class UserConnection(tornado.database.Connection): + def __init__(self): + tornado.database.Connection.__init__(self, MYSQL_SERVER, "forum", user="webapp") diff --git a/www/webapp/handlers.py b/www/webapp/handlers.py index eaa5ec12..6b145602 100644 --- a/www/webapp/handlers.py +++ b/www/webapp/handlers.py @@ -84,7 +84,7 @@ class BaseHandler(tornado.web.RequestHandler): @property def db(self): - return self.application.db + return self.ds.db @property def ds(self): @@ -220,15 +220,6 @@ class BuildHandler(BaseHandler): self.render("builds.html", builds=self.builds) -class UrielBaseHandler(BaseHandler): - #db = uriel.Database() - pass - -class UrielHandler(UrielBaseHandler): - def get(self): - pass - - class SourceHandler(BaseHandler): def get(self): source_path = "/srv/sources" @@ -418,14 +409,12 @@ class TrackerScrapeHandler(TrackerBaseHandler): class PlanetBaseHandler(BaseHandler): - @property - def db(self): - return self.db.planet + pass class PlanetMainHandler(PlanetBaseHandler): def get(self): - authors = self.db.query("SELECT DISTINCT author_id FROM entries") + authors = self.db.query("SELECT DISTINCT author_id FROM planet") authors = [a["author_id"] for a in authors] users = [] @@ -433,7 +422,7 @@ class PlanetMainHandler(PlanetBaseHandler): if user.id in authors: users.append(user) - entries = self.db.query("SELECT * FROM entries " + entries = self.db.query("SELECT * FROM planet " "ORDER BY published DESC LIMIT 3") for entry in entries: @@ -449,7 +438,7 @@ class PlanetUserHandler(PlanetBaseHandler): user = self.user_db.get_user_by_name(user) - entries = self.db.query("SELECT * FROM entries " + entries = self.db.query("SELECT * FROM planet " "WHERE author_id = '%s' ORDER BY published DESC" % (user.id)) self.render("planet-user.html", entries=entries, user=user) @@ -457,7 +446,7 @@ class PlanetUserHandler(PlanetBaseHandler): class PlanetPostingHandler(PlanetBaseHandler): def get(self, slug): - entry = self.db.get("SELECT * FROM entries WHERE slug = %s", slug) + entry = self.db.get("SELECT * FROM planet WHERE slug = %s", slug) if not entry: raise tornado.web.HTTPError(404) -- 2.47.3