From 9f5817b4922794083e75c2b8befadfb44ea492d0 Mon Sep 17 00:00:00 2001 From: bashmak Date: Sat, 29 Jul 2017 18:57:51 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A2=D0=B5=D0=BF=D0=B5=D1=80=D1=8C=20=D0=BE?= =?UTF-8?q?=D0=BD=D1=83=D1=88=D0=BA=D0=B8=20=D1=80=D0=B5=D0=B3=D0=B8=D1=81?= =?UTF-8?q?=D1=82=D1=80=D0=B8=D1=80=D1=83=D1=8E=D1=82=D1=81=D1=8F=20=D0=BF?= =?UTF-8?q?=D1=80=D0=B8=20=D1=81=D0=BE=D1=85=D1=80=D0=B0=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B8=20=D0=BE=D0=BD=D1=83=D1=88=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- devapp/models.py | 35 +++++++++++++++++++++++++++++++++++ devapp/onu_register.sh | 32 ++++++++++++++++++++++++++++++++ djing/__init__.py | 32 +++++--------------------------- 3 files changed, 72 insertions(+), 27 deletions(-) create mode 100755 devapp/onu_register.sh diff --git a/devapp/models.py b/devapp/models.py index 6561425..a028786 100644 --- a/devapp/models.py +++ b/devapp/models.py @@ -6,6 +6,8 @@ from .base_intr import DevBase from mydefs import MyGenericIPAddressField, MyChoicesAdapter from .dev_types import DLinkDevice, OLTDevice, OnuDevice from mapapp.models import Dot +from subprocess import call +from django.conf import settings DEVICE_TYPES = ( @@ -66,3 +68,36 @@ class Port(models.Model): class Meta: db_table = 'dev_port' unique_together = (('device', 'num')) + + +def dev_post_save_signal(sender, instance, **kwargs): + if instance.devtype != 'On': + return + grp = instance.user_group.pk + if grp == 87: + code = 'chk' + elif grp == 85: + code = 'drf' + elif grp == 86: + code = 'eme' + elif grp == 84: + code = 'kunc' + elif grp == 47: + code = 'mtr' + elif grp == 60: + code = 'nvg' + elif grp == 65: + code = 'ohot' + elif grp == 89: + code = 'psh' + elif grp == 92: + code = 'str' + elif grp == 80: + code = 'uy' + elif grp == 79 or grp == 91: + code = 'zrk' + newmac = str(instance.mac_addr) + call(["%s/devapp/onu_register.sh" % settings.BASE_DIR, newmac, code]) + + +models.signals.post_save.connect(dev_post_save_signal, sender=Device) diff --git a/devapp/onu_register.sh b/devapp/onu_register.sh new file mode 100755 index 0000000..32bd6a7 --- /dev/null +++ b/devapp/onu_register.sh @@ -0,0 +1,32 @@ +#!/bin/bash + + +# old mac address +if [[ $1 =~ ^([0-9A-Fa-f]{1,2}[:-]){5}([0-9A-Fa-f]{1,2})$ ]]; then + MAC=$1 +else + echo "Bad mac $MAC addr" + exit +fi + + +# part code +if [[ $2 =~ ^[a-zA-Z]+$ ]]; then + PART_CODE=$2 +else + echo 'code must contains only letters' + exit +fi + + +DHCP_PATH='/home/bashmak/Projects/djing/macs' +PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/bin + + +if grep "${MAC}" "${DHCP_PATH}/${PART_CODE}.conf" > /dev/null; then + # mac is already exists + exit +else + # add new mac + echo "subclass \"${PART_CODE}\" \"${MAC}\";" >> "${DHCP_PATH}/${PART_CODE}.conf" +fi diff --git a/djing/__init__.py b/djing/__init__.py index 8c21558..f71a4cf 100644 --- a/djing/__init__.py +++ b/djing/__init__.py @@ -1,39 +1,17 @@ import importlib -import warnings - -from django.conf import settings from netaddr import mac_unix, mac_eui48 -MAC_ADDR_REGEX = r'^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$' +MAC_ADDR_REGEX = r'^([0-9A-Fa-f]{1,2}[:-]){5}([0-9A-Fa-f]{1,2})$' class mac_linux(mac_unix): """MAC format with zero-padded all upper-case hex and colon separated""" - word_fmt = '%.2X' + word_fmt = '%x' + def default_dialect(eui_obj=None): - # Check to see if a default dialect class has been specified in settings, - # using 'module.dialect_cls' string and use importlib and getattr to retrieve dialect class. 'module' is the module and - # 'dialect_cls' is the class name of the custom dialect. The dialect must either be defined or imported by the module's - # __init__.py if the module is a package. - from .fields import MACAddressField # Remove import at v1.4 - if hasattr(settings, 'MACADDRESS_DEFAULT_DIALECT') and not MACAddressField.dialect: - module, dialect_cls = settings.MACADDRESS_DEFAULT_DIALECT.split('.') - dialect = getattr(importlib.import_module(module), dialect_cls, mac_linux) - return dialect - else: - if MACAddressField.dialect: # Remove this "if" statement at v1.4 - warnings.warn( - "The set_dialect class method on MACAddressField has been deprecated, in favor of the default_dialect " - "utility function and settings.MACADDRESS_DEFAULT_DIALECT. See macaddress.__init__.py source or the " - "project README for more information.", - DeprecationWarning, - ) - return MACAddressField.dialect - if eui_obj: - return eui_obj.dialect - else: - return mac_linux + return mac_linux + def format_mac(eui_obj, dialect): # Format a EUI instance as a string using the supplied dialect class, allowing custom string classes by