From eeb3266dade3b0cd7d30ff35c6ccd0336bc0c1d9 Mon Sep 17 00:00:00 2001 From: Andrew Williams Date: Thu, 4 Nov 2010 11:47:19 +0000 Subject: [PATCH] Switch SSO Service API layer to use the new Django 1.2 ORM layer --- dbsettings.py.example | 14 ++++++++------ sso/services/__init__.py | 18 +----------------- sso/services/miningbuddy/__init__.py | 12 ++++++------ sso/services/phpbb/__init__.py | 11 +++++------ sso/services/postracker/__init__.py | 10 +++++----- sso/services/qms/__init__.py | 8 ++++---- sso/services/wiki/__init__.py | 9 ++++----- 7 files changed, 33 insertions(+), 49 deletions(-) diff --git a/dbsettings.py.example b/dbsettings.py.example index b509e1f..c257db3 100644 --- a/dbsettings.py.example +++ b/dbsettings.py.example @@ -1,6 +1,8 @@ -DATABASE_ENGINE = 'sqlite3' # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'. -DATABASE_NAME = 'dreddit_sso.db' # Or path to database file if using sqlite3. -DATABASE_USER = '' # Not used with sqlite3. -DATABASE_PASSWORD = '' # Not used with sqlite3. -DATABASE_HOST = '' # Set to empty string for localhost. Not used with sqlite3. -DATABASE_PORT = '' # Set to empty string for default. Not used with sqlite3. +DATABASES = { + 'default': { + 'NAME': 'dreddit_sso.db', + 'ENGINE': 'django.db.backends.sqlite3', + 'USER': '', + 'PASSWORD': '', + } +} diff --git a/sso/services/__init__.py b/sso/services/__init__.py index 3edce28..936057e 100644 --- a/sso/services/__init__.py +++ b/sso/services/__init__.py @@ -73,17 +73,7 @@ class BaseDBService(BaseService): @property def db(self): if not hasattr(self, '_db'): - # Use the master DB settings, bar the database name - backend = load_backend(settings.DATABASE_ENGINE) - self._db = backend.DatabaseWrapper({ - 'DATABASE_HOST': settings.DATABASE_HOST, - 'DATABASE_NAME': self.settings['database_name'], - 'DATABASE_OPTIONS': {}, - 'DATABASE_PASSWORD': settings.DATABASE_PASSWORD, - 'DATABASE_PORT': settings.DATABASE_PORT, - 'DATABASE_USER': settings.DATABASE_USER, - 'TIME_ZONE': settings.TIME_ZONE,}) - + self._db = connections[self.settings['database_name']] return self._db @property @@ -92,9 +82,3 @@ class BaseDBService(BaseService): self._dbcursor = self.db.cursor() return self._dbcursor - def __del__(self): - if hasattr(self, '_db'): - self.db.connection.commit() - self.db.close() - self.db = None - diff --git a/sso/services/miningbuddy/__init__.py b/sso/services/miningbuddy/__init__.py index 74b2565..84623bd 100644 --- a/sso/services/miningbuddy/__init__.py +++ b/sso/services/miningbuddy/__init__.py @@ -1,7 +1,7 @@ import crypt import random import time -from django.db import load_backend, transaction +from django.db import transaction from sso.services import BaseDBService import settings @@ -46,12 +46,12 @@ class MiningBuddyService(BaseDBService): email = '' self.dbcursor.execute(self.SQL_ADD_USER, [self._clean_username(username), pwhash, email]) - self.db.connection.commit() + transaction.set_dirty() userid = self.dbcursor.lastrowid api = kwargs['character'].eveaccount_set.all()[0] self.dbcursor.execute(self.SQL_ADD_API, [userid, int(time.time()), api.api_user_id, api.api_key, kwargs['character'].id]) - self.db.connection.commit() + transaction.set_dirty() return { 'username': self._clean_username(username), 'password': password } @@ -66,20 +66,20 @@ class MiningBuddyService(BaseDBService): def delete_user(self, uid): """ Delete a user """ self.dbcursor.execute(self.SQL_DEL_USER, [uid]) - self.db.connection.commit() + transaction.set_dirty() return True def disable_user(self, uid): """ Disable a user """ self.dbcursor.execute(self.SQL_DIS_USER, [uid]) - self.db.connection.commit() + transaction.set_dirty() return True def enable_user(self, uid, password): """ Enable a user """ pwhash = self._gen_mb_hash(password) self.dbcursor.execute(self.SQL_ENABLE_USER, [pwhash, uid]) - self.db.connection.commit() + transaction.set_dirty() return True def reset_password(self, uid, password): diff --git a/sso/services/phpbb/__init__.py b/sso/services/phpbb/__init__.py index de6aea5..4db136e 100644 --- a/sso/services/phpbb/__init__.py +++ b/sso/services/phpbb/__init__.py @@ -41,7 +41,7 @@ class PhpBBService(BaseDBService): pwhash = self._gen_hash(password) self._dbcursor.execute(self.SQL_ADD_USER, [username, pwhash, email]) - self._db.connection.commit() + transaction.set_dirty() self.update_groups(username) return { 'username': username, 'password': password } @@ -56,12 +56,12 @@ class PhpBBService(BaseDBService): row = self._dbcursor.fetchone() if not row: self._dbcursor.execute(self.SQL_ADD_GROUP, [group.name]) - self._db.connection.commit() + transaction.set_dirty() self._dbcursor.execute(self.SQL_GET_GROUP, [group.name]) row = self._dbcursor.fetchone() self._dbcursor.execute(self.SQL_ADD_USER_GROUP, [row['group_id'], user_id]) - self._db.connection.commit() + transaction.set_dirty() def check_user(self, username): """ Check if the username exists """ @@ -83,16 +83,15 @@ class PhpBBService(BaseDBService): except IntegrityError: # Record already exists, skip it pass - self._db.connection.commit() + transaction.set_dirty() return True def enable_user(self, uid, password): """ Enable a user """ pwhash = self._gen_mw_hash(password) self._dbcursor.execute(self.SQL_ENABLE_USER, [pwhash, uid]) - self._db.connection.commit() self._dbcursor.execute(self.SQL_ENABLE_GROUP, [uid]) - self._db.connection.commit() + transaction.set_dirty() return True def reset_password(self, uid, password): diff --git a/sso/services/postracker/__init__.py b/sso/services/postracker/__init__.py index da70fe6..30ec345 100644 --- a/sso/services/postracker/__init__.py +++ b/sso/services/postracker/__init__.py @@ -1,6 +1,6 @@ import hashlib import random -from django.db import load_backend, transaction, IntegrityError +from django.db import transaction, IntegrityError from sso.services import BaseDBService import settings @@ -37,7 +37,7 @@ class POSTrackerService(BaseDBService): eveid = kwargs['character'].eveaccount_set.all()[0].api_user_id self.dbcursor.execute(self.SQL_ADD_USER, [eveid, username, "%s%s" % (salt, pwhash) , email]) - self.db.connection.commit() + transaction.set_dirty() return { 'username': username, 'password': password } def check_user(self, username): @@ -51,20 +51,20 @@ class POSTrackerService(BaseDBService): def delete_user(self, uid): """ Delete a user """ self.dbcursor.execute(self.SQL_DEL_USER, [uid]) - self.db.connection.commit() + transaction.set_dirty() return True def disable_user(self, uid): """ Disable a user """ self.dbcursor.execute(self.SQL_DIS_USER, [uid]) - self.db.connection.commit() + transaction.set_dirty() return True def enable_user(self, uid, password): """ Enable a user """ pwhash, salt = self._gen_pwhash(password) self.dbcursor.execute(self.SQL_ENABLE_USER, ["%s%s" % (salt, pwhash), uid]) - self.db.connection.commit() + transaction.set_dirty() return True def reset_password(self, uid, password): diff --git a/sso/services/qms/__init__.py b/sso/services/qms/__init__.py index 0b27ca8..0ae8aa6 100644 --- a/sso/services/qms/__init__.py +++ b/sso/services/qms/__init__.py @@ -35,7 +35,7 @@ class QMSService(BaseDBService): email = kwargs['user'].email pwhash, salt, cert = self._gen_pwhash(password) self.dbcursor.execute(self.SQL_ADD_USER, [username, username, pwhash, salt, email, cert]) - self.db.connection.commit() + transaction.set_dirty() return { 'username': username, 'password': password } def check_user(self, username): @@ -50,20 +50,20 @@ class QMSService(BaseDBService): """ Delete a user """ #self.dbcursor.execute(self.SQL_DEL_REV, [uid]) #self.dbcursor.execute(self.SQL_DEL_USER, [uid]) - #self.db.connection.commit() + #transaction.set_dirty() return True def disable_user(self, uid): """ Disable a user """ self.dbcursor.execute(self.SQL_DIS_USER, [uid]) - self.db.connection.commit() + transaction.set_dirty() return True def enable_user(self, uid, password): """ Enable a user """ pwhash, salt, cert = self._gen_pwhash(password) self.dbcursor.execute(self.SQL_ENABLE_USER, [pwhash, salt, cert, uid]) - self.db.connection.commit() + transaction.set_dirty() return True def reset_password(self, uid, password): diff --git a/sso/services/wiki/__init__.py b/sso/services/wiki/__init__.py index c02a5c0..f9b3517 100644 --- a/sso/services/wiki/__init__.py +++ b/sso/services/wiki/__init__.py @@ -53,7 +53,7 @@ class MediawikiService(BaseDBService): email = '' pwhash = self._gen_mw_hash(password) self.dbcursor.execute(self.SQL_ADD_USER, [self._clean_username(username), pwhash, email, self.default_options]) - self.db.connection.commit() + transaction.set_dirty() return { 'username': self._clean_username(username), 'password': password } def check_user(self, username): @@ -68,7 +68,7 @@ class MediawikiService(BaseDBService): """ Delete a user """ self.dbcursor.execute(self.SQL_DEL_REV, [uid]) self.dbcursor.execute(self.SQL_DEL_USER, [uid]) - self.db.connection.commit() + transaction.set_dirty() return True def disable_user(self, uid): @@ -79,16 +79,15 @@ class MediawikiService(BaseDBService): except IntegrityError: # Record already exists, skip it pass - self.db.connection.commit() + transaction.set_dirty() return True def enable_user(self, uid, password): """ Enable a user """ pwhash = self._gen_mw_hash(password) self.dbcursor.execute(self.SQL_ENABLE_USER, [pwhash, uid]) - self.db.connection.commit() self.dbcursor.execute(self.SQL_ENABLE_GROUP, [uid]) - self.db.connection.commit() + transaction.set_dirty() return True def reset_password(self, uid, password):