screens/views.py
def load_widget(request):
    """
    Load the configuration for a given screen widget

    Widgets can provide any functionality to the screen
    widgets can store data in 3 different places:
        Global widget configuration is stored in the ScreenWidgetConfig db table
        Global widget data is stored in the ScreenWidgetData db table
        Per Screen / Per Widget instance data is stored in the layout object on the screen itself

        For example, you may configure a graph widget globally to point to a specific automation for data
        then each graph can load specific parameters from the global widget data db table to be shared among screens
        then finally, the specific time slice can be stored in the layout data on the screen
    :param request: http request
    :return: html template
    """
    logger.info("__ screens load_widget_ __")
    required_fields = set(["widget_id"])
    if not required_fields.issubset(request.POST):
        logger.error("Did not find all required fields in request")
        return render(request, "error.html", {"error": "Invalid Parameters in POST"})

    widget_id = request.POST["widget_id"]

    # widget_layout_id is an identifier used to id the specific widget config stored
    # in the layout object of this screen. This is a per-screen / per-widget id
    # widgets defined as transient do not have a widget_layout_id
    widget_layout_id = request.POST["widget_layout_id"]

    widgets = settings.SCREEN_WIDGETS
    widget_name = ""

    found = False
    for w in widgets:
        if w["id"] == widget_id:
            widget_name = w["label"]
            widget_template = w["render_template"]
            found = True
            break

    if not found:
        return render(request, "screens/widgets/overlay_error.html",
                      {"error": "Could not find widget"})

    try:
        context = {"widget_id": widget_id,
                   "widget_name": widget_name,
                   "widget_layout_id": widget_layout_id,
                   "widget_global_config": {}
                   }

        # grab the widget global configuration if it exists and set on the context
        # FIXME - widget_type and widget_id are used interchangeably, this should just be widget_id basically everywhere
        if ScreenWidgetConfig.objects.filter(widget_type=widget_id).exists():
            widget_config = ScreenWidgetConfig.objects.get(widget_type=widget_id)
            # context.update
            context.update({"widget_global_config": widget_config.data})

        if "consumes_automation" in w:

            post_vars = dict()
            for v in request.POST:
                post_vars[v] = request.POST[v]

            post_vars["template_name"] = w["consumes_automation"]
            automation_results = aframe_utils.execute_template(post_vars)

            context.update({"automation_results": automation_results})
            try:
                # attempt to load results for the user! not every automation will return json though, so we can't
                # count on it!
                results_object = json.loads(automation_results['output'])
                context.update({"automation_output": results_object})
            except ValueError:
                print "Could not parse JSON output from automation!"
                logger.warn('Could not parse JSON output from automation!')
                pass

        return render(request, "screens/widgets/%s" % widget_template, context)

    except TemplateDoesNotExist:
        return render(request, "screens/widgets/overlay_error.html",
                      {"error": "Could not load widget configuration"})

    Load the configuration for a given screen widget

    Widgets can provide any functionality to the screen
    widgets can store data in 3 different places:
        Global widget configuration is stored in the ScreenWidgetConfig db table
        Global widget data is stored in the ScreenWidgetData db table
        Per Screen / Per Widget instance data is stored in the layout object on the screen itself

        For example, you may configure a graph widget globally to point to a specific automation for data
        then each graph can load specific parameters from the global widget data db table to be shared among screens
        then finally, the specific time slice can be stored in the layout data on the screen
    :param request: http request
    :return: html template
    
Load the configuration for a given screen widget
Widgets can provide any functionality to the screen
widgets can store data in 3 different places:
Global widget configuration is stored in the ScreenWidgetConfig db table
Global widget data is stored in the ScreenWidgetData db table
Per Screen / Per Widget instance data is stored in the layout object on the screen itself

For example, you may configure a graph widget globally to point to a specific automation for data
then each graph can load specific parameters from the global widget data db table to be shared among screens
then finally, the specific time slice can be stored in the layout data on the screen
Load the configuration for a given screen widget Widgets can provide any functionality to the screen widgets can store data in 3 different places : Global widget configuration is stored in the ScreenWidgetConfig db table Global widget data is stored in the ScreenWidgetData db table Per Screen / Per Widget instance data is stored in the layout object on the screen itself For example you may configure a graph widget globally to point to a specific automation for data then each graph can load specific parameters from the global widget data db table to be shared among screens then finally the specific time slice can be stored in the layout data on the screen
def load_widget(request):
    logger.info("__ screens load_widget_ __")
    required_fields = set(["widget_id"])
    if not required_fields.issubset(request.POST):
        logger.error("Did not find all required fields in request")
        return render(request, "error.html", {"error": "Invalid Parameters in POST"})
    widget_id = request.POST["widget_id"]
    widget_layout_id = request.POST["widget_layout_id"]
    widgets = settings.SCREEN_WIDGETS
    widget_name = ""
    found = False
    for w in widgets:
        if w["id"] == widget_id:
            widget_name = w["label"]
            widget_template = w["render_template"]
            found = True
            break
    if not found:
        return render(request, "screens/widgets/overlay_error.html",
                      {"error": "Could not find widget"})
    try:
        context = {"widget_id": widget_id,
                   "widget_name": widget_name,
                   "widget_layout_id": widget_layout_id,
                   "widget_global_config": {}
                   }
        if ScreenWidgetConfig.objects.filter(widget_type=widget_id).exists():
            widget_config = ScreenWidgetConfig.objects.get(widget_type=widget_id)
            context.update({"widget_global_config": widget_config.data})
        if "consumes_automation" in w:
            post_vars = dict()
            for v in request.POST:
                post_vars[v] = request.POST[v]
            post_vars["template_name"] = w["consumes_automation"]
            automation_results = aframe_utils.execute_template(post_vars)
            context.update({"automation_results": automation_results})
            try:
                results_object = json.loads(automation_results['output'])
                context.update({"automation_output": results_object})
            except ValueError:
                print "Could not parse JSON output from automation!"
                logger.warn('Could not parse JSON output from automation!')
                pass
        return render(request, "screens/widgets/%s" % widget_template, context)
    except TemplateDoesNotExist:
        return render(request, "screens/widgets/overlay_error.html",
                      {"error": "Could not load widget configuration"})
def load_widget ( request ) : logger . info ( "__ screens load_widget_ __" ) required_fields = set ( [ "widget_id" ] ) if not required_fields . issubset ( request . POST ) : logger . error ( "Did not find all required fields in request" ) return render ( request , "error.html" , { "error" : "Invalid Parameters in POST" } ) widget_id = request . POST [ "widget_id" ] widget_layout_id = request . POST [ "widget_layout_id" ] widgets = settings . SCREEN_WIDGETS widget_name = "" found = False for w in widgets : if w [ "id" ] == widget_id : widget_name = w [ "label" ] widget_template = w [ "render_template" ] found = True break if not found : return render ( request , "screens/widgets/overlay_error.html" , { "error" : "Could not find widget" } ) try : context = { "widget_id" : widget_id , "widget_name" : widget_name , "widget_layout_id" : widget_layout_id , "widget_global_config" : { } } if ScreenWidgetConfig . objects . filter ( widget_type = widget_id ) . exists ( ) : widget_config = ScreenWidgetConfig . objects . get ( widget_type = widget_id ) context . update ( { "widget_global_config" : widget_config . data } ) if "consumes_automation" in w : post_vars = dict ( ) for v in request . POST : post_vars [ v ] = request . POST [ v ] post_vars [ "template_name" ] = w [ "consumes_automation" ] automation_results = aframe_utils . execute_template ( post_vars ) context . update ( { "automation_results" : automation_results } ) try : results_object = json . loads ( automation_results [ 'output' ] ) context . update ( { "automation_output" : results_object } ) except ValueError : print "Could not parse JSON output from automation!" logger . warn ( 'Could not parse JSON output from automation!' ) pass return render ( request , "screens/widgets/%s" % widget_template , context ) except TemplateDoesNotExist : return render ( request , "screens/widgets/overlay_error.html" , { "error" : "Could not load widget configuration" } )
Load the configuration for a given screen widget
Widgets can provide any functionality to the screen
widgets can store data in 3 different places:
Global widget configuration is stored in the ScreenWidgetConfig db table
Global widget data is stored in the ScreenWidgetData db table
Per Screen / Per Widget instance data is stored in the layout object on the screen itself
Load the configuration for a given screen widget Widgets can provide any functionality to the screen widgets can store data in 3 different places : Global widget configuration is stored in the ScreenWidgetConfig db table Global widget data is stored in the ScreenWidgetData db table Per Screen / Per Widget instance data is stored in the layout object on the screen itself
"""
    Load the configuration for a given screen widget

    Widgets can provide any functionality to the screen
    widgets can store data in 3 different places:
        Global widget configuration is stored in the ScreenWidgetConfig db table
        Global widget data is stored in the ScreenWidgetData db table
        Per Screen / Per Widget instance data is stored in the layout object on the screen itself

        For example, you may configure a graph widget globally to point to a specific automation for data
        then each graph can load specific parameters from the global widget data db table to be shared among screens
        then finally, the specific time slice can be stored in the layout data on the screen
    :param request: http request
    :return: html template
    """ # widget_layout_id is an identifier used to id the specific widget config stored # in the layout object of this screen. This is a per-screen / per-widget id # widgets defined as transient do not have a widget_layout_id # grab the widget global configuration if it exists and set on the context # FIXME - widget_type and widget_id are used interchangeably, this should just be widget_id basically everywhere # context.update # attempt to load results for the user! not every automation will return json though, so we can't # count on it!
None
request
None