| 180 | |
| 181 | |
| 182 | class ProjectEnvironmentForm(StarletteForm): |
| 183 | environment_id = HiddenField() |
| 184 | color = SelectField( |
| 185 | _l("Color"), |
| 186 | validators=[DataRequired()], |
| 187 | choices=[(color, color) for color in COLORS], |
| 188 | ) |
| 189 | name = StringField(_l("Name"), validators=[DataRequired(), Length(min=1, max=255)]) |
| 190 | slug = StringField( |
| 191 | _l("Identifier"), |
| 192 | validators=[ |
| 193 | DataRequired(), |
| 194 | Length(min=1, max=255), |
| 195 | Regexp( |
| 196 | r"^[A-Za-z0-9][A-Za-z0-9._-]*[A-Za-z0-9]$", |
| 197 | message=_l( |
| 198 | "Environment IDs can only contain letters, numbers, hyphens, underscores and dots. They cannot start or end with a dot, underscore or hyphen." |
| 199 | ), |
| 200 | ), |
| 201 | ], |
| 202 | ) |
| 203 | branch = StringField( |
| 204 | _l("Branch"), validators=[DataRequired(), Length(min=1, max=255)] |
| 205 | ) |
| 206 | |
| 207 | def __init__(self, *args, project, **kwargs): |
| 208 | super().__init__(*args, **kwargs) |
| 209 | self.project = project |
| 210 | |
| 211 | def validate_name(self, field): |
| 212 | if self.environment_id.data: # type: ignore |
| 213 | env = self.project.get_environment_by_id(self.environment_id.data) # type: ignore |
| 214 | if env["slug"] == "production" and field.data != env["name"]: |
| 215 | raise ValidationError( |
| 216 | _("The production environment name cannot be modified.") |
| 217 | ) |
| 218 | |
| 219 | def validate_slug(self, field): |
| 220 | if self.environment_id.data: # type: ignore |
| 221 | env = self.project.get_environment_by_id(self.environment_id.data) # type: ignore |
| 222 | if field.data == env["slug"]: |
| 223 | return |
| 224 | |
| 225 | if env["slug"] == "production": |
| 226 | raise ValidationError( |
| 227 | _("The production environment identifier cannot be modified.") |
| 228 | ) |
| 229 | |
| 230 | # New environment |
| 231 | if self.project.has_active_environment_with_slug(field.data): |
| 232 | raise ValidationError( |
| 233 | _("This identifier is already in use by another environment.") |
| 234 | ) |
| 235 | |
| 236 | |
| 237 | class ProjectEnvironmentRemoveForm(StarletteForm): |
nothing calls this directly
no outgoing calls
no test coverage detected