This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
from pathlib import Path
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
from django.core.files import File
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
|
||||
from finance.importing import file_digest, profile_file, suggest_mapping, validate_mapping
|
||||
from finance.models import Audit, Dataset
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "Import a local source using reviewed field aliases and lookup rates"
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument("path")
|
||||
parser.add_argument("--username", required=True)
|
||||
parser.add_argument("--confirm-mapping", action="store_true")
|
||||
|
||||
def handle(self, *args, **options):
|
||||
user = User.objects.get(username=options["username"])
|
||||
if user.membership.role != "admin" or user.membership.departments:
|
||||
raise CommandError("Full administrator required")
|
||||
path = Path(options["path"])
|
||||
profile = profile_file(path)
|
||||
mapping = validate_mapping(suggest_mapping(profile), profile)
|
||||
if not options["confirm_mapping"]:
|
||||
self.stdout.write(str(mapping))
|
||||
self.stdout.write("Review mapping, then rerun with --confirm-mapping")
|
||||
return
|
||||
with path.open("rb") as source:
|
||||
file = File(source, name=path.name)
|
||||
digest = file_digest(file)
|
||||
ds = Dataset.objects.filter(organization=user.membership.organization, sha256=digest).first()
|
||||
if ds:
|
||||
self.stdout.write(f"Existing version: {ds.pk} {ds.status}")
|
||||
return
|
||||
ds = Dataset.objects.create(
|
||||
organization=user.membership.organization,
|
||||
created_by=user,
|
||||
name=path.name,
|
||||
file=file,
|
||||
sha256=digest,
|
||||
mapping=mapping,
|
||||
profile=profile,
|
||||
status="queued",
|
||||
)
|
||||
Audit.objects.create(
|
||||
organization=user.membership.organization,
|
||||
user=user,
|
||||
action="dataset.cli_import",
|
||||
resource=str(ds.pk),
|
||||
details={"mapping": mapping},
|
||||
)
|
||||
self.stdout.write(f"{ds.pk}: queued; import_worker will process this version")
|
||||
Reference in New Issue
Block a user