# add counter app

This commit is contained in:
xianfuxing 2018-07-28 17:39:55 +08:00
parent 9d66545550
commit dc4acd0850
9 changed files with 86 additions and 0 deletions

0
apps/counter/__init__.py Normal file
View File

3
apps/counter/admin.py Normal file
View File

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

View File

5
apps/counter/apps.py Normal file
View File

@ -0,0 +1,5 @@
from django.apps import AppConfig
class CounterConfig(AppConfig):
name = 'counter'

1
apps/counter/models.py Normal file
View File

@ -0,0 +1 @@
from django.db import models

3
apps/counter/tests.py Normal file
View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

3
apps/counter/views.py Normal file
View File

@ -0,0 +1,3 @@
from django.shortcuts import render
# Create your views here.

View File

@ -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

View File

@ -97,9 +97,20 @@ DATABASES = {
'PASSWORD': 'Q5BkPk62mqeBxI_lHh2', 'PASSWORD': 'Q5BkPk62mqeBxI_lHh2',
'HOST': '47.106.73.20', 'HOST': '47.106.73.20',
'PORT': 3326 '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 # Password validation
# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators # https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators