2828def _readonly ():
2929 return getattr (settings , 'SITE_READ_ONLY' , False )
3030
31+ def _get_readonly_dbs ():
32+ read_on_db_names = []
33+ for db_key in getattr (settings , 'DB_READ_ONLY_DATABASES' , tuple ()):
34+ db = settings .DATABASES .get (db_key )
35+ if db :
36+ read_on_db_names .append (db ['NAME' ])
37+ return read_on_db_names
3138
3239class ReadOnlyCursorWrapper (object ):
3340 """
@@ -54,13 +61,15 @@ class ReadOnlyCursorWrapper(object):
5461 )
5562 _last_executed = ''
5663
57- def __init__ (self , cursor ):
64+ def __init__ (self , cursor , db ):
5865 self .cursor = cursor
66+ self .db = db
5967 self .readonly = _readonly ()
68+ self .readonly_dbs = _get_readonly_dbs ()
6069
6170 def execute (self , sql , params = ()):
6271 # Check the SQL
63- if self .readonly and self ._write_sql (sql ):
72+ if self .readonly and self ._write_sql (sql ) and self . _write_to_readonly_db () :
6473 raise DatabaseWriteDenied
6574 return self .cursor .execute (sql , params )
6675
@@ -79,10 +88,12 @@ def __iter__(self):
7988 def _write_sql (self , sql ):
8089 return sql .startswith (self .SQL_WRITE_BLACKLIST )
8190
91+ def _write_to_readonly_db (self ):
92+ return not self .readonly_dbs or self .db .settings_dict ['NAME' ] in self .readonly_dbs
8293
8394class CursorWrapper (util .CursorWrapper ):
8495 def __init__ (self , cursor , db ):
85- self .cursor = ReadOnlyCursorWrapper (cursor )
96+ self .cursor = ReadOnlyCursorWrapper (cursor , db )
8697 self .db = db
8798
8899
0 commit comments