| 127 | |
| 128 | |
| 129 | class StorageProjectForm(StarletteForm): |
| 130 | association_id = HiddenField() |
| 131 | storage_id = HiddenField(_l("Storage"), validators=[DataRequired()]) |
| 132 | project_id = StringField(_l("Project"), validators=[DataRequired()]) |
| 133 | environment_ids = StringField(_l("Environments"), validators=[Optional()]) |
| 134 | |
| 135 | def __init__( |
| 136 | self, |
| 137 | request: Request, |
| 138 | *args, |
| 139 | storage: Storage | None = None, |
| 140 | storages: list[Storage] | None = None, |
| 141 | projects: list[Project], |
| 142 | associations: list["StorageProject"], |
| 143 | **kwargs, |
| 144 | ): |
| 145 | super().__init__(request, *args, **kwargs) |
| 146 | self.storage = storage |
| 147 | self.storages = storages or [] |
| 148 | self.projects = projects |
| 149 | self.associations = associations |
| 150 | self._projects_by_id = {project.id: project for project in projects} |
| 151 | self._storages_by_id = {storage.id: storage for storage in self.storages} |
| 152 | self._associations_by_id = { |
| 153 | str(association.id): association for association in associations |
| 154 | } |
| 155 | self._selected_project = None |
| 156 | self._selected_storage = None |
| 157 | self.association = None |
| 158 | if self.environment_ids.data in (None, ""): |
| 159 | self.environment_ids.data = [] |
| 160 | |
| 161 | def _parse_environment_ids(self, value): |
| 162 | return _parse_environment_ids(value) |
| 163 | |
| 164 | def validate_association_id(self, field): |
| 165 | if not field.data: |
| 166 | return |
| 167 | association = self._associations_by_id.get(field.data) |
| 168 | if not association: |
| 169 | raise ValidationError(_("Association not found.")) |
| 170 | if self.storage and association.storage_id != self.storage.id: |
| 171 | raise ValidationError(_("Association not found.")) |
| 172 | self.association = association |
| 173 | |
| 174 | def validate_storage_id(self, field): |
| 175 | if self.storage: |
| 176 | if field.data != self.storage.id: |
| 177 | raise ValidationError(_("Storage not found.")) |
| 178 | elif self._storages_by_id: |
| 179 | storage = self._storages_by_id.get(field.data) |
| 180 | if not storage: |
| 181 | raise ValidationError(_("Storage not found.")) |
| 182 | self._selected_storage = storage |
| 183 | else: |
| 184 | raise ValidationError(_("Storage not found.")) |
| 185 | if self.association and field.data != self.association.storage_id: |
| 186 | raise ValidationError(_("Storage cannot be changed.")) |
nothing calls this directly
no outgoing calls
no test coverage detected