[django, misc] adds create_or_update to djangos ETLCommand class, adds getitem/setitem/contains dunders to ValueHolder.
This commit is contained in:
@ -9,9 +9,34 @@ from bonobo.commands.run import get_default_services
|
|||||||
from bonobo.ext.console import ConsoleOutputPlugin
|
from bonobo.ext.console import ConsoleOutputPlugin
|
||||||
from bonobo.util.term import CLEAR_EOL
|
from bonobo.util.term import CLEAR_EOL
|
||||||
|
|
||||||
|
|
||||||
class ETLCommand(BaseCommand):
|
class ETLCommand(BaseCommand):
|
||||||
GraphType = bonobo.Graph
|
GraphType = bonobo.Graph
|
||||||
|
|
||||||
|
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 get_graph(self, *args, **options):
|
||||||
def not_implemented():
|
def not_implemented():
|
||||||
raise NotImplementedError('You must implement {}.get_graph() method.'.format(self))
|
raise NotImplementedError('You must implement {}.get_graph() method.'.format(self))
|
||||||
|
|||||||
@ -1,7 +1,3 @@
|
|||||||
import functools
|
|
||||||
from functools import partial
|
|
||||||
|
|
||||||
|
|
||||||
def get_name(mixed):
|
def get_name(mixed):
|
||||||
try:
|
try:
|
||||||
return mixed.__name__
|
return mixed.__name__
|
||||||
@ -146,10 +142,10 @@ class ValueHolder:
|
|||||||
return divmod(other, self._value)
|
return divmod(other, self._value)
|
||||||
|
|
||||||
def __pow__(self, other):
|
def __pow__(self, other):
|
||||||
return self._value**other
|
return self._value ** other
|
||||||
|
|
||||||
def __rpow__(self, other):
|
def __rpow__(self, other):
|
||||||
return other**self._value
|
return other ** self._value
|
||||||
|
|
||||||
def __ipow__(self, other):
|
def __ipow__(self, other):
|
||||||
self._value **= other
|
self._value **= other
|
||||||
@ -220,6 +216,15 @@ class ValueHolder:
|
|||||||
def __len__(self):
|
def __len__(self):
|
||||||
return len(self._value)
|
return len(self._value)
|
||||||
|
|
||||||
|
def __contains__(self, item):
|
||||||
|
return item in self._value
|
||||||
|
|
||||||
|
def __getitem__(self, item):
|
||||||
|
return self._value[item]
|
||||||
|
|
||||||
|
def __setitem__(self, key, value):
|
||||||
|
self._value[key] = value
|
||||||
|
|
||||||
|
|
||||||
def get_attribute_or_create(obj, attr, default):
|
def get_attribute_or_create(obj, attr, default):
|
||||||
try:
|
try:
|
||||||
|
|||||||
Reference in New Issue
Block a user