File: functions.py

package info (click to toggle)
python-django-netfields 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 444 kB
  • sloc: python: 2,067; sh: 7; makefile: 4; sql: 2
file content (119 lines) | stat: -rw-r--r-- 2,449 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
"""Postgres network address functions.

https://www.postgresql.org/docs/11/functions-net.html
"""

from django.db.models import BooleanField, Func, IntegerField, TextField

from .fields import CidrAddressField, InetAddressField, MACAddress8Field


class Abbrev(Func):
    """Function to abbreviate field as text."""

    arity = 1
    function = 'ABBREV'
    output_field = TextField()


class Broadcast(Func):
    """Function to extract broadcast address for network."""

    arity = 1
    function = 'BROADCAST'
    output_field = InetAddressField()


class Family(Func):
    """Function to extract family of address; 4 for IPv4, 6 for IPv6."""

    arity = 1
    function = 'FAMILY'
    output_field = IntegerField()


class Host(Func):
    """Function to extract IP address as text."""

    arity = 1
    function = 'HOST'
    output_field = TextField()


class Hostmask(Func):
    """Function to construct host mask for network."""

    arity = 1
    function = 'HOSTMASK'
    output_field = InetAddressField()


class Masklen(Func):
    """Function to extract netmask length."""

    arity = 1
    function = 'MASKLEN'
    output_field = IntegerField()


class Netmask(Func):
    """Function to construct netmask for network."""

    arity = 1
    function = 'NETMASK'
    output_field = InetAddressField()


class Network(Func):
    """Function to extract network part of address."""

    arity = 1
    function = 'NETWORK'
    output_field = CidrAddressField()


class SetMasklen(Func):
    """Function to set netmask length."""

    arity = 2
    function = 'SET_MASKLEN'
    output_field = InetAddressField()


class AsText(Func):
    """Function to extract IP address and netmask length as text."""

    arity = 1
    function = 'TEXT'
    output_field = TextField()


class IsSameFamily(Func):
    """Function to test that addresses are from the same family."""

    arity = 2
    function = 'INET_SAME_FAMILY'
    output_field = BooleanField()


class Merge(Func):
    """Function to calculate the smallest network which includes both of the given
    networks.
    """

    arity = 2
    function = 'INET_MERGE'
    output_field = CidrAddressField()


class Trunc(Func):

    arity = 1
    function = 'TRUNC'


class Macaddr8Set7bit(Func):
    """Function that sets 7th bit to one, also known as modified EUI-64, for inclusion in an IPv6 address"""
    arity = 1
    function = 'MACADDR8_SET7BIT'
    output_field = MACAddress8Field()