Skip to content

Commit

Permalink
Create field_model_ip_address_range.py
Browse files Browse the repository at this point in the history
  • Loading branch information
vanvianen committed Jul 7, 2024
1 parent 7816ae1 commit d343c97
Showing 1 changed file with 98 additions and 0 deletions.
98 changes: 98 additions & 0 deletions src/django-lan/fields/model/field_model_ip_address_range.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# -*- coding: utf-8 -*-


# =============================================================================
# Docstring
# =============================================================================

"""
Provides IP Address Range Model Field Class
=============================================
A field that helps in storing and validating a range of IP addresses.
Links:
- https://en.wikipedia.org/wiki/IP_address
"""


# =============================================================================
# Import
# =============================================================================

# Import | Standard Library
from typing import Any

# Import | Libraries
from django.db.models import CharField
from django.utils.translation import gettext_lazy as _

# Import | Local Modules
from ...utils.validators.validator_ip_address_range import validate_ip_address_range # noqa E501

# =============================================================================
# Variables
# =============================================================================


# =============================================================================
# Classes
# =============================================================================

class IPAddressRangeModelField(CharField):
"""
IP Address Range Model Field Class
====================================
A Django model field that stores and validates IP address versions
(IPv4, IPv6).
Attributes:
description (str): Description of what the field is used for.
"""

# Class | Variables
# =========================================================================

description = _(
"A field that stores and validates ranges of IP addresses."
)

# Class | Methods
# =========================================================================

def __init__(self, *args: Any, **kwargs: Any) -> None:
"""
Initializes the IPAddressRangeModelField with sufficient maximum length
to store two IPv4 or IPv6 addresses and custom validation.
"""
# Enough for two IPv6 addresses "XXXX:XXXX:...-XXXX:XXXX:..."
kwargs['max_length'] = 39
super().__init__(*args, **kwargs)
self.validators.append(validate_ip_address_range)

def clean(self, value: Any, model_instance: Any) -> Any:
"""
Cleans and validates the input value using the superclass’s cleaning
logic and the custom IP range validator.
Args:
value (Any): The value that needs to be cleaned.
model_instance (Any): The Django model instance that this field
belongs to.
Returns:
Any: The cleaned and validated value.
"""
# Validator will handle range check
return super().clean(value, model_instance)


# =============================================================================
# Public Interface
# =============================================================================

__all__ = [
"IPAddressRangeModelField",
]

0 comments on commit d343c97

Please sign in to comment.