|
1 | 1 | from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
|
| 2 | +from django.contrib.contenttypes.models import ContentType |
2 | 3 | from django.contrib.postgres.fields import ArrayField, IntegerRangeField
|
3 | 4 | from django.core.exceptions import ValidationError
|
4 | 5 | from django.core.validators import MaxValueValidator, MinValueValidator
|
5 | 6 | from django.db import models
|
6 | 7 | from django.db.backends.postgresql.psycopg_any import NumericRange
|
7 | 8 | from django.utils.translation import gettext_lazy as _
|
8 | 9 |
|
9 |
| -from dcim.models import Interface |
| 10 | +from dcim.models import Interface, Site, SiteGroup |
10 | 11 | from ipam.choices import *
|
11 | 12 | from ipam.constants import *
|
12 | 13 | from ipam.querysets import VLANQuerySet, VLANGroupQuerySet
|
@@ -279,12 +280,20 @@ def clean(self):
|
279 | 280 | super().clean()
|
280 | 281 |
|
281 | 282 | # Validate VLAN group (if assigned)
|
282 |
| - if self.group and self.site and self.group.scope != self.site: |
283 |
| - raise ValidationError( |
284 |
| - _( |
285 |
| - "VLAN is assigned to group {group} (scope: {scope}); cannot also assign to site {site}." |
286 |
| - ).format(group=self.group, scope=self.group.scope, site=self.site) |
287 |
| - ) |
| 283 | + if self.group and self.site and self.group.scope_type == ContentType.objects.get_for_model(Site): |
| 284 | + if self.site != self.group.scope: |
| 285 | + raise ValidationError( |
| 286 | + _( |
| 287 | + "VLAN is assigned to group {group} (scope: {scope}); cannot also assign to site {site}." |
| 288 | + ).format(group=self.group, scope=self.group.scope, site=self.site) |
| 289 | + ) |
| 290 | + if self.group and self.site and self.group.scope_type == ContentType.objects.get_for_model(SiteGroup): |
| 291 | + if self.site not in self.group.scope.sites.all(): |
| 292 | + raise ValidationError( |
| 293 | + _( |
| 294 | + "The assigned site {site} is not a member of the assigned group {group} (scope: {scope})." |
| 295 | + ).format(group=self.group, scope=self.group.scope, site=self.site) |
| 296 | + ) |
288 | 297 |
|
289 | 298 | # Check that the VLAN ID is permitted in the assigned group (if any)
|
290 | 299 | if self.group:
|
|
0 commit comments