Moves django extension to contrib module.
This commit is contained in:
0
bonobo/contrib/__init__.py
Normal file
0
bonobo/contrib/__init__.py
Normal file
7
bonobo/contrib/django/__init__.py
Normal file
7
bonobo/contrib/django/__init__.py
Normal file
@ -0,0 +1,7 @@
|
||||
from .utils import create_or_update
|
||||
from .commands import ETLCommand
|
||||
|
||||
__all__ = [
|
||||
'ETLCommand',
|
||||
'create_or_update',
|
||||
]
|
||||
@ -1,54 +1,16 @@
|
||||
from logging import getLogger
|
||||
|
||||
import bonobo
|
||||
import bonobo.util
|
||||
from bonobo.plugins.console import ConsoleOutputPlugin
|
||||
from bonobo.util.term import CLEAR_EOL
|
||||
from colorama import Fore, Back, Style
|
||||
from django.core.management.base import BaseCommand, OutputWrapper
|
||||
from django.core.management import BaseCommand
|
||||
from django.core.management.base import OutputWrapper
|
||||
|
||||
from .utils import create_or_update
|
||||
|
||||
|
||||
class ETLCommand(BaseCommand):
|
||||
GraphType = bonobo.Graph
|
||||
|
||||
def create_parser(self, prog_name, subcommand):
|
||||
return bonobo.get_argument_parser(
|
||||
super().create_parser(prog_name, subcommand)
|
||||
)
|
||||
|
||||
def create_or_update(self, model, *, defaults=None, save=True, **kwargs):
|
||||
"""
|
||||
Create or update a django model instance.
|
||||
|
||||
:param model:
|
||||
:param defaults:
|
||||
:param kwargs:
|
||||
:return: object, created, updated
|
||||
|
||||
"""
|
||||
obj, created = model._default_manager.get_or_create(defaults=defaults, **kwargs)
|
||||
|
||||
updated = False
|
||||
if not created:
|
||||
for k, v in defaults.items():
|
||||
if getattr(obj, k) != v:
|
||||
setattr(obj, k, v)
|
||||
updated = True
|
||||
|
||||
if updated and save:
|
||||
obj.save()
|
||||
|
||||
return obj, created, updated
|
||||
|
||||
def get_graph(self, *args, **options):
|
||||
def not_implemented():
|
||||
raise NotImplementedError('You must implement {}.get_graph() method.'.format(self))
|
||||
|
||||
return self.GraphType(not_implemented)
|
||||
|
||||
def get_services(self):
|
||||
return {}
|
||||
|
||||
@property
|
||||
def logger(self):
|
||||
try:
|
||||
@ -57,6 +19,22 @@ class ETLCommand(BaseCommand):
|
||||
self._logger = getLogger(type(self).__module__)
|
||||
return self._logger
|
||||
|
||||
create_or_update = staticmethod(create_or_update)
|
||||
|
||||
def create_parser(self, prog_name, subcommand):
|
||||
return bonobo.get_argument_parser(
|
||||
super().create_parser(prog_name, subcommand)
|
||||
)
|
||||
|
||||
def get_graph(self, *args, **options):
|
||||
def not_implemented():
|
||||
raise NotImplementedError('You must implement {}.get_graph() method.'.format(self))
|
||||
|
||||
return bonobo.Graph(not_implemented)
|
||||
|
||||
def get_services(self):
|
||||
return {}
|
||||
|
||||
def info(self, *args, **kwargs):
|
||||
self.logger.info(*args, **kwargs)
|
||||
|
||||
23
bonobo/contrib/django/utils.py
Normal file
23
bonobo/contrib/django/utils.py
Normal file
@ -0,0 +1,23 @@
|
||||
def create_or_update(model, *, defaults=None, save=True, **kwargs):
|
||||
"""
|
||||
Create or update a django model instance.
|
||||
|
||||
:param model:
|
||||
:param defaults:
|
||||
:param kwargs:
|
||||
:return: object, created, updated
|
||||
|
||||
"""
|
||||
obj, created = model._default_manager.get_or_create(defaults=defaults, **kwargs)
|
||||
|
||||
updated = False
|
||||
if not created:
|
||||
for k, v in defaults.items():
|
||||
if getattr(obj, k) != v:
|
||||
setattr(obj, k, v)
|
||||
updated = True
|
||||
|
||||
if updated and save:
|
||||
obj.save()
|
||||
|
||||
return obj, created, updated
|
||||
Reference in New Issue
Block a user