From 289b9b325f0d48a5b344ca29b84566f5b9220d9b Mon Sep 17 00:00:00 2001
From: Katherine Fleming <2205659+kflemin@users.noreply.github.com>
Date: Wed, 11 Nov 2026 11:14:35 -0700
Subject: [PATCH 1/3] Show children of ref'd global elements with a named
complexType
The schema tree viewer showed no children for elements used via
ref="auc:X" when the global element X is declared as
and XType is a named
xs:complexType (e.g. FanBased, TimeSeries, CalculationMethod,
PortfolioManager). _walk_named_element only followed the element's
type when it resolved to a simpleType.
- Walk the named complexType in _walk_named_element
- Add a parser test with a minimal XSD comparing a ref'd element to a
local name+type element of the same type
Fixes #229
Co-Authored-By: Claude Opus 5.5
---
bsyncviewer/lib/schema_parser.py | 10 +++
.../tests/data/test_ref_complex_type.xsd | 33 ++++++++++
bsyncviewer/tests/test_schema_parser.py | 61 +++++++++++++++++++
3 files changed, 104 insertions(+)
create mode 100644 bsyncviewer/tests/data/test_ref_complex_type.xsd
create mode 100644 bsyncviewer/tests/test_schema_parser.py
diff --git a/bsyncviewer/lib/schema_parser.py b/bsyncviewer/lib/schema_parser.py
index 7050af9..280b576 100644
--- a/bsyncviewer/lib/schema_parser.py
+++ b/bsyncviewer/lib/schema_parser.py
@@ -453,6 +453,16 @@ def _walk_named_element(
num_added += this_num_added
return_rows.extend(new_rows)
+ # named complexType linked to "type", e.g. a global
+ # reached via ref="auc:X"
+ elif isinstance(instance, ComplexTypeElement):
+ this_num_added, new_rows = self._walk_complex_element(
+ instance, root_path, current_tree_level, current_index
+ )
+ current_index += this_num_added
+ num_added += this_num_added
+ return_rows.extend(new_rows)
+
for elem in parent_element.annotations:
potential_doc_string = self._walk_annotation_element(elem)
diff --git a/bsyncviewer/tests/data/test_ref_complex_type.xsd b/bsyncviewer/tests/data/test_ref_complex_type.xsd
new file mode 100644
index 0000000..518a309
--- /dev/null
+++ b/bsyncviewer/tests/data/test_ref_complex_type.xsd
@@ -0,0 +1,33 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/bsyncviewer/tests/test_schema_parser.py b/bsyncviewer/tests/test_schema_parser.py
new file mode 100644
index 0000000..9997df1
--- /dev/null
+++ b/bsyncviewer/tests/test_schema_parser.py
@@ -0,0 +1,61 @@
+import os
+
+import xmlschema
+from django.test import SimpleTestCase
+
+from bsyncviewer.lib.schema_parser import BuildingSyncSchemaProcessor
+
+
+class TestSchemaParser(SimpleTestCase):
+ def setUp(self):
+ xsd = os.path.join(
+ os.path.dirname(__file__), "data", "test_ref_complex_type.xsd"
+ )
+ processor = BuildingSyncSchemaProcessor(xmlschema.XMLSchema(xsd))
+ self.rows = processor.walk_root_element()
+
+ def _children(self, parent_path):
+ return [r["name"] for r in self.rows if r["parent_path"] == parent_path]
+
+ def _paths(self):
+ return {r["path"] for r in self.rows}
+
+ def test_ref_to_global_element_with_named_complex_type(self):
+ # ->
+ self.assertEqual(
+ self._children("BuildingSync.FanBased"),
+ ["FanBasedDistributionType", "AirSideEconomizer"],
+ )
+ self.assertEqual(
+ self._children("BuildingSync.FanBased.FanBasedDistributionType"),
+ ["AirDistribution", "FanCoil"],
+ )
+ self.assertEqual(
+ self._children("BuildingSync.FanBased.AirSideEconomizer"),
+ ["Dry bulb temperature", "None"],
+ )
+
+ def test_ref_matches_local_named_element_with_same_type(self):
+ # the ref'd element and a local name+type element of the same type expand identically
+ ref_paths = {
+ p[len("BuildingSync.FanBased") :]
+ for p in self._paths()
+ if p.startswith("BuildingSync.FanBased.")
+ }
+ local_paths = {
+ p[len("BuildingSync.LocalFanBased") :]
+ for p in self._paths()
+ if p.startswith("BuildingSync.LocalFanBased.")
+ }
+ self.assertGreater(len(ref_paths), 0)
+ self.assertEqual(ref_paths, local_paths)
+
+ def test_tree_levels_and_indexes(self):
+ # enumeration rows reuse their parent's path, so leave them out of the lookup
+ by_path = {r["path"]: r for r in self.rows if r["type"] != "Enumeration"}
+ self.assertEqual(by_path["BuildingSync.FanBased"]["$$treeLevel"], 0)
+ self.assertEqual(
+ by_path["BuildingSync.FanBased.AirSideEconomizer"]["$$treeLevel"], 1
+ )
+ indexes = [r["index"] for r in self.rows]
+ self.assertEqual(indexes, list(range(1, len(self.rows) + 1)))
From 11e596b78188468ef503a802dec039a5160862db Mon Sep 17 00:00:00 2001
From: Katherine Fleming <2205659+kflemin@users.noreply.github.com>
Date: Tue, 17 Nov 2026 15:20:00 -0700
Subject: [PATCH 2/3] add reparse schema and parsing of complex types
---
README.md | 11 +
.../management/commands/reparse_schema.py | 252 ++++++++++++++++++
bsyncviewer/tests/test_reparse_schema.py | 168 ++++++++++++
docker/start_server.sh | 9 +-
4 files changed, 437 insertions(+), 3 deletions(-)
create mode 100644 bsyncviewer/management/commands/reparse_schema.py
create mode 100644 bsyncviewer/tests/test_reparse_schema.py
diff --git a/README.md b/README.md
index bd97cf9..640076e 100644
--- a/README.md
+++ b/README.md
@@ -50,10 +50,21 @@ This is the repository for the BuildingSync Validator web application.
```
- To test schema import, you can run this command:
+
```bash
python manage.py reset_schema --schema_version=2.6.0
```
+ `reset_schema` deletes the schema record, which also deletes the use cases linked to that version.
+
+- To rebuild the parsed tree of a schema that is already loaded (for example after a parser fix), use `reparse_schema` instead. It keeps the schema record, its use cases and its uploaded file, and carries the existing BEDES term and enumeration mappings over to the new rows by attribute path and enumeration name. Start with `--dry-run`, which reports what would change and then rolls back:
+ ```bash
+ python manage.py reparse_schema --schema_version 2.7.0 --dry-run
+ python manage.py reparse_schema --schema_version 2.7.0 --schema_version 2.6.0
+ python manage.py reparse_schema --all
+ ```
+ Re-parsing gives every attribute and enumeration a new database ID. The BEDES CSV files in `bsyncviewer/lib/bedes/` reference those IDs, so after a re-parse they no longer match the database and `bedes --save_to_db` skips their rows. The mappings already in the database are carried over, so the CSVs only need regenerating (with `bedes --schema_version --bedes_version v2.5`) to bring the files back in sync or to map attributes the re-parse added.
+
**NOTE**: You will need to be _off_ the VPN to import a schema, otherwise you'll get an error related to `http://www.gbxml.org/schema}Area'` as the gbXML schema cannot be imported when on the VPN.
### Pre-commit
diff --git a/bsyncviewer/management/commands/reparse_schema.py b/bsyncviewer/management/commands/reparse_schema.py
new file mode 100644
index 0000000..76af237
--- /dev/null
+++ b/bsyncviewer/management/commands/reparse_schema.py
@@ -0,0 +1,252 @@
+import os
+from collections import defaultdict
+
+from django.core.management.base import BaseCommand, CommandError
+from django.db import transaction
+
+from bsyncviewer.models.attribute import Attribute
+from bsyncviewer.models.attribute_enumeration_class import AttributeEnumerationClass
+from bsyncviewer.models.bedes_models import BedesEnumerationMapping, BedesMapping
+from bsyncviewer.models.enumeration import Enumeration
+from bsyncviewer.models.enumeration_class import EnumerationClass
+from bsyncviewer.models.schema import Schema
+
+# how many unrestorable mappings to list per version before summarizing
+MAX_LISTED = 25
+
+
+class Command(BaseCommand):
+ help = (
+ "Rebuild the parsed attribute tree and enumerations of existing schema versions in place. "
+ "Unlike reset_schema, the Schema row is kept, so its use cases and uploaded file are preserved. "
+ "BEDES term and enumeration mappings are carried over to the new rows by attribute path and "
+ "by (attribute path, enumeration name), falling back to the enumeration class name."
+ )
+
+ def add_arguments(self, parser):
+ group = parser.add_mutually_exclusive_group(required=True)
+ group.add_argument(
+ "--schema_version",
+ action="append",
+ dest="schema_versions",
+ metavar="VERSION",
+ help="Schema version to re-parse. Repeat to re-parse several versions.",
+ )
+ group.add_argument(
+ "--all", action="store_true", help="Re-parse every schema version."
+ )
+ parser.add_argument(
+ "--dry-run",
+ action="store_true",
+ help="Re-parse and report what would change, then roll back all database changes.",
+ )
+
+ def handle(self, *args, **options):
+ if options["all"]:
+ schemas = list(Schema.objects.order_by("id"))
+ else:
+ schemas = []
+ for version in options["schema_versions"]:
+ schema = Schema.objects.filter(version=version).first()
+ if not schema:
+ raise CommandError("Schema version %s does not exist" % version)
+ schemas.append(schema)
+
+ for schema in schemas:
+ if not schema.schema_file or not os.path.isfile(schema.schema_file.path):
+ raise CommandError(
+ "Schema version %s has no schema file on disk; nothing was changed"
+ % schema.version
+ )
+
+ for schema in schemas:
+ self._reparse(schema, options["dry_run"])
+
+ if options["dry_run"]:
+ self.stdout.write(
+ self.style.WARNING("Dry run: all database changes were rolled back.")
+ )
+
+ def _reparse(self, schema, dry_run):
+ self.stdout.write("Re-parsing schema %s" % schema.version)
+
+ with transaction.atomic():
+ # 1. Snapshot BEDES mappings by stable keys (DB ids change on re-parse)
+ old_attribute_count = Attribute.objects.filter(schema=schema).count()
+ old_enumeration_count = Enumeration.objects.filter(schema=schema).count()
+
+ term_mappings = defaultdict(set) # path -> {(bedes_term_id, match_type)}
+ for path, term_id, match_type in BedesMapping.objects.filter(
+ attribute__schema=schema
+ ).values_list("attribute__path", "bedesTerm_id", "match_type"):
+ term_mappings[path].add((term_id, match_type))
+
+ # Enumeration class names are not stable: a class is renamed "Parent::Leaf" once
+ # its leaf name appears under more than one parent. So key enumerations by the
+ # attribute paths that use them, and fall back to the class name.
+ old_class_paths = self._class_paths(schema)
+ enum_mappings = {} # old enum id -> {paths, class_name, name, bedes_ids}
+ for (
+ enum_id,
+ class_id,
+ class_name,
+ enum_name,
+ bedes_enum_id,
+ ) in BedesEnumerationMapping.objects.filter(
+ enumeration__schema=schema
+ ).values_list(
+ "enumeration_id",
+ "enumeration__enumeration_class_id",
+ "enumeration__enumeration_class__name",
+ "enumeration__name",
+ "bedesEnumeration_id",
+ ):
+ record = enum_mappings.setdefault(
+ enum_id,
+ {
+ "paths": old_class_paths.get(class_id, set()),
+ "class_name": class_name,
+ "name": enum_name,
+ "bedes_ids": set(),
+ },
+ )
+ record["bedes_ids"].add(bedes_enum_id)
+
+ # 2. Delete the parsed rows only. This cascades to the BEDES mappings and the
+ # attribute/enumeration-class links, but not to the Schema or its use cases.
+ Attribute.objects.filter(schema=schema).delete()
+ Enumeration.objects.filter(schema=schema).delete()
+ EnumerationClass.objects.filter(schema=schema).delete()
+
+ # 3. Re-parse: the Schema post_save signal parses the file when schema_parsed is False
+ schema.schema_parsed = False
+ schema.save()
+ schema.refresh_from_db()
+ if not schema.schema_parsed:
+ raise CommandError(
+ "Schema %s was not re-parsed; rolled back" % schema.version
+ )
+
+ # 4. Restore BEDES term mappings by attribute path
+ new_attribute_ids = defaultdict(list)
+ for attribute_id, path in Attribute.objects.filter(
+ schema=schema
+ ).values_list("id", "path"):
+ new_attribute_ids[path].append(attribute_id)
+
+ new_term_mappings = []
+ lost_paths = []
+ for path, mappings in term_mappings.items():
+ if path not in new_attribute_ids:
+ lost_paths.append(path)
+ continue
+ for attribute_id in new_attribute_ids[path]:
+ for term_id, match_type in mappings:
+ new_term_mappings.append(
+ BedesMapping(
+ attribute_id=attribute_id,
+ bedesTerm_id=term_id,
+ match_type=match_type,
+ )
+ )
+ BedesMapping.objects.bulk_create(new_term_mappings)
+
+ # 5. Restore BEDES enumeration mappings by (attribute path, enumeration name),
+ # falling back to (enumeration class name, enumeration name)
+ new_class_paths = self._class_paths(schema)
+ new_by_path = defaultdict(set) # (path, enum name) -> {new enum id}
+ new_by_class = {} # (class name, enum name) -> new enum id
+ new_enum_count = 0
+ for enum_id, class_id, class_name, enum_name in Enumeration.objects.filter(
+ schema=schema
+ ).values_list(
+ "id", "enumeration_class_id", "enumeration_class__name", "name"
+ ):
+ new_enum_count += 1
+ new_by_class[(class_name, enum_name)] = enum_id
+ for path in new_class_paths.get(class_id, ()):
+ new_by_path[(path, enum_name)].add(enum_id)
+
+ new_enum_pairs = set() # {(new enum id, bedes enum id)}
+ lost_enums = []
+ for record in enum_mappings.values():
+ targets = set()
+ for path in record["paths"]:
+ targets |= new_by_path.get((path, record["name"]), set())
+ if (
+ not targets
+ and (record["class_name"], record["name"]) in new_by_class
+ ):
+ targets.add(new_by_class[(record["class_name"], record["name"])])
+ if not targets:
+ lost_enums.append((record["class_name"], record["name"]))
+ continue
+ for target in targets:
+ for bedes_enum_id in record["bedes_ids"]:
+ new_enum_pairs.add((target, bedes_enum_id))
+ new_enum_mappings = [
+ BedesEnumerationMapping(enumeration_id=e, bedesEnumeration_id=b)
+ for e, b in sorted(new_enum_pairs)
+ ]
+ BedesEnumerationMapping.objects.bulk_create(new_enum_mappings)
+
+ # 6. Report
+ new_paths = set(new_attribute_ids)
+ self.stdout.write(
+ " attributes: %s -> %s, enumerations: %s -> %s"
+ % (
+ old_attribute_count,
+ sum(len(ids) for ids in new_attribute_ids.values()),
+ old_enumeration_count,
+ new_enum_count,
+ )
+ )
+ self.stdout.write(
+ " BEDES term mappings restored: %s (on %s of %s mapped paths)"
+ % (
+ len(new_term_mappings),
+ len(term_mappings) - len(lost_paths),
+ len(term_mappings),
+ )
+ )
+ self.stdout.write(
+ " BEDES enumeration mappings restored: %s (on %s of %s mapped enumerations)"
+ % (
+ len(new_enum_mappings),
+ len(enum_mappings) - len(lost_enums),
+ len(enum_mappings),
+ )
+ )
+ self.stdout.write(
+ " attributes without a BEDES term mapping: %s"
+ % len(new_paths - set(term_mappings))
+ )
+ self._write_lost(
+ "BEDES term mappings not restored (path no longer exists)", lost_paths
+ )
+ self._write_lost(
+ "BEDES enumeration mappings not restored (enumeration no longer exists)",
+ ["%s: %s" % key for key in lost_enums],
+ )
+
+ if dry_run:
+ transaction.set_rollback(True)
+
+ @staticmethod
+ def _class_paths(schema):
+ """Return {enumeration class id: {attribute paths that use the class}}."""
+ class_paths = defaultdict(set)
+ for class_id, path in AttributeEnumerationClass.objects.filter(
+ attribute__schema=schema
+ ).values_list("enumeration_class_id", "attribute__path"):
+ class_paths[class_id].add(path)
+ return class_paths
+
+ def _write_lost(self, label, items):
+ if not items:
+ return
+ self.stdout.write(self.style.WARNING(" %s: %s" % (label, len(items))))
+ for item in sorted(items)[:MAX_LISTED]:
+ self.stdout.write(" %s" % item)
+ if len(items) > MAX_LISTED:
+ self.stdout.write(" ... and %s more" % (len(items) - MAX_LISTED))
diff --git a/bsyncviewer/tests/test_reparse_schema.py b/bsyncviewer/tests/test_reparse_schema.py
new file mode 100644
index 0000000..98b7116
--- /dev/null
+++ b/bsyncviewer/tests/test_reparse_schema.py
@@ -0,0 +1,168 @@
+import os
+from io import StringIO
+
+from django.core.files.uploadedfile import SimpleUploadedFile
+from django.core.management import call_command
+from django.core.management.base import CommandError
+from django.test import TestCase
+
+from bsyncviewer.models.attribute import Attribute
+from bsyncviewer.models.bedes_models import (
+ BedesEnumeration,
+ BedesEnumerationMapping,
+ BedesMapping,
+ BedesTerm,
+)
+from bsyncviewer.models.enumeration import Enumeration
+from bsyncviewer.models.schema import Schema
+from bsyncviewer.models.use_case import UseCase
+
+# Use a custom version that is not an actual version to prevent overwriting saved BEDES mappings
+TEST_SCHEMA_VERSION = "0.0.2"
+MAPPED_PATH = "BuildingSync.FanBased.AirSideEconomizer"
+
+
+class TestReparseSchemaCommand(TestCase):
+ def setUp(self):
+ sf = os.path.join(
+ os.path.dirname(__file__), "data", "test_ref_complex_type.xsd"
+ )
+ with open(sf, "rb") as file:
+ uploaded = SimpleUploadedFile(file.name, file.read())
+ self.schema = Schema(
+ name="Version {}".format(TEST_SCHEMA_VERSION),
+ version=TEST_SCHEMA_VERSION,
+ schema_file=uploaded,
+ )
+ self.schema.save() # Calling save also processes the schema
+
+ self.use_case = UseCase.objects.create(name="reparse-test", schema=self.schema)
+
+ self.term = BedesTerm.objects.create(
+ content_uuid="term-uuid", term="Economizer", category="", url=""
+ )
+ self.attribute = Attribute.objects.get(schema=self.schema, path=MAPPED_PATH)
+ BedesMapping.objects.create(
+ attribute=self.attribute, bedesTerm=self.term, match_type="Term"
+ )
+
+ self.enumeration = Enumeration.objects.filter(
+ schema=self.schema, name="Dry bulb temperature"
+ ).first()
+ self.enum_class_name = self.enumeration.enumeration_class.name
+ self.bedes_enum = BedesEnumeration.objects.create(
+ content_uuid="enum-uuid", term="Dry Bulb", url="", related_term_uuid=""
+ )
+ BedesEnumerationMapping.objects.create(
+ enumeration=self.enumeration, bedesEnumeration=self.bedes_enum
+ )
+
+ def tearDown(self):
+ if self.schema and self.schema.id is not None:
+ self.schema.delete()
+
+ def _reparse(self, *args):
+ out = StringIO()
+ call_command(
+ "reparse_schema", "--schema_version", TEST_SCHEMA_VERSION, *args, stdout=out
+ )
+ return out.getvalue()
+
+ def test_reparse_rebuilds_tree_and_keeps_schema_and_use_cases(self):
+ old_ids = set(
+ Attribute.objects.filter(schema=self.schema).values_list("id", flat=True)
+ )
+ old_paths = sorted(
+ Attribute.objects.filter(schema=self.schema).values_list("path", flat=True)
+ )
+
+ self._reparse()
+
+ attributes = Attribute.objects.filter(schema=self.schema)
+ self.assertTrue(Schema.objects.filter(pk=self.schema.pk).exists())
+ self.assertTrue(UseCase.objects.filter(pk=self.use_case.pk).exists())
+ self.assertEqual(sorted(attributes.values_list("path", flat=True)), old_paths)
+ self.assertEqual(attributes.filter(path="BuildingSync").count(), 1)
+ self.assertFalse(old_ids & set(attributes.values_list("id", flat=True)))
+ self.assertTrue(Schema.objects.get(pk=self.schema.pk).schema_parsed)
+
+ def test_reparse_restores_bedes_mappings(self):
+ self._reparse()
+
+ mapping = BedesMapping.objects.get(attribute__schema=self.schema)
+ self.assertEqual(mapping.attribute.path, MAPPED_PATH)
+ self.assertNotEqual(mapping.attribute_id, self.attribute.id)
+ self.assertEqual(mapping.bedesTerm_id, self.term.id)
+ self.assertEqual(mapping.match_type, "Term")
+
+ enum_mapping = BedesEnumerationMapping.objects.get(
+ enumeration__schema=self.schema
+ )
+ self.assertEqual(enum_mapping.enumeration.name, "Dry bulb temperature")
+ self.assertEqual(
+ enum_mapping.enumeration.enumeration_class.name, self.enum_class_name
+ )
+ self.assertNotEqual(enum_mapping.enumeration_id, self.enumeration.id)
+ self.assertEqual(enum_mapping.bedesEnumeration_id, self.bedes_enum.id)
+
+ def test_reparse_restores_enum_mapping_when_class_is_renamed(self):
+ # class names become "Parent::Leaf" when a leaf name gains a second parent, so the
+ # mapping must be carried over by attribute path, not only by class name
+ enum_class = self.enumeration.enumeration_class
+ enum_class.name = "Renamed::AirSideEconomizer"
+ enum_class.save()
+
+ self._reparse()
+
+ enum_mapping = BedesEnumerationMapping.objects.get(
+ enumeration__schema=self.schema
+ )
+ self.assertEqual(enum_mapping.enumeration.name, "Dry bulb temperature")
+ self.assertNotEqual(
+ enum_mapping.enumeration.enumeration_class.name, enum_class.name
+ )
+ self.assertEqual(enum_mapping.bedesEnumeration_id, self.bedes_enum.id)
+
+ def test_reparse_reports_mappings_that_cannot_be_restored(self):
+ gone = Attribute.objects.create(
+ name="Gone",
+ schema=self.schema,
+ tree_level=1,
+ path="BuildingSync.Gone",
+ )
+ BedesMapping.objects.create(attribute=gone, bedesTerm=self.term)
+
+ output = self._reparse()
+
+ self.assertIn("not restored", output)
+ self.assertIn("BuildingSync.Gone", output)
+ self.assertEqual(
+ BedesMapping.objects.filter(attribute__schema=self.schema).count(), 1
+ )
+
+ def test_dry_run_changes_nothing(self):
+ old_ids = sorted(
+ Attribute.objects.filter(schema=self.schema).values_list("id", flat=True)
+ )
+
+ output = self._reparse("--dry-run")
+
+ self.assertIn("Dry run", output)
+ self.assertEqual(
+ sorted(
+ Attribute.objects.filter(schema=self.schema).values_list(
+ "id", flat=True
+ )
+ ),
+ old_ids,
+ )
+ self.assertEqual(
+ BedesMapping.objects.get(attribute__schema=self.schema).attribute_id,
+ self.attribute.id,
+ )
+
+ def test_unknown_version_raises(self):
+ with self.assertRaises(CommandError):
+ call_command(
+ "reparse_schema", "--schema_version", "9.9.9", stdout=StringIO()
+ )
diff --git a/docker/start_server.sh b/docker/start_server.sh
index 6097c83..2be8f2a 100755
--- a/docker/start_server.sh
+++ b/docker/start_server.sh
@@ -9,9 +9,12 @@ echo "Waiting for postgres to start"
./manage.py migrate
./manage.py collectstatic --noinput
-# Check if a schema has been imported
-./manage.py reset_schema --schema_version 2.6.0
-./manage.py bedes --schema_version=2.6.0 --bedes_version=v2.5 --save_to_db
+# On a new database, import the default schema version (settings.DEFAULT_SCHEMA_VERSION).
+# This is a no-op when that version already exists.
+# BEDES mappings are not loaded here: the CSVs in bsyncviewer/lib/bedes reference database
+# IDs from the database they were generated in, so they only match that database. Load
+# them manually with `./manage.py bedes --schema_version= --save_to_db` if needed.
+./manage.py reset_schema
# Create the default user based on the env vars
echo "from django.contrib.auth import get_user_model; User = get_user_model(); User.objects.create_superuser('${BUILDINGSYNC_WEBSITE_ADMIN_USER}', '${BUILDINGSYNC_WEBSITE_ADMIN_EMAIL}', '${BUILDINGSYNC_WEBSITE_ADMIN_PASSWORD}')" | python manage.py shell
From a9eeb28ec05b06112e3534fd4611cd280b6e1959 Mon Sep 17 00:00:00 2001
From: kflemin <2205659+kflemin@users.noreply.github.com>
Date: Tue, 17 Nov 2026 22:38:07 +0000
Subject: [PATCH 3/3] Address schema and BEDES review feedback
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
---
README.md | 6 +++++-
bsyncviewer/management/commands/reparse_schema.py | 9 +++++----
bsyncviewer/tests/test_reparse_schema.py | 5 ++++-
docker/start_server.sh | 7 ++++---
4 files changed, 18 insertions(+), 9 deletions(-)
diff --git a/README.md b/README.md
index 640076e..d9a8438 100644
--- a/README.md
+++ b/README.md
@@ -63,7 +63,11 @@ This is the repository for the BuildingSync Validator web application.
python manage.py reparse_schema --schema_version 2.7.0 --schema_version 2.6.0
python manage.py reparse_schema --all
```
- Re-parsing gives every attribute and enumeration a new database ID. The BEDES CSV files in `bsyncviewer/lib/bedes/` reference those IDs, so after a re-parse they no longer match the database and `bedes --save_to_db` skips their rows. The mappings already in the database are carried over, so the CSVs only need regenerating (with `bedes --schema_version --bedes_version v2.5`) to bring the files back in sync or to map attributes the re-parse added.
+ Re-parsing gives every attribute and enumeration a new database ID. BEDES CSV files reference database-specific IDs, so regenerate them against the target database before loading them. For example, for schema 2.7.0:
+ ```bash
+ python manage.py bedes --schema_version 2.7.0 --bedes_version v2.5
+ python manage.py bedes --schema_version 2.7.0 --bedes_version v2.5 --save_to_db
+ ```
**NOTE**: You will need to be _off_ the VPN to import a schema, otherwise you'll get an error related to `http://www.gbxml.org/schema}Area'` as the gbXML schema cannot be imported when on the VPN.
diff --git a/bsyncviewer/management/commands/reparse_schema.py b/bsyncviewer/management/commands/reparse_schema.py
index 76af237..3c14bc2 100644
--- a/bsyncviewer/management/commands/reparse_schema.py
+++ b/bsyncviewer/management/commands/reparse_schema.py
@@ -4,6 +4,7 @@
from django.core.management.base import BaseCommand, CommandError
from django.db import transaction
+from bsyncviewer.lib.schema_parser import process_schema
from bsyncviewer.models.attribute import Attribute
from bsyncviewer.models.attribute_enumeration_class import AttributeEnumerationClass
from bsyncviewer.models.bedes_models import BedesEnumerationMapping, BedesMapping
@@ -118,10 +119,10 @@ def _reparse(self, schema, dry_run):
Enumeration.objects.filter(schema=schema).delete()
EnumerationClass.objects.filter(schema=schema).delete()
- # 3. Re-parse: the Schema post_save signal parses the file when schema_parsed is False
- schema.schema_parsed = False
- schema.save()
- schema.refresh_from_db()
+ # 3. Re-parse without invoking the post_save signal's documentation generation.
+ process_schema(schema)
+ schema.schema_parsed = True
+ schema.save(update_fields=["schema_parsed"])
if not schema.schema_parsed:
raise CommandError(
"Schema %s was not re-parsed; rolled back" % schema.version
diff --git a/bsyncviewer/tests/test_reparse_schema.py b/bsyncviewer/tests/test_reparse_schema.py
index 98b7116..e78610c 100644
--- a/bsyncviewer/tests/test_reparse_schema.py
+++ b/bsyncviewer/tests/test_reparse_schema.py
@@ -1,5 +1,6 @@
import os
from io import StringIO
+from unittest.mock import patch
from django.core.files.uploadedfile import SimpleUploadedFile
from django.core.management import call_command
@@ -145,7 +146,9 @@ def test_dry_run_changes_nothing(self):
Attribute.objects.filter(schema=self.schema).values_list("id", flat=True)
)
- output = self._reparse("--dry-run")
+ with patch("bsyncviewer.models.schema.generate_docs") as generate_docs:
+ output = self._reparse("--dry-run")
+ generate_docs.assert_not_called()
self.assertIn("Dry run", output)
self.assertEqual(
diff --git a/docker/start_server.sh b/docker/start_server.sh
index 2be8f2a..f4eb765 100755
--- a/docker/start_server.sh
+++ b/docker/start_server.sh
@@ -11,9 +11,10 @@ echo "Waiting for postgres to start"
# On a new database, import the default schema version (settings.DEFAULT_SCHEMA_VERSION).
# This is a no-op when that version already exists.
-# BEDES mappings are not loaded here: the CSVs in bsyncviewer/lib/bedes reference database
-# IDs from the database they were generated in, so they only match that database. Load
-# them manually with `./manage.py bedes --schema_version= --save_to_db` if needed.
+# BEDES mappings are not loaded here because mapping CSVs contain database-specific IDs.
+# To generate mappings for this database and load them, run:
+# ./manage.py bedes --schema_version=2.7.0 --bedes_version=v2.5
+# ./manage.py bedes --schema_version=2.7.0 --bedes_version=v2.5 --save_to_db
./manage.py reset_schema
# Create the default user based on the env vars