forked from OpenDataPolicingNC/Traffic-Stops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouters.py
54 lines (46 loc) · 1.63 KB
/
routers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import logging
from django.conf import settings
logger = logging.getLogger(__name__)
class StateDatasetRouter(object):
"""Read/write from speciic State databases"""
def _db_name(self, model):
return 'traffic_stops_{}'.format(model._meta.app_label)
def db_for_read(self, model, **hints):
"""Return state DB if model's app name is a database"""
state_db = self._db_name(model)
if state_db in settings.DATABASES:
name = state_db
else:
name = 'default'
logger.debug('db_for_read({}): {}'.format(state_db, name))
return name
def db_for_write(self, model, **hints):
"""Return state DB if model's app name is a database"""
state_db = self._db_name(model)
if state_db in settings.DATABASES:
name = state_db
else:
name = 'default'
logger.debug('db_for_write({}): {}'.format(state_db, name))
return name
def allow_syncdb(self, db, model):
# scenarios:
#
# default traffic_stops_nc False
# default traffic_stops_admin True
# traffic_stops_nc traffic_stops_admin False
# traffic_stops_nc traffic_stops_nc True
#
name = self._db_name(model)
if db == 'default':
if name in settings.DATABASES:
ret = False
else:
ret = True
else:
if name in settings.DATABASES:
ret = True
else:
ret = False
logger.debug('allow_syncdb({}, {}): {}'.format(db, model, ret))
return ret