Exemplo n.º 1
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