Skip to content

Commit

Permalink
Update model generation to relational models.yml (#2337)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsangmeister authored Apr 19, 2024
1 parent bcc3ca3 commit 819fe4b
Show file tree
Hide file tree
Showing 7 changed files with 377 additions and 151 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/continuous_integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ jobs:
- name: Install requirements
run: . requirements/export_service_commits.sh && pip install --requirement requirements/requirements_development.txt

# - name: Check for up to date models.py
# if: always()
# run: make check-models
- name: Check for up to date models.py
if: always()
run: make check-models

- name: Check for up to date permissions.py
if: always()
Expand Down
48 changes: 27 additions & 21 deletions cli/generate_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ class Attribute(Node):
default: Any = None
on_delete: OnDelete | None = None
equal_fields: str | list[str] | None = None
contraints: dict[str, Any]
constraints: dict[str, Any]
is_view_field: bool = False

FIELD_TEMPLATE = string.Template(
" ${field_name} = fields.${field_class}(${properties})\n"
Expand All @@ -210,38 +211,41 @@ def __init__(self, value: str | dict) -> None:
**COMMON_FIELD_CLASSES,
**RELATION_FIELD_CLASSES,
}
self.contraints = {}
self.constraints = {}
self.in_array_constraints = {}
if isinstance(value, str):
self.type = value
else:
self.type = value.get("type", "")
self.type = value.pop("type")
if self.type in RELATION_FIELD_CLASSES.keys():
self.to = To(value.get("to", {}))
self.on_delete = value.get("on_delete")
self.to = To(value.pop("to"))
self.on_delete = value.pop("on_delete", None)
if (
self.type.endswith("relation-list")
or value.get("sql")
or (value.get("to") and not value.get("reference"))
):
self.is_view_field = True
else:
assert self.type in COMMON_FIELD_CLASSES.keys(), (
"Invalid type: " + self.type
)
self.required = value.get("required", False)
self.read_only = value.get("read_only", False)
self.constant = value.get("constant", False)
self.default = value.get("default")
self.equal_fields = value.get("equal_fields")
self.required = value.pop("required", False)
self.read_only = value.pop("read_only", False)
self.constant = value.pop("constant", False)
self.default = value.pop("default", None)
self.equal_fields = value.pop("equal_fields", None)
for k, v in value.items():
if k not in (
"type",
"to",
"required",
"read_only",
"constant",
"default",
"on_delete",
"equal_fields",
"items",
"restriction_mode",
# database metadata
"reference",
"sql",
"deferred",
"unique",
):
self.contraints[k] = v
self.constraints[k] = v
elif self.type in ("string[]", "number[]") and k == "items":
self.in_array_constraints.update(v)

Expand All @@ -256,6 +260,8 @@ def get_code(self, field_name: str) -> str:
if self.on_delete:
assert self.on_delete in [mode for mode in OnDelete]
properties += f"on_delete=fields.OnDelete.{self.on_delete}, "
if self.is_view_field:
properties += "is_view_field=True, "
if self.required:
properties += "required=True, "
if self.read_only:
Expand All @@ -266,8 +272,8 @@ def get_code(self, field_name: str) -> str:
properties += f"default={repr(self.default)}, "
if self.equal_fields is not None:
properties += f"equal_fields={repr(self.equal_fields)}, "
if self.contraints:
properties += f"constraints={repr(self.contraints)}, "
if self.constraints:
properties += f"constraints={repr(self.constraints)}, "
if self.in_array_constraints and self.type in ("string[]", "number[]"):
properties += f"in_array_constraints={repr(self.in_array_constraints)}"
return self.FIELD_TEMPLATE.substitute(
Expand Down
2 changes: 1 addition & 1 deletion docs/actions/meeting.update.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
default_projector_agenda_item_list_ids: Ids;
default_projector_topic_ids: Ids;
default_projector_list_of_speakers_ids: Ids;
default_projector_current_list_of_speakers_ids: Ids;
default_projector_current_los_ids: Ids;
default_projector_motion_ids: Ids;
default_projector_amendment_ids: Ids;
default_projector_motion_block_ids: Ids;
Expand Down
4 changes: 2 additions & 2 deletions global/data/example-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@
"default_projector_agenda_item_list_ids": [1],
"default_projector_topic_ids": [1],
"default_projector_list_of_speakers_ids": [2],
"default_projector_current_list_of_speakers_ids": [2],
"default_projector_current_los_ids": [2],
"default_projector_motion_ids": [1],
"default_projector_amendment_ids": [1],
"default_projector_motion_block_ids": [1],
Expand Down Expand Up @@ -2410,7 +2410,7 @@
"show_clock": true,
"sequential_number": 2,
"used_as_default_projector_for_list_of_speakers_in_meeting_id": 1,
"used_as_default_projector_for_current_list_of_speakers_in_meeting_id": 1,
"used_as_default_projector_for_current_los_in_meeting_id": 1,
"meeting_id": 1
}
},
Expand Down
3 changes: 3 additions & 0 deletions openslides_backend/models/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class Field:
constant: bool
default: str | None
constraints: dict[str, Any]
is_view_field: bool

def __init__(
self,
Expand All @@ -52,6 +53,7 @@ def __init__(
constant: bool = False,
default: Any | None = None,
constraints: dict[str, Any] | None = None,
is_view_field: bool = False,
) -> None:
self.required = required
self.read_only = read_only
Expand All @@ -60,6 +62,7 @@ def __init__(
if not self.required and constraints and "enum" in constraints:
constraints["enum"].append(None)
self.constraints = constraints or {}
self.is_view_field = is_view_field
self.schema_validator = fastjsonschema.compile(self.get_schema())

def get_schema(self) -> Schema:
Expand Down
2 changes: 1 addition & 1 deletion openslides_backend/models/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class MeetingModelMixin:
"agenda_item_list",
"topic",
"list_of_speakers",
"current_list_of_speakers",
"current_los",
"motion",
"amendment",
"motion_block",
Expand Down
Loading

0 comments on commit 819fe4b

Please sign in to comment.