| 285 | |
| 286 | |
| 287 | class ProjectDomainForm(StarletteForm): |
| 288 | domain_id = HiddenField() |
| 289 | hostname = StringField( |
| 290 | _l("Domain"), |
| 291 | validators=[ |
| 292 | DataRequired(), |
| 293 | Length(min=1, max=255), |
| 294 | Regexp( |
| 295 | r"^[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+$", |
| 296 | message=_l("Please enter a valid domain name"), |
| 297 | ), |
| 298 | ], |
| 299 | ) |
| 300 | type = SelectField( |
| 301 | _l("Type"), |
| 302 | validators=[DataRequired()], |
| 303 | choices={ |
| 304 | _l("Routing"): [("route", _l("Route"))], |
| 305 | _l("Permanent Redirect"): [ |
| 306 | ("301", _l("301 - Moved Permanently")), |
| 307 | ("308", _l("308 - Permanent Redirect")), |
| 308 | ], |
| 309 | _l("Temporary Redirect"): [ |
| 310 | ("302", _l("302 - Found")), |
| 311 | ("307", _l("307 - Temporary Redirect")), |
| 312 | ], |
| 313 | }, |
| 314 | ) |
| 315 | environment_id = SelectField(_l("Environment"), choices=[]) |
| 316 | |
| 317 | def __init__( |
| 318 | self, |
| 319 | *args, |
| 320 | project: Project, |
| 321 | domains: list[Domain] | None = None, |
| 322 | db: AsyncSession, |
| 323 | **kwargs, |
| 324 | ): |
| 325 | super().__init__(*args, **kwargs) |
| 326 | self.project = project |
| 327 | self.domains = domains or [] |
| 328 | self.db = db |
| 329 | |
| 330 | self.environment_id.choices = [ |
| 331 | (env["id"], env["name"]) for env in project.active_environments |
| 332 | ] |
| 333 | |
| 334 | async def async_validate_hostname(self, field): |
| 335 | if field.data: |
| 336 | query = select(Domain).where( |
| 337 | func.lower(Domain.hostname) == field.data.lower() |
| 338 | ) |
| 339 | if self.domain_id.data: |
| 340 | query = query.where(Domain.id != int(self.domain_id.data)) |
| 341 | result = await self.db.execute(query) |
| 342 | domain = result.scalar_one_or_none() |
| 343 | if domain: |
| 344 | raise ValidationError(_("This domain is already in use.")) |
nothing calls this directly
no outgoing calls
no test coverage detected