From dc4acd08505f2e70e0203ef616ce39e71f535e84 Mon Sep 17 00:00:00 2001 From: xianfuxing Date: Sat, 28 Jul 2018 17:39:55 +0800 Subject: [PATCH] # add counter app --- apps/counter/__init__.py | 0 apps/counter/admin.py | 3 ++ apps/counter/api/__init__.py | 0 apps/counter/apps.py | 5 +++ apps/counter/models.py | 1 + apps/counter/tests.py | 3 ++ apps/counter/views.py | 3 ++ mosqkiller/database_router.py | 60 +++++++++++++++++++++++++++++++++++ mosqkiller/settings.py | 11 +++++++ 9 files changed, 86 insertions(+) create mode 100644 apps/counter/__init__.py create mode 100644 apps/counter/admin.py create mode 100644 apps/counter/api/__init__.py create mode 100644 apps/counter/apps.py create mode 100644 apps/counter/models.py create mode 100644 apps/counter/tests.py create mode 100644 apps/counter/views.py create mode 100644 mosqkiller/database_router.py diff --git a/apps/counter/__init__.py b/apps/counter/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/counter/admin.py b/apps/counter/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/apps/counter/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/apps/counter/api/__init__.py b/apps/counter/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/counter/apps.py b/apps/counter/apps.py new file mode 100644 index 0000000..a849ff6 --- /dev/null +++ b/apps/counter/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class CounterConfig(AppConfig): + name = 'counter' diff --git a/apps/counter/models.py b/apps/counter/models.py new file mode 100644 index 0000000..137941f --- /dev/null +++ b/apps/counter/models.py @@ -0,0 +1 @@ +from django.db import models diff --git a/apps/counter/tests.py b/apps/counter/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/apps/counter/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/apps/counter/views.py b/apps/counter/views.py new file mode 100644 index 0000000..91ea44a --- /dev/null +++ b/apps/counter/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. diff --git a/mosqkiller/database_router.py b/mosqkiller/database_router.py new file mode 100644 index 0000000..a03f7bb --- /dev/null +++ b/mosqkiller/database_router.py @@ -0,0 +1,60 @@ +from django.conf import settings + +DATABASE_MAPPING = settings.DATABASE_APPS_MAPPING + + +class DatabaseAppsRouter(object): + """ + A router to control all database operations on models for different + databases. + + In case an app is not set in settings.DATABASE_APPS_MAPPING, the router + will fallback to the `default` database. + + Settings example: + + DATABASE_APPS_MAPPING = {'counter': 'counter'} + """ + + def db_for_read(self, model, **hints): + """"Point all read operations to the specific database.""" + if model._meta.app_label in DATABASE_MAPPING: + return DATABASE_MAPPING[model._meta.app_label] + return None + + def db_for_write(self, model, **hints): + """Point all write operations to the specific database.""" + if model._meta.app_label in DATABASE_MAPPING: + return DATABASE_MAPPING[model._meta.app_label] + return None + + def allow_relation(self, obj1, obj2, **hints): + """Allow any relation between apps that use the same database.""" + db_obj1 = DATABASE_MAPPING.get(obj1._meta.app_label) + db_obj2 = DATABASE_MAPPING.get(obj2._meta.app_label) + if db_obj1 and db_obj2: + if db_obj1 == db_obj2: + return True + else: + return False + return None + + def allow_syncdb(self, db, model): + """Make sure that apps only appear in the related database.""" + + if db in DATABASE_MAPPING.values(): + return DATABASE_MAPPING.get(model._meta.app_label) == db + elif model._meta.app_label in DATABASE_MAPPING: + return False + return None + + def allow_migrate(self, db, app_label, model=None, **hints): + """ + Make sure the auth app only appears in the 'auth_db' + database. + """ + if db in DATABASE_MAPPING.values(): + return DATABASE_MAPPING.get(app_label) == db + elif app_label in DATABASE_MAPPING: + return False + return None diff --git a/mosqkiller/settings.py b/mosqkiller/settings.py index ae83a12..7b9b5c8 100644 --- a/mosqkiller/settings.py +++ b/mosqkiller/settings.py @@ -97,9 +97,20 @@ DATABASES = { 'PASSWORD': 'Q5BkPk62mqeBxI_lHh2', 'HOST': '47.106.73.20', 'PORT': 3326 + }, + 'counter': { + 'ENGINE': 'django.db.backends.mysql', + 'NAME': 'counter', + 'USER': 'mosq_admin', + 'PASSWORD': 'Q5BkPk62mqeBxI_lHh2', + 'HOST': '47.106.73.20', + 'PORT': 3306 } } +DATABASE_APPS_MAPPING = { + 'counter': 'counter' +} # Password validation # https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators