コード例 #1
0
def _save_device(self, request, mac_address):
    """
    A person is trying to save some new settings on a device
    """

    logger = logging.getLogger('dri.custom')
    form = DeviceForm(request.POST)
    log_object(request.POST)
    if not form.errors:
        submitted_policy = request.POST.get('policy')
        submitted_device_type = request.POST.get('device_type')
        submitted_device_name = request.POST.get('device_name')
        try:
            device = Device.objects.get(mac_address=mac_address.upper())
        except Device.DoesNotExist:
            logger.info("NO object! Bailing out!")
            return HttpResponse("Name: (%s) does not exist in the database." % mac_address)

        device.name = submitted_device_name
        try:
            policy = Policy.objects.get(name=submitted_policy)
            device.policy = policy
        except Policy.DoesNotExist:
            logger.info("NO POLICY object! Bailing out!")
            return HttpResponse("POLICY: (%s) does not exist in the database." % submitted_policy)

        try:
            device_type = DeviceType.objects.get(name=submitted_device_type)
            device.device_type = device_type
        except DeviceType.DoesNotExist:
            logger.info("NO DEVICE_TYPE object! Bailing out!")
            return HttpResponse("DEVICE_TYPE: (%s) does not exist in the database." % submitted_device_type)

        device.save()
        return HttpResponse("<h1>SAVED</h1>")
コード例 #2
0
def _save_policy(request, traffic_policy_name):
    """
    A user is saving changes to a traffic_policy
    """
    logger = logging.getLogger('dri.custom')
    log_object(request.POST)
    description = request.POST.get('description')
    try:
        traffic_policy = TrafficPolicy.objects.get(name=traffic_policy_name)
    except TrafficPolicy.DoesNotExist:
        logger.info("NO object! Bailing out!")
        msg = "Name: (%s) does not exist in the database." % traffic_policy_name
        return {"success": False,
                "message": msg,
               }

    traffic_policy.description = description
    _purge_traffic_matchers(traffic_policy)
    for index in xrange(0, len(request.POST.keys())):
        object_name = "regex_%d" % index
        if object_name in request.POST.keys():
            logger.info("Post: %s // %s" % (object_name, request.POST.get(object_name)))
            regex = request.POST.get(object_name).strip()
            if regex:
                traffic_matcher = TrafficMatcher(regex = regex, traffic_policy=traffic_policy)
                traffic_matcher.save()
    traffic_policy.save()
    return {"success": True,
            "message": "Saved",
           }
コード例 #3
0
def enable_device(request):
    """
    The user wants to temporarily enable a device
    """
    logger = logging.getLogger('dri.custom')
    output = {"success": True,
              "message": ""}
    if request.method != 'POST':
        output['success'] = False
        output["message"] = "Must use a POST"
        return output
    mac_address = request.POST.get("mac_address").upper()
    provided_duration = request.POST.get("duration", "30")
    logger.info("IN ENABLE-DEVICES: %s" % mac_address)
    try:
        duration = int(provided_duration)
        device = None
        logger.info("Duration: %s" % duration)
        try:
            device = Device.objects.get(mac_address=mac_address)
            logger.info("device found. Device name: %s" % device.name)
        except Device.DoesNotExist:
            return {"success": False,
                    "message": "can't look that record up: (%s)" % mac_address
                   }
        temporary_approval = TemporaryApproval()
        temporary_approval.device = device
        temporary_approval.set_parameters(duration)
        temporary_approval.save()
        output['message'] = "Saved"
        logger.info("Saving approval")
    except ValueError:
        msg = "Bad duration provided: (%s)" % provided_duration
        logger.info(msg)
        output["success"] = False
        output['message'] = msg
    log_object(output, "MY OUTPUT")
    return output