Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import table metadata #119

Merged
merged 5 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions daiquiri/config/settings/base.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import os

import daiquiri.core.env as env
import saml2
import saml2.saml

import daiquiri.core.env as env

from . import (
ADDITIONAL_APPS,
AUTHENTICATION_BACKENDS,
Expand Down Expand Up @@ -66,7 +65,7 @@
# Default: None
SITE_UPDATED = "2024-06-13"

LINEA_APPS = ["djangosaml2", "services", "data"]
LINEA_APPS = ["djangosaml2", "services", "data", "utils"]

WAGTAIL_APPS = [
"wagtail.contrib.forms",
Expand Down
1,160 changes: 1,160 additions & 0 deletions daiquiri/fixtures/des_dr2.yml

Large diffs are not rendered by default.

890 changes: 890 additions & 0 deletions daiquiri/fixtures/gaia_dr3.yml

Large diffs are not rendered by default.

368 changes: 368 additions & 0 deletions daiquiri/fixtures/twomass.yml

Large diffs are not rendered by default.

Empty file added daiquiri/utils/__init__.py
Empty file.
6 changes: 6 additions & 0 deletions daiquiri/utils/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class UtilsConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "utils"
Empty file.
Empty file.
125 changes: 125 additions & 0 deletions daiquiri/utils/management/commands/update_table_metadata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
from datetime import datetime, timezone
from pathlib import Path

import yaml
from daiquiri.core.constants import ACCESS_LEVEL_PRIVATE
from daiquiri.metadata.models import Schema
from django.core.management.base import BaseCommand


class Command(BaseCommand):
help = "Updates table metadata from yml file."

# def add_arguments(self, parser):
# parser.add_argument(
# "--local",
# action="store_true",
# dest="local",
# default=False,
# help="Does not download the files, uses the local files in /data/asteroid_table",
# )

def add_arguments(self, parser):
parser.add_argument(
"schema",
help="schema to be updated, there must be a yml file for the schema in the utils/fixtures directory",
)

def handle(self, *args, **options):
self.stdout.write("Teste Update Metadata")
print(f"Schema: {options['schema']}")
fixtures_path = Path.cwd().joinpath("fixtures")
print(f"Fixtures Path: {fixtures_path}")

schema_yml = fixtures_path.joinpath(f"{options['schema']}.yml")
print(f"Schema YML: {schema_yml}")

if not schema_yml.exists():
self.stdout.write(f"File {schema_yml} not found.")
return

with open(schema_yml, "r") as file:
schemas = yaml.safe_load(file)

schema_data = schemas[0]

print(f"Geting daiquiri model for schema: {schema_data['name']}")
try:
schema_obj = Schema.objects.get(name=schema_data["name"])
schema_obj.title = schema_data.get("title", "").strip()
schema_obj.description = schema_data.get("description", "").strip()
schema_obj.long_description = schema_data.get(
"long_description", ""
).strip()
schema_obj.attribution = schema_data.get("attribution", "").strip()
schema_obj.license = schema_data.get("license", "PD").strip()
schema_obj.doi = schema_data.get("doi", "").strip()
schema_obj.order = schema_data.get("order", None)
schema_obj.published = schema_data.get("published", None)
schema_obj.updated = schema_data.get("updated", None)
schema_obj.access_level = schema_data.get(
"access_level", ACCESS_LEVEL_PRIVATE
).strip()
schema_obj.metadata_access_level = schema_data.get(
"metadata_access_level", ACCESS_LEVEL_PRIVATE.strip()
)
schema_obj.save()

print(
f"Updated schema metadata. ID: [{schema_obj.id}] Name: [{schema_obj.name}]"
)
except Exception as e:
print(f"Error updating schema metadata: {schema_data['name']}")
raise e

schema_obj.refresh_from_db()

# Iterate over schema table and update metadata
for table_data in schema_data["tables"]:
try:
table_obj = schema_obj.tables.get(name=table_data["name"])
table_obj.title = table_data["title"]
table_obj.description = table_data["description"]
table_obj.long_description = table_data.get("long_description", "")
table_obj.attribution = table_data.get("attribution", "")
table_obj.license = table_data.get("license", "PD")
table_obj.doi = table_data.get("doi", "")
table_obj.order = table_data.get("order", None)
table_obj.published = table_data.get("published", None)
table_obj.updated = table_data.get("updated", None)
table_obj.access_level = table_data.get(
"access_level", ACCESS_LEVEL_PRIVATE
).strip()
table_obj.metadata_access_level = table_data.get(
"metadata_access_level", ACCESS_LEVEL_PRIVATE.strip()
)
table_obj.save()

print(
f" Updated table metadata. ID: [{table_obj.id}] Name: [{table_obj.name}]"
)
except Exception as e:
print(f" Error updating table metadata: {table_data['name']}")
raise e

table_obj.refresh_from_db()

# Iterate over table columns and update metadata
print(f" QTD Columns: {len(table_data['columns'])}")
for column_data in table_data["columns"]:
try:
column_obj = table_obj.columns.get(name=column_data["name"])
column_obj.description = column_data.get("description", "")
column_obj.unit = column_data.get("unit", "")
column_obj.ucd = column_data.get("ucd", "")
column_obj.order = column_data.get("order", None)
column_obj.save()

print(
f" Updated column metadata. ID: [{column_obj.id}] Name: [{column_obj.name}]"
)
except Exception as e:
print(
f" Error updating column metadata: {column_data['name']}"
)
raise e
Empty file.
Loading
Loading