<commit_before>from piston.handler import BaseHandler, rc
from systems.models import System, SystemRack,SystemStatus,NetworkAdapter,KeyValue
from truth.models import Truth, KeyValue as TruthKeyValue
from dhcp.DHCP import DHCP as DHCPInterface
from dhcp.models import DHCP
from MacroExpansion import MacroExpansion
from KeyValueTree import KeyValueTree
import re
try:
    import json
except:
    from django.utils import simplejson as json
from django.test.client import Client
from settings import API_ACCESS

class KeyValueHandler(BaseHandler):
    allowed_methods = API_ACCESS
    def create(self, request, key_value_id=None):
        if 'system_id' in request.POST:
            n = KeyValue()
            system = System.objects.get(id=request.POST['system_id'])
            n.system = system
            if 'key' in request.POST:
                n.key = request.POST['key']
            if 'value' in request.POST:
                n.value = request.POST['value']
            try:
                n.save()
                resp = rc.ALL_OK
                resp.write('json = {"id":%i}' % (n.id))
            except:
                resp = rc.NOT_FOUND
                resp.write('Unable to Create Key/Value Pair')
            return resp
        elif 'truth_name' in request.POST:
            n = TruthKeyValue()
            truth = Truth.objects.get(name=request.POST['truth_name'])
            n.truth = truth
            if 'key' in request.POST:
                n.key = request.POST['key']
            if 'value' in request.POST:
                n.value = request.POST['value']
            try:
                n.save()
                resp = rc.ALL_OK
                resp.write('json = {"id":%i}' % (n.id))
            except:
                resp = rc.NOT_FOUND
                resp.write('Unable to Create Key/Value Pair')
            return resp
        else:
            resp = rc.NOT_FOUND
            resp.write('system_id or truth_name required')
    def build_validation_array(self):
        input_regex_array = []
        output_regex_array = []
        error_message_array = []

        ipv4_regex = re.compile(r'((2[0-5]|1[0-9]|[0-9])?[0-9]\.){3}((2[0-5]|1[0-9]|[0-9])?[0-9])')
        true_false_regex = re.compile('(^True$|^False$)')

        input_regex_array.append(re.compile('nic\.\d+\.ipv4_address\.\d+'))
        output_regex_array.append(ipv4_regex)
        error_message_array.append('Requires IP Address')

        input_regex_array.append(re.compile('^dhcp\.scope\.netmask$'))
        output_regex_array.append(ipv4_regex)
        error_message_array.append('Requires Subnet Mask')

        input_regex_array.append(re.compile('^is_dhcp_scope$'))
        output_regex_array.append(re.compile(true_false_regex))
        error_message_array.append('Requires True|False')

        input_regex_array.append(re.compile('^dhcp\.scope\.start$'))
        output_regex_array.append(re.compile(ipv4_regex))
        error_message_array.append('Requires IP Address')
        
        input_regex_array.append(re.compile('^dhcp\.scope\.end$'))
        output_regex_array.append(re.compile(ipv4_regex))
        error_message_array.append('Requires IP Address')

        input_regex_array.append(re.compile('^dhcp\.pool\.start$'))
        output_regex_array.append(re.compile(ipv4_regex))
        error_message_array.append('Requires IP Address')

        input_regex_array.append(re.compile('^dhcp\.pool\.end$'))
        output_regex_array.append(re.compile(ipv4_regex))
        error_message_array.append('Requires IP Address')

        input_regex_array.append(re.compile('^dhcp\.option\.ntp_server\.\d+$'))
        output_regex_array.append(re.compile(ipv4_regex))
        error_message_array.append('Requires IP Address')

        input_regex_array.append(re.compile('^dhcp\.dns_server\.\d+$'))
        output_regex_array.append(re.compile(ipv4_regex))
        error_message_array.append('Requires IP Address')

        input_regex_array.append(re.compile('^dhcp\.option_router\.\d+$'))
        output_regex_array.append(re.compile(ipv4_regex))
        error_message_array.append('Requires IP Address')

        input_regex_array.append(re.compile('^dhcp\.option\.subnet_mask\.\d+$'))
        output_regex_array.append(re.compile(ipv4_regex))
        error_message_array.append('Requires IP Address')

        input_regex_array.append(re.compile('^dhcp\.pool\.allow_booting\.\d+$'))
        output_regex_array.append(re.compile(true_false_regex))
        error_message_array.append('Requires True|False')
        
        input_regex_array.append(re.compile('^dhcp\.pool\.allow_bootp\.\d+$'))
        output_regex_array.append(re.compile(true_false_regex))
        error_message_array.append('Requires True|False')

        input_regex_array.append(re.compile('^nic\.\d+\.mac_address\.\d+$'))
        output_regex_array.append(re.compile('^([0-9a-f]{2}([:-]|$)){6}$', re.I))
        error_message_array.append('Requires Mac Address XX:XX:XX:XX:XX:XX')

        return input_regex_array, output_regex_array, error_message_array

    def validate(self, key, passed_value):
        error_message = None
        return_regex = None
        return_bool = True


        input_regex_array, output_regex_array, error_message_array = self.build_validation_array()
        ## Here we loop through all of the possible input validation array. If they key matches one, then we need to validate the value for the key/value pair
        for i in range(0, len(input_regex_array)):
            if input_regex_array[i].match(key):
                return_regex = output_regex_array[i]
                error_message = error_message_array[i];
                continue

        ## Check if we should validate the value portion of the key/value pair. No use validating it if the key doesn't require it
        if return_regex is not None:
            if return_regex.match(passed_value) is None:
                return_bool = False
            else:
                error_message = None


        return return_bool, error_message


    def update(self, request, key_value_id=None):
        ###TODO This whole method is not functioning correctly. Just for version 2. Not getting the system_id or truth_id from the poster firefox plugin
        if 'system_id' in request.POST:
            n = None
            key_validated, validation_error_string = self.validate(request.POST['key'], request.POST['value']) 
            if key_validated is False:
                resp = rc.FORBIDDEN
                resp.write('Validation Failed for %s %s' % (request.POST['key'], validation_error_string) )
                return resp
            try:
                n = KeyValue.objects.get(id=key_value_id,key=request.POST['key'])
                system = System.objects.get(id=request.POST['system_id'])
            except:
                resp = rc.NOT_FOUND
                resp.write('Neither system_id or truth_id found')<commit_msg>Support of keyvalue to update based on system_id and key<commit_after>from piston.handler import BaseHandler, rc
from systems.models import System, SystemRack,SystemStatus,NetworkAdapter,KeyValue
from truth.models import Truth, KeyValue as TruthKeyValue
from dhcp.DHCP import DHCP as DHCPInterface
from dhcp.models import DHCP
from MacroExpansion import MacroExpansion
from KeyValueTree import KeyValueTree
import re
try:
    import json
except:
    from django.utils import simplejson as json
from django.test.client import Client
from settings import API_ACCESS

class KeyValueHandler(BaseHandler):
    allowed_methods = API_ACCESS
    def create(self, request, key_value_id=None):
        if 'system_id' in request.POST:
            n = KeyValue()
            system = System.objects.get(id=request.POST['system_id'])
            n.system = system
            if 'key' in request.POST:
                n.key = request.POST['key']
            if 'value' in request.POST:
                n.value = request.POST['value']
            try:
                n.save()
                resp = rc.ALL_OK
                resp.write('json = {"id":%i}' % (n.id))
            except:
                resp = rc.NOT_FOUND
                resp.write('Unable to Create Key/Value Pair')
            return resp
        elif 'truth_name' in request.POST:
            n = TruthKeyValue()
            truth = Truth.objects.get(name=request.POST['truth_name'])
            n.truth = truth
            if 'key' in request.POST:
                n.key = request.POST['key']
            if 'value' in request.POST:
                n.value = request.POST['value']
            try:
                n.save()
                resp = rc.ALL_OK
                resp.write('json = {"id":%i}' % (n.id))
            except:
                resp = rc.NOT_FOUND
                resp.write('Unable to Create Key/Value Pair')
            return resp
        else:
            resp = rc.NOT_FOUND
            resp.write('system_id or truth_name required')
    def build_validation_array(self):
        input_regex_array = []
        output_regex_array = []
        error_message_array = []

        ipv4_regex = re.compile(r'((2[0-5]|1[0-9]|[0-9])?[0-9]\.){3}((2[0-5]|1[0-9]|[0-9])?[0-9])')
        true_false_regex = re.compile('(^True$|^False$)')

        input_regex_array.append(re.compile('nic\.\d+\.ipv4_address\.\d+'))
        output_regex_array.append(ipv4_regex)
        error_message_array.append('Requires IP Address')

        input_regex_array.append(re.compile('^dhcp\.scope\.netmask$'))
        output_regex_array.append(ipv4_regex)
        error_message_array.append('Requires Subnet Mask')

        input_regex_array.append(re.compile('^is_dhcp_scope$'))
        output_regex_array.append(re.compile(true_false_regex))
        error_message_array.append('Requires True|False')

        input_regex_array.append(re.compile('^dhcp\.scope\.start$'))
        output_regex_array.append(re.compile(ipv4_regex))
        error_message_array.append('Requires IP Address')
        
        input_regex_array.append(re.compile('^dhcp\.scope\.end$'))
        output_regex_array.append(re.compile(ipv4_regex))
        error_message_array.append('Requires IP Address')

        input_regex_array.append(re.compile('^dhcp\.pool\.start$'))
        output_regex_array.append(re.compile(ipv4_regex))
        error_message_array.append('Requires IP Address')

        input_regex_array.append(re.compile('^dhcp\.pool\.end$'))
        output_regex_array.append(re.compile(ipv4_regex))
        error_message_array.append('Requires IP Address')

        input_regex_array.append(re.compile('^dhcp\.option\.ntp_server\.\d+$'))
        output_regex_array.append(re.compile(ipv4_regex))
        error_message_array.append('Requires IP Address')

        input_regex_array.append(re.compile('^dhcp\.dns_server\.\d+$'))
        output_regex_array.append(re.compile(ipv4_regex))
        error_message_array.append('Requires IP Address')

        input_regex_array.append(re.compile('^dhcp\.option_router\.\d+$'))
        output_regex_array.append(re.compile(ipv4_regex))
        error_message_array.append('Requires IP Address')

        input_regex_array.append(re.compile('^dhcp\.option\.subnet_mask\.\d+$'))
        output_regex_array.append(re.compile(ipv4_regex))
        error_message_array.append('Requires IP Address')

        input_regex_array.append(re.compile('^dhcp\.pool\.allow_booting\.\d+$'))
        output_regex_array.append(re.compile(true_false_regex))
        error_message_array.append('Requires True|False')
        
        input_regex_array.append(re.compile('^dhcp\.pool\.allow_bootp\.\d+$'))
        output_regex_array.append(re.compile(true_false_regex))
        error_message_array.append('Requires True|False')

        input_regex_array.append(re.compile('^nic\.\d+\.mac_address\.\d+$'))
        output_regex_array.append(re.compile('^([0-9a-f]{2}([:-]|$)){6}$', re.I))
        error_message_array.append('Requires Mac Address XX:XX:XX:XX:XX:XX')

        return input_regex_array, output_regex_array, error_message_array

    def validate(self, key, passed_value):
        error_message = None
        return_regex = None
        return_bool = True


        input_regex_array, output_regex_array, error_message_array = self.build_validation_array()
        ## Here we loop through all of the possible input validation array. If they key matches one, then we need to validate the value for the key/value pair
        for i in range(0, len(input_regex_array)):
            if input_regex_array[i].match(key):
                return_regex = output_regex_array[i]
                error_message = error_message_array[i];
                continue

        ## Check if we should validate the value portion of the key/value pair. No use validating it if the key doesn't require it
        if return_regex is not None:
            if return_regex.match(passed_value) is None:
                return_bool = False
            else:
                error_message = None


        return return_bool, error_message


    def update(self, request, key_value_id=None):
        ###TODO This whole method is not functioning correctly. Just for version 2. Not getting the system_id or truth_id from the poster firefox plugin
        if 'system_id' in request.POST:
            n = None
            found = False

            key_validated, validation_error_string = self.validate(request.POST['key'], request.POST['value']) 
            if key_validated is False:
                resp = rc.FORBIDDEN
                resp.write('Validation Failed for %s %s' % (request.POST['key'], validation_error_string) )
                return resp

            try:
                n = KeyValue.objects.get(id=key_value_id,key=request.POST['key'])
                system = System.objects.get(id=request.POST['system_id'])
            except:
                found = False
            if found is False:
                try:
                    system = System.objects.get(id=request.POST['system_id'])
                    n = KeyValue.objects.get(system=system,key=request.POST['key'])
                except:
                    found = False

            if found is False:
                resp = rc.NOT_FOUND
                resp.write('Neither system_id or truth_id found')
                return resp
{'fraction_non_alphanumeric': 0.11364750367621315, 'fraction_numerical': 0.005461802394790281, 'mean_word_length': 2.4715605250364607, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4707935', 'n_tokens_mistral': 4349, 'n_tokens_neox': 4166, 'n_words': 804}