Skip to content

Commit

Permalink
fix: replace direct Config references with Flask's current_app.config
Browse files Browse the repository at this point in the history
This commit fixes the "Config is not defined" error by replacing direct references
to the Config class with Flask's current_app.config in the entity.py file. This
ensures proper access to configuration values through the Flask application context.
  • Loading branch information
arcangelo7 committed Feb 27, 2025
1 parent 38759f7 commit 0f75eca
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 40 deletions.
15 changes: 1 addition & 14 deletions config.example.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os
from enum import Enum

from heritrace.meta_counter_handler import MetaCounterHandler
from heritrace.uri_generator import DefaultURIGenerator, MetaURIGenerator
from heritrace.utils.strategies import OrphanHandlingStrategy, ProxyHandlingStrategy

# Base directory for the application
BASE_HERITRACE_DIR = os.path.abspath(os.path.dirname(__file__))
Expand All @@ -23,19 +23,6 @@
display_rules_path = os.path.join(BASE_HERITRACE_DIR, "display_rules.yaml")


# Entity handling strategies
class OrphanHandlingStrategy(Enum):
DELETE = "delete" # Automatically delete orphaned entities
ASK = "ask" # Ask the user before deleting orphaned entities
KEEP = "keep" # Keep orphaned entities (do nothing)


class ProxyHandlingStrategy(Enum):
DELETE = "delete" # Automatically delete proxy entities
ASK = "ask" # Ask the user before deleting proxy entities
KEEP = "keep" # Keep proxy entities (do nothing)


class Config(object):
# Application display settings
APP_TITLE = "Your App Title"
Expand Down
10 changes: 3 additions & 7 deletions heritrace/routes/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,26 @@
import traceback
from typing import Dict, Optional

import validators
from config import OrphanHandlingStrategy, ProxyHandlingStrategy
from flask import Blueprint, current_app, g, jsonify, request
from flask_babel import gettext
from flask_login import current_user, login_required
from heritrace.editor import Editor
from heritrace.extensions import (
get_custom_filter,
get_dataset_endpoint,
get_form_fields,
get_provenance_endpoint,
get_shacl_graph,
)
from heritrace.services.resource_lock_manager import LockStatus
from heritrace.utils.shacl_utils import get_valid_predicates, validate_new_triple
from heritrace.utils.shacl_utils import validate_new_triple
from heritrace.utils.sparql_utils import (
fetch_data_graph_for_subject,
find_orphaned_entities,
get_available_classes,
get_catalog_data,
get_deleted_entities_with_filtering,
import_entity_graph,
)
from rdflib import RDF, XSD, Literal, URIRef
from heritrace.utils.strategies import OrphanHandlingStrategy, ProxyHandlingStrategy
from rdflib import RDF, XSD, URIRef
from resources.datatypes import DATATYPE_MAPPING

api_bp = Blueprint("api", __name__)
Expand Down
10 changes: 5 additions & 5 deletions heritrace/routes/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
from typing import Dict, List, Optional, Tuple

import validators
from config import Config
from flask import (
Blueprint,
abort,
current_app,
flash,
g,
jsonify,
redirect,
render_template,
Expand Down Expand Up @@ -528,9 +526,11 @@ def determine_datatype(value, datatype_uris):

def generate_unique_uri(entity_type: URIRef | str = None):
entity_type = str(entity_type)
uri = Config.URI_GENERATOR.generate_uri(entity_type)
if hasattr(Config.URI_GENERATOR, "counter_handler"):
Config.URI_GENERATOR.counter_handler.increment_counter(entity_type)
uri = current_app.config["URI_GENERATOR"].generate_uri(entity_type)
if hasattr(current_app.config["URI_GENERATOR"], "counter_handler"):
current_app.config["URI_GENERATOR"].counter_handler.increment_counter(
entity_type
)
return URIRef(uri)


Expand Down
21 changes: 21 additions & 0 deletions heritrace/utils/strategies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
Strategies for handling orphaned entities and proxy relationships.
"""

from enum import Enum


class OrphanHandlingStrategy(Enum):
"""Strategy for handling orphaned entities."""

DELETE = "delete" # Automatically delete orphaned entities
ASK = "ask" # Ask the user before deleting orphaned entities
KEEP = "keep" # Keep orphaned entities (do nothing)


class ProxyHandlingStrategy(Enum):
"""Strategy for handling proxy entities."""

DELETE = "delete" # Automatically delete proxy entities
ASK = "ask" # Ask the user before deleting proxy entities
KEEP = "keep" # Keep proxy entities (do nothing)
15 changes: 1 addition & 14 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os
from enum import Enum

from heritrace.meta_counter_handler import MetaCounterHandler
from heritrace.uri_generator import DefaultURIGenerator, MetaURIGenerator
from heritrace.utils.strategies import OrphanHandlingStrategy, ProxyHandlingStrategy

# Base directory for the application
BASE_HERITRACE_DIR = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
Expand All @@ -23,19 +23,6 @@
display_rules_path = os.path.join(BASE_HERITRACE_DIR, "display_rules.yaml")


# Entity handling strategies
class OrphanHandlingStrategy(Enum):
DELETE = "delete" # Automatically delete orphaned entities
ASK = "ask" # Ask the user before deleting orphaned entities
KEEP = "keep" # Keep orphaned entities (do nothing)


class ProxyHandlingStrategy(Enum):
DELETE = "delete" # Automatically delete proxy entities
ASK = "ask" # Ask the user before deleting proxy entities
KEEP = "keep" # Keep proxy entities (do nothing)


class TestConfig(object):
# Application display settings
APP_TITLE = "Test App"
Expand Down
Binary file modified tests/test_dataset_db/virtuoso.trx
Binary file not shown.
Binary file modified tests/test_provenance_db/virtuoso.trx
Binary file not shown.

0 comments on commit 0f75eca

Please sign in to comment.