Skip to content

Commit

Permalink
PEP 8
Browse files Browse the repository at this point in the history
  • Loading branch information
streeter committed Apr 28, 2011
1 parent d46a079 commit c8bce65
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
34 changes: 18 additions & 16 deletions readonly/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,59 +19,61 @@

logger = getLogger('django.db.backends')


def _readonly():
return getattr(settings, 'SITE_READ_ONLY', False)


class ReadOnlyCursorWrapper(object):
"""
This is a wrapper for a database cursor.
This sits between django's own wrapper at
`django.db.backends.util.CursorWrapper` and the database specific cursor at
`django.db.backends.*.base.*CursorWrapper`. It overrides two specific
methods: `execute` and `executemany`. If the site is in read-only mode, then
the SQL is examined to see if it contains any write actions. If a write
is detected, an exception is raised.
methods: `execute` and `executemany`. If the site is in read-only mode,
then the SQL is examined to see if it contains any write actions. If a
write is detected, an exception is raised.
A site is in read only mode by setting the SITE_READ_ONLY setting. For
obvious reasons, this is False by default.
Raises a DatabaseWriteDenied exception if writes are disabled.
"""

SQL_WRITE_BLACKLIST = (
# Data Definition
'CREATE', 'ALTER', 'RENAME', 'DROP', 'TRUNCATE',
# Data Manipulation
'INSERT INTO', 'UPDATE', 'REPLACE', 'DELETE FROM',
'INSERT INTO', 'UPDATE', 'REPLACE', 'DELETE FROM',
)

def __init__(self, cursor):
self.cursor = cursor
self.readonly = _readonly()

def execute(self, sql, params=()):
# Check the SQL
if self.readonly and self._write_sql(sql):
raise DatabaseWriteDenied
return self.cursor.execute(sql, params)

def executemany(self, sql, param_list):
# Check the SQL
if self.readonly and self._write_sql(sql):
raise DatabaseWriteDenied
return self.cursor.executemany(sql, param_list)

def __getattr__(self, attr):
return getattr(self.cursor, attr)

def __iter__(self):
return iter(self.cursor)

def _write_sql(self, sql):
return sql.startswith(self.SQL_WRITE_BLACKLIST)


class CursorWrapper(util.CursorWrapper):
def __init__(self, cursor, db):
self.cursor = ReadOnlyCursorWrapper(cursor)
Expand All @@ -95,7 +97,7 @@ def execute(self, sql, params=()):
'time': "%.3f" % duration,
})
logger.debug('(%.3f) %s; args=%s' % (duration, sql, params),
extra={'duration':duration, 'sql':sql, 'params':params}
extra={'duration': duration, 'sql': sql, 'params': params}
)

def executemany(self, sql, param_list):
Expand All @@ -110,7 +112,7 @@ def executemany(self, sql, param_list):
'time': "%.3f" % duration,
})
logger.debug('(%.3f) %s; args=%s' % (duration, sql, param_list),
extra={'duration':duration, 'sql':sql, 'params':param_list}
extra={'duration': duration, 'sql': sql, 'params': param_list}
)

if _readonly():
Expand Down
2 changes: 1 addition & 1 deletion readonly/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.db.utils import DatabaseError


class DatabaseWriteDenied(DatabaseError):
pass

7 changes: 4 additions & 3 deletions runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
if not settings.configured:
settings.configure(
DATABASE_ENGINE='sqlite3',

# Uncomment below to run tests with mysql
#DATABASE_ENGINE='django.db.backends.mysql',
#DATABASE_NAME='readonly_test',
Expand All @@ -23,11 +23,12 @@

from django.test.simple import run_tests


def runtests(*test_args):
if 'south' in settings.INSTALLED_APPS:
from south.management.commands import patch_for_test_db_setup
patch_for_test_db_setup()

if not test_args:
test_args = ['readonly']
parent = dirname(abspath(__file__))
Expand All @@ -37,4 +38,4 @@ def runtests(*test_args):


if __name__ == '__main__':
runtests(*sys.argv[1:])
runtests(*sys.argv[1:])
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ def run(self, *args, **kwargs):
author='Chris Streeter',
author_email='pypi@chrisstreeter.com',
url='http://github.com/streeter/django-db-readonly',
description = 'Add a global database read-only setting.',
description='Add a global database read-only setting.',
packages=find_packages(),
zip_safe=False,
install_requires=[
],
test_suite = 'readonly.tests',
test_suite='readonly.tests',
include_package_data=True,
cmdclass={"test": mytest},
classifiers=[
Expand Down

0 comments on commit c8bce65

Please sign in to comment.