Skip to content

Commit 6c6a9d8

Browse files
committed
More documentation
1 parent 4c17742 commit 6c6a9d8

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,13 @@ TEMPLATE_CONTEXT_PROCESSORS = (
6969
)
7070
```
7171

72-
And use it as you would any boolean in the template, e.g. `{% if SITE_READ_ONLY %} We're down for maintenance. {% endif %}`
72+
And use it as you would any boolean in the template, e.g. `{% if SITE_READ_ONLY %}We're down for maintenance.{% endif %}`
73+
74+
## Configuration
75+
76+
- `SITE_READ_ONLY` - Use to disable writes to the database.
77+
- `DB_READ_ONLY_DATABASES` - A list of database names that read only is enforced on (and ignored for others).
78+
- `DB_READ_ONLY_MIDDLEWARE_MESSAGE` - A custom message that can be used to tell the user when the DB is in readonly mode.
7379

7480
## Testing
7581

readonly/cursor.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ def _is_readonly():
1414

1515

1616
def _get_readonly_dbs():
17-
read_on_db_names = []
17+
read_only_db_names = []
1818
for db_key in getattr(settings, "DB_READ_ONLY_DATABASES", tuple()):
1919
db = settings.DATABASES.get(db_key)
2020
if db:
21-
read_on_db_names.append(db["NAME"])
22-
return read_on_db_names
21+
read_only_db_names.append(db["NAME"])
22+
return read_only_db_names
2323

2424

2525
class ReadOnlyCursorWrapper(object):
@@ -54,16 +54,12 @@ class ReadOnlyCursorWrapper(object):
5454
)
5555

5656
def __init__(self, cursor, db, read_only=None, readonly_dbs=None):
57-
58-
if read_only is None:
59-
read_only = _is_readonly()
60-
if readonly_dbs is None:
61-
readonly_dbs = _get_readonly_dbs()
62-
6357
self.cursor = cursor
6458
self.db = db
65-
self.readonly = read_only
66-
self.readonly_dbs = readonly_dbs
59+
self.readonly = _is_readonly() if read_only is None else read_only
60+
self.readonly_dbs = (
61+
_get_readonly_dbs() if readonly_dbs is None else readonly_dbs
62+
)
6763

6864
def execute(self, sql, params=()):
6965
# Check the SQL
@@ -93,9 +89,15 @@ def _write_sql(self, sql):
9389
)
9490

9591
def _write_to_readonly_db(self):
96-
return (
97-
not self.readonly_dbs or self.db.settings_dict["NAME"] in self.readonly_dbs
98-
)
92+
""""
93+
Is this an access to a readonly database?
94+
""""
95+
if not self.readonly_dbs:
96+
# All dbs are readonly
97+
return True
98+
99+
# Is the db in this list of readonly dbs?
100+
return self.db.settings_dict["NAME"] in self.readonly_dbs
99101

100102
@property
101103
def _last_executed(self):

0 commit comments

Comments
 (0)