django_courseware_routers.py 1.98 KB
Newer Older
1
"""
2
Database Routers for use with the coursewarehistoryextended django app.
3 4 5
"""


6
class StudentModuleHistoryExtendedRouter(object):
7
    """
8
    A Database Router that separates StudentModuleHistoryExtended into its own database.
9 10 11 12 13 14
    """

    DATABASE_NAME = 'student_module_history'

    def _is_csmh(self, model):
        """
15
        Return True if ``model`` is courseware.StudentModuleHistoryExtended.
16 17
        """
        return (
18 19
            model._meta.app_label == 'coursewarehistoryextended' and  # pylint: disable=protected-access
            model.__name__ == 'StudentModuleHistoryExtended'
20 21 22 23
        )

    def db_for_read(self, model, **hints):  # pylint: disable=unused-argument
        """
24
        Use the StudentModuleHistoryExtendedRouter.DATABASE_NAME if the model is StudentModuleHistoryExtended.
25 26 27 28 29 30 31 32
        """
        if self._is_csmh(model):
            return self.DATABASE_NAME
        else:
            return None

    def db_for_write(self, model, **hints):  # pylint: disable=unused-argument
        """
33
        Use the StudentModuleHistoryExtendedRouter.DATABASE_NAME if the model is StudentModuleHistoryExtended.
34 35 36 37 38 39 40 41
        """
        if self._is_csmh(model):
            return self.DATABASE_NAME
        else:
            return None

    def allow_relation(self, obj1, obj2, **hints):  # pylint: disable=unused-argument
        """
42
        Disable relations if the model is StudentModuleHistoryExtended.
43 44 45 46 47
        """
        if self._is_csmh(obj1) or self._is_csmh(obj2):
            return False
        return None

48
    def allow_migrate(self, db, app_label, model_name=None, **hints):  # pylint: disable=unused-argument
49
        """
50
        Only sync StudentModuleHistoryExtended to StudentModuleHistoryExtendedRouter.DATABASE_Name
51
        """
52 53
        if model_name is not None:
            model = hints.get('model')
54
            if model is not None and self._is_csmh(model):
55 56
                return db == self.DATABASE_NAME
        if db == self.DATABASE_NAME:
57 58 59
            return False

        return None