Changeset - 2cef966f277b
[Not reviewed]
version_5
0 1 1
Dennis Fink - 10 years ago 2015-08-30 15:32:48
dennis.fink@c3l.lu
Added new check_ip function
2 files changed with 13 insertions and 5 deletions:
0 comments (0 inline, 0 general)
ennstatus/api/model.py
Show inline comments
 
@@ -4,24 +4,27 @@ import functools
 
import statistics
 

	
 
from pathlib import Path
 
from datetime import datetime
 

	
 
import jsonschema
 
import strict_rfc3339
 
import requests
 

	
 
from flask import current_app
 
from pkg_resources import resource_filename
 

	
 
from ..utils import check_ip
 

	
 

	
 
schema = json.load(
 
    open(
 
        resource_filename('ennstatus.api', 'schema/server.json'),
 
        encoding='utf-8'
 
    )
 
)
 

	
 
validate = functools.partial(
 
    jsonschema.validate,
 
    schema=schema,
 
    format_checker=jsonschema.FormatChecker()
 
)
 
@@ -170,30 +173,26 @@ class Server:
 

	
 
    @staticmethod
 
    def check_json_format(server):
 

	
 
        try:
 
            validate(server)
 
        except jsonschema.ValidationError as e:
 
            raise e
 

	
 
        for key in ('ip', 'ip6'):
 
            if key in server:
 
                address = ipaddress.ip_address(server[key])
 

	
 
                if any({address.is_private, address.is_multicast,
 
                        address.is_unspecified, address.is_reserved,
 
                        address.is_loopback}):
 
                if not check_ip(address):
 
                    raise ValueError('{} is not accepted!\n'.format(key))
 

	
 
        return True
 

	
 
    def save(self):
 

	
 
        filepath = Path('data') / (self.name.lower() + '.json')
 

	
 
        try:
 
            with filepath.open(mode='w', encoding='utf-8') as f:
 
                json.dump(self.__dict__, f, cls=ServerEncoder)
 
        except Exception as e:
 
            raise e
 

	
ennstatus/utils.py
Show inline comments
 
new file 100644
 

	
 
def check_ip(ipaddress):
 
    return not any({
 
        ipaddress.is_private,
 
        ipaddress.is_multicast,
 
        ipaddress.is_unspecified,
 
        ipaddress.is_reserved,
 
        ipaddress.is_loopback
 
    })
0 comments (0 inline, 0 general)