if kepicreate creates a Create, it prints the created object too

2019-08-17
Marnanel Thurman 2019-06-25 00:17:27 +01:00
rodzic fb212e725e
commit a7e7d43e30
1 zmienionych plików z 22 dodań i 14 usunięć

Wyświetl plik

@ -1,5 +1,7 @@
from django.core.management.base import BaseCommand, CommandError
import json
import django_kepi.models
import sys
class Command(BaseCommand):
help = 'create a kepi object'
@ -13,25 +15,27 @@ class Command(BaseCommand):
def handle(self, *args, **options):
fields = {
'type': options['type'].title(),
}
if options['type'] == '-':
fields = json.loads(sys.stdin.read())
for fv in options['fields']:
try:
f, v = fv.split('=', maxsplit=1)
except ValueError:
self.stderr.write(self.style.ERROR(
fv + ' needs to be in the format FIELD=VALUE',
))
continue
else:
fields = {
'type': options['type'].title(),
}
fields[f.lower()] = v
for fv in options['fields']:
try:
f, v = fv.split('=', maxsplit=1)
except ValueError:
self.stderr.write(self.style.ERROR(
fv + ' needs to be in the format FIELD=VALUE',
))
continue
fields[f.lower()] = v
created = None
print(fields)
try:
created = django_kepi.models.create(**fields)
self.stdout.write(self.style.SUCCESS('Created object.'))
@ -40,3 +44,7 @@ class Command(BaseCommand):
if created:
self.stdout.write(self.style.NOTICE(created.pretty))
if created['type']=='Create':
self.stdout.write(' ---')
self.stdout.write(self.style.NOTICE(created['object__obj'].pretty))