Skip to content

Commit

Permalink
Use OID class to validate sysobjectid
Browse files Browse the repository at this point in the history
  • Loading branch information
johannaengland committed Mar 23, 2023
1 parent c8bb100 commit c242631
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions python/nav/web/seeddb/forms/__init__.py
Expand Up @@ -36,6 +36,7 @@
Netbox,
)
from nav.models.cabling import Cabling
from nav.oids import OID

_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -316,12 +317,14 @@ class Meta(object):

def clean_sysobjectid(self):
sysobjectid = self.cleaned_data.get('sysobjectid')
sysobjectid_numbers = sysobjectid.strip(SEPARATOR).split(SEPARATOR)

if all(number.isdigit() for number in sysobjectid_numbers):
return sysobjectid.strip(SEPARATOR)

raise forms.ValidationError("Sysobjectid can only contain digits and periods.")
try:
sysobjectid_oid = OID(sysobjectid)
except ValueError:
raise forms.ValidationError(
"Sysobjectid can only contain digits and periods."
)
else:
return str(sysobjectid_oid).strip(SEPARATOR)


class CablingForm(forms.ModelForm):
Expand Down

0 comments on commit c242631

Please sign in to comment.