File size: 1,569 Bytes
947c08e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import inspect
import backend.models.model_cache as model_cache
import backend.models.model_1 as model_1
import backend.models.model_2 as model_2

CACHE_MODELS = [cls.__name__.lower() for name, cls in inspect.getmembers(model_cache, inspect.isclass) if cls.__module__ == model_cache.__name__]
DB1_MODELS = [cls.__name__.lower() for name, cls in inspect.getmembers(model_1, inspect.isclass) if cls.__module__ == model_1.__name__]
DB2_MODELS = [cls.__name__.lower() for name, cls in inspect.getmembers(model_2, inspect.isclass) if cls.__module__ == model_2.__name__]


class Router:
    def db_for_read(self, model, **hints):
        if model._meta.model_name in CACHE_MODELS:
            return 'cache'
        elif model._meta.model_name in DB1_MODELS:
            return 'DB1'
        elif model._meta.model_name in DB2_MODELS:
            return 'DB2'
        return 'default'

    def db_for_write(self, model, **hints):
        if model._meta.model_name in CACHE_MODELS:
            return 'cache'
        elif model._meta.model_name in DB1_MODELS:
            return 'DB1'
        elif model._meta.model_name in DB2_MODELS:
            return 'DB2'
        return 'default'
    
    def allow_relation(self, obj1, obj2, **hints):
        return True

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        if model_name in CACHE_MODELS:
            return db == 'cache'
        elif model_name in DB1_MODELS:
            return db == 'DB1'
        elif model_name in DB2_MODELS:
            return db == 'DB2'
        return db == 'default'