new_url command will automatically gather and replace the router file
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 4s
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 4s
This commit is contained in:
@@ -4,21 +4,44 @@ import os
|
|||||||
#import jinja2
|
#import jinja2
|
||||||
from jinja2 import Environment, FileSystemLoader
|
from jinja2 import Environment, FileSystemLoader
|
||||||
|
|
||||||
router_path = os.path.join(settings.BASE_DIR,'core','routers_jinja.py')
|
router_path = os.path.join(settings.BASE_DIR,'core','routers.py')
|
||||||
|
|
||||||
class Command(BaseCommand):
|
class Command(BaseCommand):
|
||||||
args = '<team_id>'
|
args = '<team_id>'
|
||||||
help = 'Affiche la liste des backlogs'
|
help = 'Affiche la liste des backlogs'
|
||||||
|
|
||||||
template_dir = os.path.join(settings.BASE_DIR, 'templates')
|
def extract_settings_database(self):
|
||||||
env = Environment(loader=FileSystemLoader(template_dir))
|
settings_databases = []
|
||||||
template = env.get_template('helloWorld.jinja')
|
for database in settings.DATABASES:
|
||||||
output = template.render(name = 'Geeks')
|
if database == 'default':
|
||||||
|
continue
|
||||||
|
print(database)
|
||||||
|
|
||||||
with open(router_path, 'w') as f:
|
database_label = database.split('_db')[0]
|
||||||
print(output, file = f)
|
|
||||||
|
settings_databases.append([database,database_label])
|
||||||
|
return settings_databases
|
||||||
|
|
||||||
|
def coucou(self):
|
||||||
|
self.stdout.write('Coucou !')
|
||||||
|
|
||||||
|
def templating(self,databases_list):
|
||||||
|
template_dir = os.path.join(settings.BASE_DIR, 'templates','routers')
|
||||||
|
env = Environment(loader=FileSystemLoader(template_dir))
|
||||||
|
template = env.get_template('routers.jinja.py')
|
||||||
|
output = template.render(databases_list = databases_list)
|
||||||
|
|
||||||
|
with open(router_path, 'w') as f:
|
||||||
|
print(output, file = f)
|
||||||
|
|
||||||
|
print(output)
|
||||||
|
|
||||||
|
def main(self):
|
||||||
|
databases_list = self.extract_settings_database()
|
||||||
|
print(databases_list)
|
||||||
|
self.templating(databases_list)
|
||||||
|
self.coucou()
|
||||||
|
|
||||||
print(output)
|
|
||||||
|
|
||||||
def handle(self, *args, **options):
|
def handle(self, *args, **options):
|
||||||
self.stdout.write('Coucou !')
|
self.main()
|
||||||
+38
-17
@@ -8,62 +8,83 @@ class ProjectDatabaseRouter:
|
|||||||
|
|
||||||
def db_for_read(self, model, **hints):
|
def db_for_read(self, model, **hints):
|
||||||
"""Attempts to read simpsons and cloudmanager models go to their specific dbs."""
|
"""Attempts to read simpsons and cloudmanager models go to their specific dbs."""
|
||||||
if model._meta.app_label == 'simpsons':
|
|
||||||
return 'simpsons_db'
|
|
||||||
if model._meta.app_label == 'job':
|
|
||||||
return 'job_db'
|
|
||||||
if model._meta.app_label == 'redirect':
|
|
||||||
return 'redirect_db'
|
|
||||||
if model._meta.app_label == 'cloudmanager':
|
if model._meta.app_label == 'cloudmanager':
|
||||||
return 'cloudmanager_db'
|
return 'cloudmanager_db'
|
||||||
|
|
||||||
|
if model._meta.app_label == 'simpsons':
|
||||||
|
return 'simpsons_db'
|
||||||
|
|
||||||
|
if model._meta.app_label == 'redirect':
|
||||||
|
return 'redirect_db'
|
||||||
|
|
||||||
|
if model._meta.app_label == 'job':
|
||||||
|
return 'job_db'
|
||||||
|
|
||||||
if model._meta.app_label == 'telegram':
|
if model._meta.app_label == 'telegram':
|
||||||
return 'telegram_db'
|
return 'telegram_db'
|
||||||
|
|
||||||
if model._meta.app_label == 'podcast':
|
if model._meta.app_label == 'podcast':
|
||||||
return 'podcast_db'
|
return 'podcast_db'
|
||||||
|
|
||||||
return 'default'
|
return 'default'
|
||||||
|
|
||||||
def db_for_write(self, model, **hints):
|
def db_for_write(self, model, **hints):
|
||||||
"""Attempts to write simpsons and cloudmanager models go to their specific dbs."""
|
"""Attempts to write simpsons and cloudmanager models go to their specific dbs."""
|
||||||
if model._meta.app_label == 'simpsons':
|
|
||||||
return 'simpsons_db'
|
|
||||||
if model._meta.app_label == 'job':
|
|
||||||
return 'job_db'
|
|
||||||
if model._meta.app_label == 'cloudmanager':
|
if model._meta.app_label == 'cloudmanager':
|
||||||
return 'cloudmanager_db'
|
return 'cloudmanager_db'
|
||||||
|
|
||||||
|
if model._meta.app_label == 'simpsons':
|
||||||
|
return 'simpsons_db'
|
||||||
|
|
||||||
if model._meta.app_label == 'redirect':
|
if model._meta.app_label == 'redirect':
|
||||||
return 'redirect_db'
|
return 'redirect_db'
|
||||||
|
|
||||||
|
if model._meta.app_label == 'job':
|
||||||
|
return 'job_db'
|
||||||
|
|
||||||
if model._meta.app_label == 'telegram':
|
if model._meta.app_label == 'telegram':
|
||||||
return 'telegram_db'
|
return 'telegram_db'
|
||||||
|
|
||||||
if model._meta.app_label == 'podcast':
|
if model._meta.app_label == 'podcast':
|
||||||
return 'podcast_db'
|
return 'podcast_db'
|
||||||
|
|
||||||
return 'default'
|
return 'default'
|
||||||
|
|
||||||
def allow_relation(self, obj1, obj2, **hints):
|
def allow_relation(self, obj1, obj2, **hints):
|
||||||
"""Allow relations if both models are in the same database."""
|
"""Allow relations if both models are in the same database."""
|
||||||
# Prevents accidental foreign key relationships across different physical DBs
|
# Prevents accidental foreign key relationships across different physical DBs
|
||||||
if (
|
if (
|
||||||
obj1._meta.app_label in ['simpsons', 'cloudmanager','redirect','job','telegram','podcast'] or
|
obj1._meta.app_label in ['cloudmanager', 'simpsons', 'redirect', 'job', 'telegram', 'podcast'] or
|
||||||
obj2._meta.app_label in ['simpsons', 'cloudmanager','redirect','job','telegram','podcast']
|
obj2._meta.app_label in ['cloudmanager', 'simpsons', 'redirect', 'job', 'telegram', 'podcast']
|
||||||
):
|
):
|
||||||
return obj1._meta.app_label == obj2._meta.app_label
|
return obj1._meta.app_label == obj2._meta.app_label
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def allow_migrate(self, db, app_label, model_name=None, **hints):
|
def allow_migrate(self, db, app_label, model_name=None, **hints):
|
||||||
"""Ensure the app's models only show up in the designated database."""
|
"""Ensure the app's models only show up in the designated database."""
|
||||||
if app_label == 'simpsons':
|
|
||||||
return db == 'simpsons_db'
|
|
||||||
if app_label == 'job':
|
|
||||||
return db == 'job_db'
|
|
||||||
if app_label == 'cloudmanager':
|
if app_label == 'cloudmanager':
|
||||||
return db == 'cloudmanager_db'
|
return db == 'cloudmanager_db'
|
||||||
|
|
||||||
|
if app_label == 'simpsons':
|
||||||
|
return db == 'simpsons_db'
|
||||||
|
|
||||||
if app_label == 'redirect':
|
if app_label == 'redirect':
|
||||||
return db == 'redirect_db'
|
return db == 'redirect_db'
|
||||||
|
|
||||||
|
if app_label == 'job':
|
||||||
|
return db == 'job_db'
|
||||||
|
|
||||||
if app_label == 'telegram':
|
if app_label == 'telegram':
|
||||||
return db == 'telegram_db'
|
return db == 'telegram_db'
|
||||||
|
|
||||||
if app_label == 'podcast':
|
if app_label == 'podcast':
|
||||||
return db == 'podcast_db'
|
return db == 'podcast_db'
|
||||||
|
|
||||||
# Ensure default Django apps (auth, sessions, etc.) don't migrate into simpsons/cloudmanager
|
# Ensure default Django apps (auth, sessions, etc.) don't migrate into simpsons/cloudmanager
|
||||||
if db in ['simpsons_db', 'cloudmanager_db','redirect_db','job_db','telegram_db','podcast_db']:
|
if db in ['cloudmanager_db', 'simpsons_db', 'redirect_db', 'job_db', 'telegram_db', 'podcast_db']:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
return None
|
return None
|
||||||
@@ -1,69 +0,0 @@
|
|||||||
# routers.py
|
|
||||||
|
|
||||||
class ProjectDatabaseRouter:
|
|
||||||
"""
|
|
||||||
A router to control all database operations on models in the
|
|
||||||
simpsons and cloudmanager applications.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def db_for_read(self, model, **hints):
|
|
||||||
"""Attempts to read simpsons and cloudmanager models go to their specific dbs."""
|
|
||||||
if model._meta.app_label == 'simpsons':
|
|
||||||
return 'simpsons_db'
|
|
||||||
if model._meta.app_label == 'job':
|
|
||||||
return 'job_db'
|
|
||||||
if model._meta.app_label == 'redirect':
|
|
||||||
return 'redirect_db'
|
|
||||||
if model._meta.app_label == 'cloudmanager':
|
|
||||||
return 'cloudmanager_db'
|
|
||||||
if model._meta.app_label == 'telegram':
|
|
||||||
return 'telegram_db'
|
|
||||||
if model._meta.app_label == 'podcast':
|
|
||||||
return 'podcast_db'
|
|
||||||
return 'default'
|
|
||||||
|
|
||||||
def db_for_write(self, model, **hints):
|
|
||||||
"""Attempts to write simpsons and cloudmanager models go to their specific dbs."""
|
|
||||||
if model._meta.app_label == 'simpsons':
|
|
||||||
return 'simpsons_db'
|
|
||||||
if model._meta.app_label == 'job':
|
|
||||||
return 'job_db'
|
|
||||||
if model._meta.app_label == 'cloudmanager':
|
|
||||||
return 'cloudmanager_db'
|
|
||||||
if model._meta.app_label == 'redirect':
|
|
||||||
return 'redirect_db'
|
|
||||||
if model._meta.app_label == 'telegram':
|
|
||||||
return 'telegram_db'
|
|
||||||
if model._meta.app_label == 'podcast':
|
|
||||||
return 'podcast_db'
|
|
||||||
return 'default'
|
|
||||||
|
|
||||||
def allow_relation(self, obj1, obj2, **hints):
|
|
||||||
"""Allow relations if both models are in the same database."""
|
|
||||||
# Prevents accidental foreign key relationships across different physical DBs
|
|
||||||
if (
|
|
||||||
obj1._meta.app_label in ['simpsons', 'cloudmanager','redirect','job','telegram','podcast'] or
|
|
||||||
obj2._meta.app_label in ['simpsons', 'cloudmanager','redirect','job','telegram','podcast']
|
|
||||||
):
|
|
||||||
return obj1._meta.app_label == obj2._meta.app_label
|
|
||||||
return None
|
|
||||||
|
|
||||||
def allow_migrate(self, db, app_label, model_name=None, **hints):
|
|
||||||
"""Ensure the app's models only show up in the designated database."""
|
|
||||||
if app_label == 'simpsons':
|
|
||||||
return db == 'simpsons_db'
|
|
||||||
if app_label == 'job':
|
|
||||||
return db == 'job_db'
|
|
||||||
if app_label == 'cloudmanager':
|
|
||||||
return db == 'cloudmanager_db'
|
|
||||||
if app_label == 'redirect':
|
|
||||||
return db == 'redirect_db'
|
|
||||||
if app_label == 'telegram':
|
|
||||||
return db == 'telegram_db'
|
|
||||||
if app_label == 'podcast':
|
|
||||||
return db == 'podcast_db'
|
|
||||||
# Ensure default Django apps (auth, sessions, etc.) don't migrate into simpsons/cloudmanager
|
|
||||||
if db in ['simpsons_db', 'cloudmanager_db','redirect_db','job_db','telegram_db','podcast_db']:
|
|
||||||
return False
|
|
||||||
|
|
||||||
return None
|
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
# routers.py
|
||||||
|
|
||||||
|
class ProjectDatabaseRouter:
|
||||||
|
"""
|
||||||
|
A router to control all database operations on models in the
|
||||||
|
simpsons and cloudmanager applications.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def db_for_read(self, model, **hints):
|
||||||
|
"""Attempts to read simpsons and cloudmanager models go to their specific dbs."""
|
||||||
|
{% for database in databases_list %}
|
||||||
|
if model._meta.app_label == '{{ database[1] }}':
|
||||||
|
return '{{ database[0] }}'
|
||||||
|
{% endfor %}
|
||||||
|
return 'default'
|
||||||
|
|
||||||
|
def db_for_write(self, model, **hints):
|
||||||
|
"""Attempts to write simpsons and cloudmanager models go to their specific dbs."""
|
||||||
|
{% for database in databases_list %}
|
||||||
|
if model._meta.app_label == '{{ database[1] }}':
|
||||||
|
return '{{ database[0] }}'
|
||||||
|
{% endfor %}
|
||||||
|
return 'default'
|
||||||
|
|
||||||
|
def allow_relation(self, obj1, obj2, **hints):
|
||||||
|
"""Allow relations if both models are in the same database."""
|
||||||
|
# Prevents accidental foreign key relationships across different physical DBs
|
||||||
|
if (
|
||||||
|
obj1._meta.app_label in {{ databases_list | map(attribute=1) | list }} or
|
||||||
|
obj2._meta.app_label in {{ databases_list | map(attribute=1) | list }}
|
||||||
|
):
|
||||||
|
return obj1._meta.app_label == obj2._meta.app_label
|
||||||
|
return None
|
||||||
|
|
||||||
|
def allow_migrate(self, db, app_label, model_name=None, **hints):
|
||||||
|
"""Ensure the app's models only show up in the designated database."""
|
||||||
|
{% for database in databases_list %}
|
||||||
|
if app_label == '{{ database[1] }}':
|
||||||
|
return db == '{{ database[0] }}'
|
||||||
|
{% endfor %}
|
||||||
|
# Ensure default Django apps (auth, sessions, etc.) don't migrate into simpsons/cloudmanager
|
||||||
|
if db in {{ databases_list | map(attribute=0) | list }}:
|
||||||
|
return False
|
||||||
|
|
||||||
|
return None
|
||||||
Reference in New Issue
Block a user