|
12 | 12 | Gtfsfeed, |
13 | 13 | Notice, |
14 | 14 | Feature, |
| 15 | + License, |
15 | 16 | t_feedsearch, |
16 | 17 | Location, |
17 | 18 | Officialstatushistory, |
18 | 19 | Gbfsversion, |
19 | 20 | Gbfsendpoint, |
20 | 21 | Gbfsfeed, |
| 22 | + Rule, |
21 | 23 | ) |
22 | 24 | from scripts.populate_db import set_up_configs, DatabasePopulateHelper |
23 | 25 | from typing import TYPE_CHECKING |
@@ -55,6 +57,51 @@ def populate_test_datasets(self, filepath, db_session: "Session"): |
55 | 57 | with open(filepath) as f: |
56 | 58 | data = json.load(f) |
57 | 59 |
|
| 60 | + # Licenses (populate license table first so feeds can reference them) |
| 61 | + if "licenses" in data: |
| 62 | + for lic in data["licenses"]: |
| 63 | + # skip if id missing |
| 64 | + lic_id = lic.get("id") |
| 65 | + if not lic_id: |
| 66 | + continue |
| 67 | + existing = db_session.get(License, lic_id) |
| 68 | + if existing: |
| 69 | + # optionally update existing fields if needed |
| 70 | + continue |
| 71 | + license_obj = License( |
| 72 | + id=lic_id, |
| 73 | + type=lic.get("type", "standard"), |
| 74 | + is_spdx=lic.get("is_spdx", False), |
| 75 | + name=lic.get("name"), |
| 76 | + url=lic.get("url"), |
| 77 | + description=lic.get("description"), |
| 78 | + content_txt=lic.get("content_txt"), |
| 79 | + content_html=lic.get("content_html"), |
| 80 | + created_at=lic.get("created_at"), |
| 81 | + updated_at=lic.get("updated_at"), |
| 82 | + ) |
| 83 | + db_session.add(license_obj) |
| 84 | + db_session.commit() |
| 85 | + |
| 86 | + # Rules (optional section to seed rule metadata used by license_rules) |
| 87 | + if "rules" in data: |
| 88 | + for rule in data["rules"]: |
| 89 | + rule_name = rule.get("name") |
| 90 | + if not rule_name: |
| 91 | + continue |
| 92 | + existing_rule = db_session.get(Rule, rule_name) |
| 93 | + if existing_rule: |
| 94 | + continue |
| 95 | + db_session.add( |
| 96 | + Rule( |
| 97 | + name=rule_name, |
| 98 | + label=rule.get("label") or rule_name, |
| 99 | + type=rule.get("type") or "permission", |
| 100 | + description=rule.get("description"), |
| 101 | + ) |
| 102 | + ) |
| 103 | + db_session.commit() |
| 104 | + |
58 | 105 | # GTFS Feeds |
59 | 106 | if "feeds" in data: |
60 | 107 | self.populate_test_feeds(data["feeds"], db_session) |
@@ -130,6 +177,29 @@ def populate_test_datasets(self, filepath, db_session: "Session"): |
130 | 177 | db_session.query(Feature).filter(Feature.name == report_features["feature_name"]).first() |
131 | 178 | ) |
132 | 179 |
|
| 180 | + # License rules: populate association table by creating missing Rule rows and attaching them to License |
| 181 | + if "license_rules" in data: |
| 182 | + for lr in data["license_rules"]: |
| 183 | + license_id = lr.get("license_id") |
| 184 | + rule_id = lr.get("rule_id") |
| 185 | + if not license_id or not rule_id: |
| 186 | + continue |
| 187 | + license_obj = db_session.get(License, license_id) |
| 188 | + if not license_obj: |
| 189 | + self.logger.error(f"No license found with id: {license_id}; skipping license_rule {rule_id}") |
| 190 | + continue |
| 191 | + rule_obj = db_session.get(Rule, rule_id) |
| 192 | + if not rule_obj: |
| 193 | + # Create a minimal Rule entry; label and type set conservatively |
| 194 | + rule_obj = Rule(name=rule_id, label=rule_id, type="permission", description=None) |
| 195 | + db_session.add(rule_obj) |
| 196 | + # flush so the relationship can reference it immediately |
| 197 | + db_session.flush() |
| 198 | + # Attach if not already associated |
| 199 | + if rule_obj not in license_obj.rules: |
| 200 | + license_obj.rules.append(rule_obj) |
| 201 | + db_session.commit() |
| 202 | + |
133 | 203 | # GBFS version |
134 | 204 | if "gbfs_versions" in data: |
135 | 205 | for version in data["gbfs_versions"]: |
@@ -180,9 +250,13 @@ def populate_test_feeds(self, feeds_data, db_session: "Session"): |
180 | 250 | note=feed_data["note"], |
181 | 251 | authentication_info_url=None, |
182 | 252 | api_key_parameter_name=None, |
183 | | - license_url=None, |
| 253 | + license_url=feed_data["source_info"]["license_url"], |
184 | 254 | feed_contact_email=feed_data["feed_contact_email"], |
185 | 255 | producer_url=feed_data["source_info"]["producer_url"], |
| 256 | + # license_id may be missing or an empty string; coerce empty -> None to avoid FK violation |
| 257 | + license_id=(feed_data["source_info"].get("license_id") or None), |
| 258 | + # allow empty notes to stay as empty string; coerce if you prefer None |
| 259 | + license_notes=(feed_data["source_info"].get("license_notes") or None), |
186 | 260 | operational_status="published", |
187 | 261 | ) |
188 | 262 | locations = [] |
|
0 commit comments