calibre-web-companion/CalibreWebCompanion/db_routers/routers.py

79 lines
2.1 KiB
Python
Raw Normal View History

2020-08-02 02:51:34 +00:00
import logging
2020-07-08 02:11:21 +00:00
2020-08-02 02:51:34 +00:00
logger = logging.getLogger(__name__)
2020-07-08 02:11:21 +00:00
class DjangoRouter:
"""
A router to control all database operations on models in the
auth and contenttypes applications.
"""
def db_for_read(self, model, **hints):
"""
Attempts to read anything else goes to calibre
2020-07-08 02:11:21 +00:00
"""
return 'default'
2020-07-08 02:11:21 +00:00
def db_for_write(self, model, **hints):
2020-07-08 02:11:21 +00:00
"""
Attempts to write auth and contenttypes models go to 'calibre'.
2020-07-08 02:11:21 +00:00
"""
return 'default'
2020-07-08 02:11:21 +00:00
def allow_relation(self, obj1, obj2, **hints):
"""
Allow relations.
2020-07-08 02:11:21 +00:00
"""
return True
2020-07-08 02:11:21 +00:00
def allow_migrate(self, db, app_label, model_name=None, **hints):
2020-07-08 02:11:21 +00:00
"""
Yes
2020-07-08 02:11:21 +00:00
"""
return True
2020-07-08 02:11:21 +00:00
class CalibreRouter:
"""
A router to control all database operations on models in the
auth and contenttypes applications.
"""
route_app_labels = {"library"}
2020-07-08 02:11:21 +00:00
def db_for_read(self, model, **hints):
"""
Attempts to read auth and contenttypes models go to default.
2020-07-08 02:11:21 +00:00
"""
if model._meta.app_label in self.route_app_labels:
return 'calibre'
return None
2020-07-08 02:11:21 +00:00
def allow_relation(self, obj1, obj2, **hints):
"""
Allow relations if a model in the auth or contenttypes apps is
involved.
2020-07-08 02:11:21 +00:00
"""
if (
obj1._meta.app_label in self.route_app_labels or
obj2._meta.app_label in self.route_app_labels
):
return True
return None
# def allow_migrate(self, db, app_label, model_name=None, **hints):
# """
# Make sure the auth and contenttypes apps only appear in the
# 'django' database.
# """
# if app_label in self.route_app_labels:
# return db == 'default'
# return None
2020-07-08 02:11:21 +00:00
# def db_for_write(self, model, **hints):
2020-07-08 02:11:21 +00:00
# """
# Attempts to write auth and contenttypes models go to django.
2020-07-08 02:11:21 +00:00
# """
# if model._meta.app_label in self.route_app_labels:
# return 'default'
# return None